Data Structure and Algorithms [CO2003] Chapter 10 - Sort Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Faculty of Computer Science and Engineering Hochiminh city University of Technology Contents 1. Divide-and-Conquer Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 1 / 57 Outcomes • L.1 - Depict the working steps of sorting algorithms step-by-steps.2 - Describe sorting algorithms by using pseudocode.3 - Implement sorting algorithms using C/C++ .4 - Analyze the complexity and develop experiment (program) to evaluate sorting algorithms.5 - Use sorting algorithms for problems in real-life.4 - Develop recursive implementations for methods supplied for the following structures: list, tree, heap, searching, and graphs.2 - Analyze algorithms and use Big-O notation to characterize the computational complexity of algorithms composed by using the following control structures: sequence, branching, and iteration (not recursion).
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 2 / 57 Sorting concepts Sorting One of the most important concepts and common applications in computing. Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 3 / 57 Sorting Sort stability: data with equal keys maintain their relative input order in the output.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 4 / 57 Sorting Sort efficiency: a measure of the relative efficiency of a sort = number of comparisons + number of moves. Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 5 / 57 Sorting Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 6 / 57 Insertion Sort Straight Insertion Sort • The list is divided into two parts: sorted and unsorted. • In each pass, the first element of the unsorted sublist is inserted into the sorted sublist. Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 7 / 57 Straight Insertion Sort Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 8 / 57 Straight Insertion Sort Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 9 / 57 Straight Insertion Sort Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 10 / 57 Straight Insertion Sort Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 11 / 57 Straight Insertion Sort Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 12 / 57 Straight Insertion Sort Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 13 / 57 Straight Insertion Sort Algorithm InsertionSort() Sorts the contiguous list using straight insertion sort. if count > 1 then current = 1 while current < count do temp = data[current] walker = current - 1 while walker >= 0 AND temp.key do data[walker+1] = data[walker] walker = walker - 1 end data[walker+1] = temp current = current + 1 end end Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 14 / 57 Shell Sort • Named after its creator Donald L.
• Given a list of N elements, the list is divided into K segments (K is called the increment). • Each segment contains N/K or more elements. • Segments are dispersed throughout the list. • Also is called diminishing-increment sort.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 15 / 57 Shell Sort Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 16 / 57 Shell Sort • For the value of K in each iteration, sort the K segments. • After each iteration, K is reduced until it is 1 in the final iteration.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 17 / 57 Example of Shell Sort Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 18 / 57 Example of Shell Sort Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 19 / 57 Choosing incremental values • From more of the comparisons, it is better when we can receive more new information.
• Incremental values should not be multiples of each other, other wise, the same keys compared on one pass would be compared again at the next. • The final incremental value must be 1. Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 20 / 57 Choosing incremental values • Incremental values may be: 1, 4, 13, 40, 121,.
kt = 1 ki−1 = 3 ∗ ki + 1 t = | log3 n| − 1 • or: 1, 3, 7, 15, 31,. kt = 1 ki−1 = 2 ∗ ki + 1 t = | log2 n| − 1 Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 21 / 57 Shell Sort Algorithm ShellSort() Sorts the contiguous list using Shell sort. k = first_incremental_value while k >= 1 do segment = 1 while segment <= k do SortSegment(segment) segment = segment + 1 end k = next_incremental_value end End ShellSort Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 22 / 57 Shell Sort Algorithm SortSegment(val segment <int>, val k <int>) Sorts the segment beginning at segment using insertion sort, step between elements in the segment is k. current = segment + k while current < count do temp = data[current] walker = current - k while walker >=0 AND temp.key do data[walker + k] = data[walker] walker = walker - k end data[walker + k] = temp current = current + k end End SortSegment Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 23 / 57 Insertion Sort Efficiency • Straight insertion sort: f (n) = n(n + 1)/2 = O(n2 ) • Shell sort: O(n1.25 ) (Empirical study) Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 24 / 57 Selection Sort Selection Sort In each pass, the smallest/largest item is selected and placed in a sorted list.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 25 / 57 Straight Selection Sort • The list is divided into two parts: sorted and unsorted. • In each pass, in the unsorted sublist, the smallest element is selected and exchanged with the first element. Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 26 / 57 Straight Selection Sort Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 27 / 57 Straight Selection Sort Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 28 / 57 Straight Selection Sort Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 29 / 57 Straight Selection Sort Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 30 / 57 Straight Selection Sort Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 31 / 57 Straight Selection Sort Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 32 / 57 Straight Selection Sort Algorithm SelectionSort() Sorts the contiguous list using straight selection sort. current = 0 while current < count - 1 do smallest = current walker = current + 1 while walker < count do if data [walker].key then smallest = walker end walker = walker + 1 end swap(current, smallest) current = current + 1 end Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 33 / 57 Heap Sort • The unsorted sublist is organized into a heap. • In each pass, in the unsorted sublist, the largest element is selected and exchanged with the last element. • The the heap is reheaped. Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 34 / 57 Heap Sort Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 35 / 57 Heap Sort Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 36 / 57 Heap Sort Algorithm HeapSort() Sorts the contiguous list using heap sort. position = count/2 - 1 while position >= 0 do ReheapDown(position, count - 1) position = position - 1 end last = count - 1 while last > 0 do swap(0, last) last = last - 1 ReheapDown(0, last - 1) end End HeapSort Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 37 / 57 Selection Sort Efficiency • Straight selection sort: O(n2 ) • Heap sort: O(nlog2 n) Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 38 / 57 Exchange Sort Exchange Sort • In each pass, elements that are out of order are exchanged, until the entire list is sorted. • Exchange is extensively used. Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 39 / 57 Bubble Sort • The list is divided into two parts: sorted and unsorted. • In each pass, the smallest element is bubbled from the unsorted sublist and moved to the sorted sublist. Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 40 / 57 Bubble Sort Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 41 / 57 Bubble Sort Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 42 / 57 Bubble Sort Algorithm BubbleSort() Sorts the contiguous list using bubble sort. current = 0, flag = False while current < count AND flag = False do walker = count - 1 flag = True while walker > current do if data [walker].key then flag = False swap(walker, walker - 1) end walker = walker - 1 end current = current + 1 end Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 43 / 57 Exchange Sort Efficiency • Bubble sort: f (n) = n(n + 1)/2 = O(n2 ) Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 44 / 57 Divide-and-Conquer