Download presentation
Presentation is loading. Please wait.
Published byKevin Morgan Modified over 8 years ago
1
METU EEE EE 441 Ece GURAN SCHMIDT Selection sort algorithm template void Swap (T &x, T &y) { T temp; temp = x; x = y; y = temp; } // sort an array of n elements of type T using the // selection sort algorithm template void SelectionSort(T A[], int n) { // index of smallest item in each pass int smallIndex; int i, j; // sort A[0]..A[n-2], and A[n-1] is in place for (i = 0; i < n-1; i++) { // start the scan at index i; set smallIndex to i smallIndex = i; // j scans the sublist A[i+1]..A[n- 1] for (j = i+1; j < n; j++) // update smallIndex if smaller element is found if (A[j] < A[smallIndex]) smallIndex = j; // when finished, place smallest item in A[i] Swap(A[i], A[smallIndex]); }
2
METU EEE EE 441 Ece GURAN SCHMIDT Insertion sort algorithm // insertion sort orders sublists A[0]... A[i], 1 <= i <= n-1. // for each i, insert A[i] in the correct position A[j] template void InsertionSort(T A[], int n) { int i, j; T temp; // i identifies the sublist A[0] to A[i]. for (i = 1; i < n; i++) { // index j scans down list from A[i] looking for // correct position to locate target. assigns it to A[j] j = i; temp = A[i]; // locate insertion point by scanning downward as long // as temp < A[j-1] and we have not encountered the // beginning of the list while (j > 0 && temp < A[j-1]) { // shift elements up list to make room for insertion A[j] = A[j-1]; j--; } // the location is found; insert the temp A[j] = temp; }
3
METU EEE EE 441 Ece GURAN SCHMIDT BubbleSort Algorithm // BubbleSort is passed an array A and list count n. it // sorts the data by making a series of passes as long as // lastExchangeIndex > 0. template void BubbleSort(T A[], int n) { int i,j; // Index of last exchange int lastExchangeIndex; // i is the index of last element in the sublist i = n-1; // continue the process until no exchanges are made while (i > 0) { // start lastExchangeIndex at 0 lastExchangeIndex = 0; // scan the sublist A[0] to A[i] for (j = 0; j < i; j++) // exchange a pair and update lastExchangeIndex if (A[j+1] < A[j]) { Swap(A[j],A[j+1]); lastExchangeIndex = j; } // set i to index of the last exchange. continue sorting // the sublist A[0] to A[i] i = lastExchangeIndex; }
4
METU EEE EE 441 Ece GURAN SCHMIDT Quick sort algorithm Partition a list A[low] to A[high] about a pivot at the middle of the list mid = (low + high)/2; pivot = A[mid]; Exchange pivot with A[low] set Scanup=low+1 ScanDown=high: // exchange the pivot and the low end of the range // and initialize the indices scanUp and scanDown. Swap(A[mid], A[low]); scanUp = low + 1; scanDown = high; Scanup: // move up lower sublist; stop when scanUp enters // upper sublist or identifies an element > pivot while (scanUp <= scanDown && A[scanUp] <= pivot) scanUp++; After ScanUp is positioned ScanDown moves down the list: / scan down upper sublist; stop when scanDown locates // an element <= pivot; we guarantee we stop at A[low] while (pivot < A[scanDown]) scanDown--; Conclusion of this loop: if ScanUp<ScanDown the vertices identify two elements that are in the wrong sublist exchange the values / if indices are still in their sublists, then they // identify two elements in wrong sublists. exchange if (scanUp < scanDown) Swap(A[scanUp], A[scanDown]);
5
METU EEE EE 441 Ece GURAN SCHMIDT Quick sort algorithm At this point, scanDown identifies the top of the left sublist that contains elements less than or equal to the pivot. Index of ScanDown is the pivot location Retrieve the pivot from A[low] and swap: // copy pivot to index (scanDown) that partitions sublists A[low] = A[scanDown]; A[scanDown] = pivot; Use recursion to process the sublists: locate the pivot to split the list Recursive part Call with parameters: low to mid-1 and mid+1 to high Stopping condition: when a sublist has 0 or 1 element (an ordered list)
6
METU EEE EE 441 Ece GURAN SCHMIDT Comparison of algorithms n40008000100001500020000 exchange sort12.2349.9577.47173.97313.33 selection sort17.329.4346.02103185.05 bubble sort15.7864.0399.1223.28399.47 insertion sort5.6723.1535.4380.23143.67 quick sort0.070.170.220.330.47
7
METU EEE EE 441 Ece GURAN SCHMIDT Comparison of algorithms AverageWorst Bubble sortO(n 2 ) Insertion sortO(n 2 ) Quick sortO(nlogn)O(n 2 ) Heap sortO(nlogn) Merge sortO(nlogn)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.