SortingSorting
AlgorithmsAlgorithms
Sorting reorders the elements in an Sorting reorders the elements in an array or list in either ascending or array or list in either ascending or descending order.descending order.
We sort values for reporting purposes We sort values for reporting purposes and to make searching more efficientand to make searching more efficient
To date we have only covered the To date we have only covered the bubble sort algorithm, but there are bubble sort algorithm, but there are other algorithms, some more effecientother algorithms, some more effecient
Analysis of the bubble sortAnalysis of the bubble sort
The bubble sort is one of the least efficient of The bubble sort is one of the least efficient of the sorting algorithms, but the easiest to the sorting algorithms, but the easiest to program and understand.program and understand.
When sorting the contents of an array, the When sorting the contents of an array, the bubble sort makes repeated “passes” through bubble sort makes repeated “passes” through the array, and in each “pass” we compare the array, and in each “pass” we compare adjacent array elements. If the elements are adjacent array elements. If the elements are out of order we swap them, and at the end of out of order we swap them, and at the end of each “pass” one element is moved into it’s each “pass” one element is moved into it’s correct place in the array.. correct place in the array..
public static void bubble(int[] anArray){public static void bubble(int[] anArray){ int temp;int temp;
for (int i=0, i < anArray.length-1; i++) {for (int i=0, i < anArray.length-1; i++) {for( int j=0; j < anArray.length-1; i++) {for( int j=0; j < anArray.length-1; i++) {
if (anArray[j] > anArray[j+1]) {if (anArray[j] > anArray[j+1]) {temp = anArray[j];temp = anArray[j];anArray[j] = anArray[j+1];anArray[j] = anArray[j+1];anArray[j+1] = temp;anArray[j+1] = temp;
}}}}
}}}}
In the worst case with this algorithm, In the worst case with this algorithm, we perform n-1 comparisons, n-1 we perform n-1 comparisons, n-1 timestimes
(n-1)(n-1) = n(n-1)(n-1) = n22 – 2n +1 – 2n +1
This makes the algorithm O(nThis makes the algorithm O(n22) - ) - which represents the number of which represents the number of operations performed.operations performed.
Selection SortSelection Sort•In the selection sort, the smallest value in the array is located and moved to it’s correct location (element 0). Then, the next smallest value is located and move to element 1, and so forth
2525 55 1717 1010 88 1111 2020 1414 2323 33
00 11 22 33 44 55 66 77 88 99
25
5
17
10 811
20
14
23
3
0 1 2 3 4 5 6 7 8 9
3 5
17
10 811
20
14
23 25
0 1 2 3 4 5 6 7 8 9
Unsorted Array
Pass 1, 3 is the smallest number and is moved to its correct position
3 5
17
10 811
20
14
23 25
0 1 2 3 4 5 6 7 8 9
In Pass 2, 5 is the smallest value, it is already in position one, but this time the scan begins at the second element
3 58 10
17
11
20
14
23 25
0 1 2 3 4 5 6 7 8 9
In Pass 3, 8 is moved into place
3 58 10
17
11
20
14
23 25
0 1 2 3 4 5 6 7 8 9
In Pass 4, 10 is the lowest value, and is already in place
3 58 10 11
1720
14
2523
0 1 2 3 4 5 6 7 8 9
Pass 5, 11 is moved
3 58 10 11
14
2017
2523
0 1 2 3 4 5 6 7 8 9
3 58 10 11
1417
202523
0 1 2 3 4 5 6 7 8 9
Pass 6, 14 is positioned correctly
Pass 7, 17 is positioned correctly, and although the algorithm continues for 3 more passes, no more changes are made
The AlgorithmThe Algorithm
For I = 0 to # of elements -1 For I = 0 to # of elements -1 Set minIndex Variable to the first element of the Set minIndex Variable to the first element of the
unsorted portion of the arrayunsorted portion of the array Set minValue to the value stored in the element Set minValue to the value stored in the element
pointed to by minIndex.pointed to by minIndex. Search the unsorted portion of the arraySearch the unsorted portion of the array
For j = I +1 to # of elements -1 For j = I +1 to # of elements -1 If element[j] < minValueIf element[j] < minValue
Store j in minIndexStore j in minIndex Store element[j] in minValueStore element[j] in minValue
Swap minValue at location minIndex with element[i]Swap minValue at location minIndex with element[i]
Insertion SortInsertion Sort
This sort takes each element of the This sort takes each element of the array and tries to put it into its correct array and tries to put it into its correct location, shifting other elements if location, shifting other elements if necessary.necessary.
The algorithm begins with the first two The algorithm begins with the first two elements which are swapped, if elements which are swapped, if necessary, the third element is taken necessary, the third element is taken and put into its correct place, and so and put into its correct place, and so forth.forth.
33
10 8
15
2519
29
4
13
23
0 1 2 3 4 5 6 7 8 9
10
33
8
15
2519
29
4
13
23
0 1 2 3 4 5 6 7 8 9
The third element needs to be inserted in element 0, so the values 10 & 33 must be moved to the “right”
8 10
33
15
2519
29
4
13
23
0 1 2 3 4 5 6 7 8 9
The 4th element needs to be inserted in between 10 and 33
8 1015
33
2519
29
4
13
23
0 1 2 3 4 5 6 7 8 9
The 5th element needs to be inserted between 15 & 33
8 1015
25
33
19
29
4
13
23
0 1 2 3 4 5 6 7 8 9
The 5th element needs to go in between 15 and 25
8 1015
1925
3329
4
13
23
0 1 2 3 4 5 6 7 8 9
The 7th element goes between 25 and 33
8 1015
1925
2933
4
13
23
0 1 2 3 4 5 6 7 8 9
The 8th element needs to go in the first location and all other need to be shifted to the right
48 10
1519
2529
33
13
23
0 1 2 3 4 5 6 7 8 9
The 9th element needs to move to between 19 and 25
48 10
1519
23 2529
13
33
0 1 2 3 4 5 6 7 8 9
The last element needs to be placed between 10 and 15
48 10
13 1519
23 25
3329
0 1 2 3 4 5 6 7 8 9
The array is sorted
The AlgorithmThe Algorithm For i = 1 to length-1 (all subscripts)For i = 1 to length-1 (all subscripts)
Save the i into tempSave the i into temp Save the value in array[temp] into Save the value in array[temp] into
unsortedValueunsortedValue While temp >0 and array[temp-1]> While temp >0 and array[temp-1]>
unsortedValueunsortedValue COPY array[temp-1] to array[temp]COPY array[temp-1] to array[temp] Temp = temp -1Temp = temp -1
Store unsortedValue into array[temp]Store unsortedValue into array[temp]