Advanced Sorting Techniques Merge Sort A merge sort assumes you have two already sorted arrays that you want to merge together. The two arrays are merged into a third that is equal in size to the sum of the first two, If you have two arrays to merge, A and B, and the third to merge into C: 1. Set the current position of all arrays to the first element. 2. Compare the current elements of two arrays to merge and move the smaller value into the current position at C. 3. Advance the current position of C and the array with the smaller value. Iterative Process: Repeat steps 2 and 3 until one array is exhausted. Once an array is exhausted, simply copy the remaining elements of the other array into the new one. void Merge(string A[], int F, int Mid, int L) { int First1 = F; int Last1 = Mid; int First2 = Mid + 1; int Last2 = L; int index = First1; for (; (First1<=Last1) && (First2<=Last2); index++) { if ( strcmp(A[First1], A[First2]) < 0) { strcpy(tmpArray[index], A[First1]); First1++; } else { strcpy(tmpArray[index], A[First2]); First2++; } } for (; First1<=Last1; First1++, index++) { strcpy(tmpArray[index], A[First1]); } for (; First2<=Last2; First2++, index++) { strcpy(tmpArray[index], A[First2]); } for ( index=F; index<=L; index++ ) { strcpy(A[index], tmpArray[index]); } intMerge++; } How can we apply this to one unsorted array? We recursively the array in two by finding the mid-point. When the size is one, we have a sorted array to merge. void MergeSort(string A[], int F, int L) { intSort++; if ( F < L ) { int Mid = (F + L) / 2; MergeSort(A, F, Mid); MergeSort(A, Mid+1, L); Merge(A, F, Mid, L); } } Quick Sort This sort partitions an array as follows: 1. Picking a pivot value (one of the end values) 2. Move all smaller values to the left of the pivot 3. Move all larger values to the right of the pivot Recursive Process: Repeat steps with lower partition Repeat steps with upper partition void Partition(string A[], int F, int L, int& iPivot) { ChoosePivot(A, F, L); string sPivot = ""; strcpy(sPivot, A[F]); int LastS1 = F; int FirstUnknown = F + 1; for (; FirstUnknown<=L; FirstUnknown++) { if ( strcmp(A[FirstUnknown], sPivot) < 0 ) { LastS1++; Swap(A[FirstUnknown], A[LastS1]); } } Swap(A[F], A[LastS1]); iPivot = LastS1; partCount++; } void QuickSort(string A[], int F, int L) { int Pivot; if ( F 0 ) Swap(A[F], A[M]); if ( strcmp(A[M], A[L]) > 0 ) Swap(A[M], A[L]); } Radix Sort A radix sort operates by grouping the data. 1. Separate each string into a bin based on it's last character 2. Pass through each string in all bins in order, comparing the next character in from the end. Place the string in a bin based on that character. 3. Repeat until you compared every position in the string 4. Pull the strings from the bins in order. For simplicity sake, our example will require all strings to be the same length and limit the strings to digits. // Declare variables string binA[10][10]; string binB[10][10]; int posA[10]; int posB[10]; int binPos = 0; // Read the file into first column of binA ifstream inFile string input = ""; while ( inFile ) { inFile.getLine(binA[count][posA[count]++]); count++; } // Repeatedly re-group the elements for ( int i=3; i>=0; i-- ) { // Group from binA to binB for ( int x=0; x<10; x++ ) { for ( int y=0; y