Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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]. 23784532856 Original List87845322356 After Pass One unsorted

2 Selection Sort 82345327856 After Pass Two unsorted 82332457856 After Pass Three unsorted

3 Selection Sort 82332784556 After Pass Four unsorted 82332564578 After Pass Five unsorted

4 Selection Sort 25 57 48 37 12 92 86 33 25 57 48 37 12 92 86 33 12 25 12 57 48 37 25 92 86 33 12 25 57 48 37 92 86 33 483733 57 48 37 48 3725 57 48 37 Initial array1st pass 2nd pass 3rd pass

5 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]); }

6 MergeSort: Merging Ordered Files 135135 1234567812345678 2467824678 File1 File 2 File 3

7 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.

8 Merging Unordered FIles 1 3 5 14 17 6 7 13 8 19 2 4 16 9 23 25 29 11 26 27 1 2 3 4 5 14 16 17 6 7 9 13 23 25 29 8 11 19 26 27 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

9 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

10 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

11 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.

12 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.

13 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.

14 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.

15 MergeSort: Example 1 12 6 10 7 2 3 9 14 15 8 11 4 16 13 5 1 12 6 107 2 3 9 1 126 10 1 12 6 10 7 2 3 9 14 15 8 11 4 16 13 5 7 23 9 1211067329

16 MergeSort: Example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 6 10 12 2 3 7 9 1 126 10 1 2 3 6 7 9 10 12 4 5 8 11 13 14 15 16 7 23 9 1211067329

17 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

18 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.


Download ppt "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."

Similar presentations


Ads by Google