the selection sort - clausen tech · the selection sort searches (linear search) all of the...
TRANSCRIPT
![Page 1: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/1.jpg)
The Selection Sort
Mr. Dave Clausen
La Cañada High School
![Page 2: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/2.jpg)
Mr. Dave Clausen 2
The Selection Sort
Description
The Selection Sort searches (linear search) all of
the elements in a list until it finds the smallest
element. It “swaps” this with the first element in
the list. Next it finds the smallest of the remaining
elements, and “swaps” it with the second element.
Repeat this process until you compare only the last
two elements in the list.
![Page 3: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/3.jpg)
Mr. Dave Clausen 3
The Selection Sort
Algorithm
• For each index position i
– Find the smallest data value in the array
from positions i through length - 1, where
length is the number of data values
stored.
– Exchange (swap) the smallest value with
the value at position i.
![Page 4: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/4.jpg)
Mr. Dave Clausen 4
6
2
1
3
5
4
We start by searching for the
smallest element in the List.
A Selection Sort Example
Smallest ?
![Page 5: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/5.jpg)
Mr. Dave Clausen 5
6
2
1
3
5
4
A Selection Sort Example
Smallest ?
![Page 6: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/6.jpg)
Mr. Dave Clausen 6
6
2
1
3
5
4
A Selection Sort Example
Smallest !
![Page 7: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/7.jpg)
Mr. Dave Clausen 7
6
2
1
3
5
4
A Selection Sort Example
Swap
![Page 8: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/8.jpg)
Mr. Dave Clausen 8
1
2
6
3
5
4
A Selection Sort Example
Swapped
![Page 9: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/9.jpg)
Mr. Dave Clausen 9
1
2
6
3
5
4
A Selection Sort Example
After the smallest element
is in the first position, we
continue searching with
the second element and
look for the next smallest
element.
Smallest ?
![Page 10: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/10.jpg)
Mr. Dave Clausen 10
1
2
6
3
5
4
A Selection Sort Example
In this special case, the
next smallest element is in
the second position
already. Swapping keeps
it in this position.
Smallest !
![Page 11: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/11.jpg)
Mr. Dave Clausen 11
1
2
6
3
5
4
A Selection Sort Example
Swapping keeps it in this
position.
Swapped
![Page 12: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/12.jpg)
Mr. Dave Clausen 12
1
2
6
3
5
4
A Selection Sort Example
After the next smallest element is
in the second position, we continue
searching with the third element
and look for the next smallest
element.
Smallest ?
![Page 13: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/13.jpg)
Mr. Dave Clausen 13
1
2
6
3
5
4
A Selection Sort Example
Smallest !
![Page 14: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/14.jpg)
Mr. Dave Clausen 14
1
2
6
3
5
4
A Selection Sort Example
Swap
![Page 15: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/15.jpg)
Mr. Dave Clausen 15
1
2
3
6
5
4
A Selection Sort Example
Swapped
![Page 16: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/16.jpg)
Mr. Dave Clausen 16
1
2
3
6
5
4
A Selection Sort Example
Smallest ?
![Page 17: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/17.jpg)
Mr. Dave Clausen 17
1
2
3
6
5
4
A Selection Sort Example
Smallest ?
![Page 18: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/18.jpg)
Mr. Dave Clausen 18
1
2
3
6
5
4
A Selection Sort Example
Smallest !
![Page 19: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/19.jpg)
Mr. Dave Clausen 19
1
2
3
6
5
4
A Selection Sort Example
Swap
![Page 20: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/20.jpg)
Mr. Dave Clausen 20
1
2
3
4
5
6
A Selection Sort Example
Swapped
![Page 21: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/21.jpg)
Mr. Dave Clausen 21
1
2
3
4
5
6
A Selection Sort Example
The last two elements are in
order, so no swap is necessary.
![Page 22: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/22.jpg)
Mr. Dave Clausen 22
What “Swapping” Means
6
2
1
3
5
4
TEMP
Place the first element into
the Temporary Variable.
6
![Page 23: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/23.jpg)
Mr. Dave Clausen 23
What “Swapping” Means
1
2
1
3
5
4
TEMP
Replace the first element
with the value of the
smallest element.
6
![Page 24: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/24.jpg)
Mr. Dave Clausen 24
What “Swapping” Means
1
2
6
3
5
4
TEMP
Replace the third element
(in this example) with the
Temporary Variable.
6
![Page 25: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/25.jpg)
Mr. Dave Clausen 25
Python Source Code:
Selection Sort
![Page 26: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/26.jpg)
Mr. Dave Clausen 26
Python Code for Swap Procedure
![Page 27: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/27.jpg)
Mr. Dave Clausen 27
Java Source Code: Selection Sort
public static void selectionSort(int[] list)
{
for (int index = 0; index < list.length - 1; index ++)
{
int minIndex = findMinimum(list, index );
if (minIndex != index )
swap(list, index , minIndex);
}
}
![Page 28: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/28.jpg)
Mr. Dave Clausen 28
Java Code For Find Minimum
public static int findMinimum(int[] list, int first)
{
int minIndex = first;
for (int index = first + 1; index < list.length; index ++)
if (list[index] < list[minIndex])
minIndex = index ;
return minIndex;
}
![Page 29: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/29.jpg)
Mr. Dave Clausen 29
Java Code for Swap Procedure
public static void swap(int[] list, int x, int y)
{
int temp = list[x];
list[x] = list[y];
list[y] = temp;
}
![Page 30: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/30.jpg)
Mr. Dave Clausen 30
C ++ Code For Selection Sortvoid Selection_Sort (apvector <int> & v)
{
int min_index = 0;
for (int index = 0; index < v.length( ) - 1; ++index)
{
min_index = Find_Minimum (v, index);
if (min_index ! = index)
Swap_Data ( v [index], v [min_index])
}
}
// Selection_Sort
![Page 31: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/31.jpg)
Mr. Dave Clausen 31
C ++ Code For Find Minimum
int Find_Minimum (const apvector <int> & v, int first)
{
int min_index = first;
for (int index = first + 1; index < v.length( ); ++index)
if ( v[index] < v[min_index])
min_index = index;
return min_index;
}
// Find_Minimum
![Page 32: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/32.jpg)
Mr. Dave Clausen 32
C ++ Code for Swap Procedure
void Swap_Data (int &number1, int &number2)
{
int temp;
temp = number1;
number1 = number2;
number2 = temp;
}
// End of Swap_Data function
![Page 33: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/33.jpg)
Mr. Dave Clausen 33
Pascal Code For Selection Sortprocedure SelectionSort (var IntArray: IntNumbers);
var element, SmallIndex, index: integer;
begin
for element := 1 to (MaxNum - 1) do
begin
SmallIndex := element;
for index := (element + 1) to MaxNum do
if IntArray [index] < IntArray [SmallIndex]
then SmallIndex := index;
Swap (IntArray [element], IntArray [SmallIndex])
end;
end; {SelectionSort}
![Page 34: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/34.jpg)
Mr. Dave Clausen 34
Pascal Code for Swap Procedure
procedure Swap (var number1, number2: integer);
var
temp: integer;
begin
temp := number1;
number1 := number2;
number2 := temp
end; {Swap}
![Page 35: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/35.jpg)
Mr. Dave Clausen 35
BASIC Code For Selection Sort8000 REM #################
8010 REM Selection Sort
8020 REM #################
8030 FOR ELEMENT = 1 TO MAXNUM - 1
8040 ::SmallIndex = element
8050 ::FOR INDEX = (element + 1) TO MAXNUM
8060 ::::::IF N (INDEX) < N (SmallIndex) THEN SmallIndex = INDEX
8070 ::NEXT INDEX
8080 ::TEMP = N (element)
8090 ::N (element) = N (SmallIndex)
8100 ::N (SmallIndex) = TEMP
8110 NEXT ELEMENT
8120 RETURN
![Page 36: The Selection Sort - Clausen Tech · The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first](https://reader036.vdocument.in/reader036/viewer/2022062910/5b93475809d3f280378cea71/html5/thumbnails/36.jpg)
Mr. Dave Clausen 36
Big - O Notation
Big - O notation is used to describe the efficiency
of a search or sort. The actual time necessary to
complete the sort varies according to the speed of
your system. Big - O notation is an approximate
mathematical formula to determine how many
operations are necessary to perform the search or
sort. The Big - O notation for the Selection Sort is
O(n2), because it takes approximately n2 passes to
sort the elements.