1.06k likes | 1.07k Views
إن الله لا يغير ما بقوم حتى يغيروا ما بأنفسهم. إذا لم تعمل على تحقيق حلمك ... فإن شخصا ما سيقوم بتوظيفك لتعمل على تحقيق حلمه هو !!. CS214 – Data Structures Lecture 6&7: Quadratic Sorting. Slides by Mohamed El-Ramly, PhD Basheer Youssef ,PhD.
E N D
إن الله لا يغير ما بقوم حتى يغيروا ما بأنفسهم
إذا لم تعمل على تحقيق حلمك... فإن شخصا ما سيقوم بتوظيفك لتعمل على تحقيق حلمه هو !!
CS214 – Data StructuresLecture 6&7: Quadratic Sorting Slides by Mohamed El-Ramly, PhD Basheer Youssef ,PhD
Agenda 0 Why Sorting? Quadratic Sorting algorithms Selection Sort Insertion Sort Bubble Sort Included but read yourself Theoretical Boundaries of Sorting Problem STL support for sorting Sub-quadratic Sorting Merge Sort I Shell Sort Quick Sort Heap Sort
Divide and Conquer • Base Case, solve the problem directly if it is small enough • Divide the problem into two or more similar and smaller subproblems • Recursively solve the subproblems • Combine solutions to the subproblems Chapter 9: Sorting
Quadratic Sorting Algorithms • Take long time • Are O(n2), which is quite slow • Theoritical limits show that we can do better. • It is possible to achieve O(n log(n)), at least theoritically. Chapter 9: Sorting
Shell Sort: A Better Insertion Sort • Shell sort is a type of insertion sort but with O(n^(3/2)) or better performance • Named after its discoverer, Donald Shell • the first sub quadratic sorting algorithm • Divide and conquer approach to insertion sort • Instead of sorting the entire array, sort many smaller subarrays using insertion sort before sorting the entire array Chapter 9: Sorting
Shell Sort • A variation of the insertion sort • But faster than O(n2) • Done by sorting subarrays of equally spaced indices • Instead of moving to an adjacent location an element moves several locations away • Results in an almost sorted array • This array sorted efficiently with ordinary insertion sort • Wanted to stop moving data small distances (in the case of insertion sort and bubble sort) and stop making swaps that are not helpful (in the case of selection sort) • Start with sub arrays created by looking at data that is far apart and then reduce the gap size Chapter 9: Sorting
Shell Sort Donald Shell suggested that the initial separation between indices be n/2 and halve this value at each pass until it is 1. An array has 13 elements, and the subarrays formed by grouping elements whose indices are 6 apart. Chapter 9: Sorting
Shell Sort The subarrays after they are sorted, and the array that contains them. Chapter 9: Sorting
Shell Sort The subarrays by grouping elements whose indices are 3 apart Chapter 9: Sorting
Shell Sort The subarrays after they are sorted, and the array that contains them. Chapter 9: Sorting
Efficiency of Shell Sort • Efficiency is O(n2) for worst case • If n is a power of 2 • Average-case behavior is O(n1.5) • Shell sort uses insertion sort repeatedly. • Initial sorts are much smaller, the later sorts are on arrays that are partially sorted, the final sort is on an array that is almost entirely sorted. Chapter 9: Sorting
ShellSort in practice 46 2 83 41 102 5 17 31 64 49 18 Gap of five. Sort sub array with 46, 5, and 1852 83 41 102 1817 31 64 49 46 Gap still five. Sort sub array with 2 and 175 283 41 102 18 1731 64 49 46 Gap still five. Sort sub array with 83 and 315 2 3141 102 18 17 8364 49 46 Gap still five Sort sub array with 41 and 645 2 31 4110218 17 83 644946 Gap still five. Sort sub array with 102 and 495 2 31 41 49 18 17 83 64 102 46 Continued on next slide: Chapter 9: Sorting
Completed Shellsort 5 2 31 41 49 18 17 83 64 102 46 Gap now 2: Sort sub array with 5 31 49 17 64 465 2 17 41 31 18 46 83 49 102 64 Gap still 2: Sort sub array with 2 41 18 83 1025 2 17 18 31 41 46 83 49 102 64 Gap of 1 (Insertion sort) 2 5 17 18 31 41 46 49 64 83 102 Array sorted Chapter 9: Sorting
Pseudocode of ShellSort Chapter 9: Sorting
Java ShellSort Code public static void shellsort(Comparable[] list){ Comparable temp; boolean swap; for(int gap = list.length / 2; gap > 0; gap /= 2) for(int i = gap; i < list.length; i++) { Comparable tmp = list[i]; int j = i; for( ; j >= gap && tmp.compareTo( list[j - gap] ) < 0; j -= gap ) list[ j ] = list[ j - gap ]; list[ j ] = tmp; }} // See C++ version in Drozdek’s Book Chapter 9: Sorting
Merge Sort • A merge is a common data processing operation that is performed on two sequences of data with the following characteristics • Both sequences contain items with a common compareTo method • The objects in both sequences are ordered in accordance with this compareTo method Chapter 9: Sorting
Merge Algorithm • Merge Algorithm • Access the first item from both sequences • While not finished with either sequence • Compare the current items from the two sequences, copy the smaller current item to the output sequence, and access the next item from the input sequence whose item was copied • Copy any remaining items from the first sequence to the output sequence • Copy any remaining items from the second sequence to the output sequence Chapter 9: Sorting
Merge Merge Sort: Idea Divide into two halves A FirstPart SecondPart Recursively sort SecondPart FirstPart A is sorted! Chapter 9: Sorting
Merge Sort: Algorithm • Merge-Sort(A, left, right) • if left ≥ right return • else • middle ←(left+right)/2 • Merge-Sort(A, left, middle) • Merge-Sort(A, middle+1, right) • Merge(A, left, middle, right) Recursive Call Chapter 9: Sorting
Merge-Sort: Merge Sorted A: merge Sorted SecondPart SortedFirstPart A: A[right] A[left] A[middle] Chapter 9: Sorting
5 15 28 30 6 10 14 5 2 3 7 8 1 4 5 6 R: L: 3 5 15 28 6 10 14 22 Temporary Arrays Merge-Sort: Merge Example A: Chapter 9: Sorting
Merge-Sort: Merge Example A: 3 1 5 15 28 30 6 10 14 k=0 R: L: 3 2 15 3 28 7 30 8 1 6 10 4 14 5 22 6 i=0 j=0 Chapter 9: Sorting
Merge-Sort: Merge Example A: 2 1 5 15 28 30 6 10 14 k=1 R: L: 2 3 3 5 15 7 8 28 1 6 10 4 14 5 6 22 i=0 j=1 Chapter 9: Sorting
Merge-Sort: Merge Example A: 3 1 2 15 28 30 6 10 14 k=2 R: L: 2 3 7 8 6 1 4 10 14 5 22 6 i=1 j=1 Chapter 9: Sorting
Merge-Sort: Merge Example A: 1 2 3 6 10 14 4 k=3 R: L: 2 3 7 8 1 6 10 4 5 14 22 6 j=1 i=2 Chapter 9: Sorting
Merge-Sort: Merge Example A: 1 2 3 4 6 10 14 5 k=4 R: L: 2 3 7 8 6 1 10 4 5 14 22 6 i=2 j=2 Chapter 9: Sorting
Merge-Sort: Merge Example A: 6 1 2 3 4 5 6 10 14 k=5 R: L: 2 3 7 8 1 6 10 4 5 14 22 6 i=2 j=3 Chapter 9: Sorting
Merge-Sort: Merge Example A: 7 1 2 3 4 5 6 14 k=6 R: L: 2 3 7 8 1 6 4 10 5 14 6 22 i=2 j=4 Chapter 9: Sorting
Merge-Sort: Merge Example A: 8 1 2 3 4 5 6 7 14 k=7 R: L: 3 2 3 5 15 7 28 8 1 6 10 4 14 5 6 22 i=3 j=4 Chapter 9: Sorting
Merge-Sort: Merge Example A: 1 2 3 4 5 6 7 8 k=8 R: L: 2 3 3 5 15 7 8 28 6 1 10 4 14 5 6 22 j=4 i=4 Chapter 9: Sorting
Merge(A, left, middle, right) • n1 ← middle – left + 1 • n2 ← right – middle • create array L[n1], R[n2] • for i ← 0 to n1-1 do L[i] ← A[left +i] • for j ← 0 to n2-1 do R[j] ← A[middle+j] • k ← i ← j ← 0 • while i < n1 & j < n2 • if L[i] < R[j] • A[k++] ← L[i++] • else • A[k++] ← R[j++] • while i < n1 • A[k++] ← L[i++] • while j < n2 • A[k++] ← R[j++] n = n1+n2 Space: n Time : cn for some constant c Chapter 9: Sorting
Merge-Sort(A, 0, 7) Divide A: 3 7 5 1 6 2 8 4 6 2 8 4 3 7 5 1 Chapter 9: Sorting
Merge-Sort(A, 0, 7) , divide Merge-Sort(A, 0, 3) A: 3 7 5 1 8 4 6 2 6 2 8 4 Chapter 9: Sorting
Merge-Sort(A, 0, 7) , divide Merge-Sort(A, 0, 1) A: 3 7 5 1 8 4 2 6 6 2 Chapter 9: Sorting
Merge-Sort(A, 0, 7) , base case Merge-Sort(A, 0, 0) A: 3 7 5 1 8 4 2 6 Chapter 9: Sorting
Merge-Sort(A, 0, 7) Merge-Sort(A, 0, 0), return A: 3 7 5 1 8 4 2 6 Chapter 9: Sorting
Merge-Sort(A, 0, 7) , base case Merge-Sort(A, 1, 1) A: 3 7 5 1 8 4 6 2 Chapter 9: Sorting
Merge-Sort(A, 0, 7) Merge-Sort(A, 1, 1), return A: 3 7 5 1 8 4 2 6 Chapter 9: Sorting
Merge-Sort(A, 0, 7) Merge(A, 0, 0, 1) A: 3 7 5 1 8 4 2 6 Chapter 9: Sorting
Merge-Sort(A, 0, 7) Merge-Sort(A, 0, 1), return A: 3 7 5 1 8 4 2 6 Chapter 9: Sorting
Merge-Sort(A, 0, 7) , divide Merge-Sort(A, 2, 3) A: 3 7 5 1 2 6 4 8 8 4 Chapter 9: Sorting
Merge-Sort(A, 0, 7) Merge-Sort(A, 2, 2), base case A: 3 7 5 1 2 6 4 8 Chapter 9: Sorting
Merge-Sort(A, 0, 7) Merge-Sort(A, 2, 2),return A: 3 7 5 1 2 6 4 8 Chapter 9: Sorting
Merge-Sort(A, 0, 7) Merge-Sort(A, 3, 3), base case A: 2 6 8 4 Chapter 9: Sorting