Selection Sort Given an array[0-N], place the smallest item in the array in position 0, the second smallest in position 1, and so forth. We do thisby comparing A[0] with A[1] and swapping if necessary. We continue in this manner until the smallest item appears in A[0]. We then forget about A[0], and repeat the process for A[1] Original List After Pass One unsorted
Selection Sort After Pass Two unsorted After Pass Three unsorted
Selection Sort After Pass Four unsorted After Pass Five unsorted
Selection Sort Initial array1st pass 2nd pass 3rd pass
Selection Sort void SelectionSort (int data[], int arraySize) { int i, j; for (i = 0; i < (arraySize - 1); i++) { for (j = i + 1; j < arraySize; j++) { if (data[j] < data[i]) swap (&data[j], &data[i]); }
MergeSort: Merging Ordered Files File1 File 2 File 3
Merging Unordered FIles Merge Run - A series of consecutively ordered data in a file Stepdown - Occurs when the sequential ordering of a file is broken. Rollout - The process of copying the consecutive series of records to the merge output file after a stepdown.
Merging Unordered FIles Three Merge Runs !st Merge Run 2nd Merge Run 3rd Merge Run Stepdown 9 < 16 Rollout File 1 File 2 File 3 Note this is not completely sorted. We need to merge the runs again
The Merge Process Assume we have 2300 records to sort – We can only put 500 records at a time in main memory We read and sort the first 500 records, then write them to an output file. Repeat this process with the remaining chunks of data until all are processed. We now need a process to merge all the chunks to gether for resorting
Natural Merge Sorts a constant number of input merge files to one merge output file. A distribution phase is required to redistribute the merge runs to the input file for remerging All merge runs are written to the same file
Balanced Merge Uses a constant number of input merge files and the same number of output files Generally, there are no more than four files. Eliminates the distribution phase. A two-way merge requires four files –Merges first merge run on file 1 with first merge run on file2 and writes it to file3 –Merges second run on file1with second merge run on file 2 and writes it to file 4 –Repeat for third merge run. Rollout any remaining data asnecessay.
Polyphase Merge Most complex type of merging A constant number of input files are merged to one output file. As the data in each input file is completely merged, it immediately becomes the output file and what was the output file becomes the input file.
Polyphase Merge Merges first merge run on file 1with first merge run on file 2and writes it to file 3. Merges second merge run on file 1 with second merge run on file 2 and writes it to file 3. Assume now that file two is empty. We can now say the first merge phase is complete. Close merge file 2 and reopen it as output Close merge file 3 and open it as input. Repeat as needed.
MergeSort MergeSort is a recursive sorting procedure that uses O(nlogn) comparisons in the worst case To sort an array ofn elements, we perform the following three steps in sequence: If n < 2 then the array is already sorted. Stop. Otherwise, n > 1, do the following: – 1. Sort the left half of the array – 2. Sort the right half of the array – 3. Merge the sorted left and right halves.
MergeSort: Example
MergeSort: Example
Merge Sort To sort the left half of the array, the program calls itself recursively, passing the left half of array down to a new activation of MergeSort. This happens repeatedly until we have an array of only one element. One-element arrays are already sorted. When the left half of an array is sorted, we sort the right half. Once the two halves of an array have been sorted by recursive calls, MergeSort merges the two sorted halves into a sorted whole. This is done by examining and comparing the smallest remaining number in each of the sorted halves. When one of the two subarrays becomes empty, the remaining elements in the other subarray are copied in order into parent array. No comparison need to be made
MergeSort and QuickSort Both mergesort and quicksort are methods that involve splitting the file into two parts, sorting the two parts separately, and then joining the two sorted halves together. In mergesort, the splitting istrivial and the poining is hard. In quicksort, the splitting is hard and joining is trivial. Insertion sort may be considered a special caseofmergesort in which the two halves consist of a single element andthe remainder of the array.