180 likes | 276 Views
23. 78. 45. 8. 32. 56. Original List. 8. 78. 45. 23. 32. 56. After Pass One. Selection Sort. Given an array[0-N], place the smallest item in the array in position 0, the second smallest in position 1,
E N D
23 78 45 8 32 56 Original List 8 78 45 23 32 56 After Pass One 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]. unsorted unsorted
8 23 45 78 32 56 8 23 32 78 45 56 Selection Sort After Pass Two unsorted After Pass Three unsorted
8 23 32 45 78 56 8 23 32 45 56 78 Selection Sort After Pass Four unsorted After Pass Five unsorted
Selection Sort 12 12 25 57 48 37 92 86 33 25 57 48 37 12 92 86 33 25 57 48 37 12 92 86 33 12 57 48 37 25 92 86 33 37 25 48 48 37 33 57 57 48 37 48 25 37 Initial array 1st 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 1 3 5 1 2 3 4 5 6 7 8 2 4 6 7 8 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.
1 2 3 4 5 14 16 17 6 7 9 13 23 25 29 8 11 19 26 27 1 3 5 14 17 6 7 13 8 19 Merging Unordered FIles !st Merge Run 2 4 16 9 23 25 29 11 26 27 Stepdown 9 < 16 Three Merge Runs 2nd Merge Run Rollout 3rd Merge Run File 1 Note this is not completely sorted. We need to merge the runs again File 2 File 3
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.
1 12 6 10 7 2 3 9 MergeSort: Example 1 12 6 10 7 2 3 9 14 15 8 11 4 16 13 5 1 12 6 10 7 2 3 9 14 15 8 11 4 16 13 5 1 12 6 10 7 2 3 9 1 12 6 10 7 2 3 9
1 12 6 10 7 2 3 9 MergeSort: Example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 6 7 9 10 12 4 5 8 11 13 14 15 16 1 6 10 12 2 3 7 9 1 12 6 10 7 2 3 9
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.