Download presentation
Presentation is loading. Please wait.
Published byMiranda Poole Modified over 9 years ago
1
Sorting
2
Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array or list in either ascending or descending order. We sort values for reporting purposes and to make searching more efficient We sort values for reporting purposes and to make searching more efficient To date we have only covered the bubble sort algorithm, but there are other algorithms, some more effecient To date we have only covered the bubble sort algorithm, but there are other algorithms, some more effecient
3
Analysis of the bubble sort The bubble sort is one of the least efficient of the sorting algorithms, but the easiest to program and understand. The bubble sort is one of the least efficient of the sorting algorithms, but the easiest to program and understand. When sorting the contents of an array, the bubble sort makes repeated “passes” through the array, and in each “pass” we compare adjacent array elements. If the elements are out of order we swap them, and at the end of each “pass” one element is moved into it’s correct place in the array.. When sorting the contents of an array, the bubble sort makes repeated “passes” through the array, and in each “pass” we compare adjacent array elements. If the elements are out of order we swap them, and at the end of each “pass” one element is moved into it’s correct place in the array..
4
public static void bubble(int[] anArray){ int temp; int temp; for (int i=0, i < anArray.length-1; i++) { for( int j=0; j < anArray.length-1; i++) { if (anArray[j] > anArray[j+1]) { temp = anArray[j]; anArray[j] = anArray[j+1]; anArray[j+1] = temp; }}}}
5
In the worst case with this algorithm, we perform n-1 comparisons, n-1 times In the worst case with this algorithm, we perform n-1 comparisons, n-1 times (n-1)(n-1) = n 2 – 2n +1 This makes the algorithm O(n 2 ) - which represents the number of operations performed.
6
Selection 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 25517108112014233 0123456789
7
Unsorted Array Pass 1, 3 is the smallest number and is moved to its correct position
8
In Pass 2, 5 is the smallest value, it is already in position one, but this time the scan begins at the second element In Pass 3, 8 is moved into place
9
In Pass 4, 10 is the lowest value, and is already in place Pass 5, 11 is moved
10
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
11
The Algorithm For I = 0 to # of elements -1 For I = 0 to # of elements -1 Set minIndex Variable to the first element of the unsorted portion of the array Set minIndex Variable to the first element of the unsorted portion of the array Set minValue to the value stored in the element pointed to by minIndex. Set minValue to the value stored in the element pointed to by minIndex. Search the unsorted portion of the array Search 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] < minValue If element[j] < minValue Store j in minIndex Store j in minIndex Store element[j] in minValue Store element[j] in minValue Swap minValue at location minIndex with element[i] Swap minValue at location minIndex with element[i]
12
Insertion Sort This sort takes each element of the array and tries to put it into its correct location, shifting other elements if necessary. This sort takes each element of the array and tries to put it into its correct location, shifting other elements if necessary. The algorithm begins with the first two elements which are swapped, if necessary, the third element is taken and put into its correct place, and so forth. The algorithm begins with the first two elements which are swapped, if necessary, the third element is taken and put into its correct place, and so forth.
14
The third element needs to be inserted in element 0, so the values 10 & 33 must be moved to the “right”
15
The 4 th element needs to be inserted in between 10 and 33
16
The 5 th element needs to be inserted between 15 & 33
17
The 5 th element needs to go in between 15 and 25
18
The 7 th element goes between 25 and 33
19
The 8 th element needs to go in the first location and all other need to be shifted to the right
20
The 9 th element needs to move to between 19 and 25
21
The last element needs to be placed between 10 and 15
22
The array is sorted
23
The Algorithm For i = 1 to length-1 (all subscripts) For i = 1 to length-1 (all subscripts) Save the i into temp Save the i into temp Save the value in array[temp] into unsortedValue Save the value in array[temp] into unsortedValue While temp >0 and array[temp-1]> unsortedValue While temp >0 and array[temp-1]> unsortedValue COPY array[temp-1] to array[temp] COPY array[temp-1] to array[temp] Temp = temp -1 Temp = temp -1 Store unsortedValue into array[temp] Store unsortedValue into array[temp]
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.