Cấu trúc dữ liệu và thuật toándsa ch10 sorting 1

63 4 0
Cấu trúc dữ liệu và thuật toándsa ch10 sorting 1

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Sorting Luong The Nhan, Tran Giang Son Chapter 10 Sorting Sorting concepts Insertion Sort Data Structures and Algorithms Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Luong The Nhan, Tran Giang Son Faculty of Computer Science and Engineering University of Technology, VNU-HCM Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.1 Outcomes • L.O.6.1 - Depict the working steps of sorting Sorting Luong The Nhan, Tran Giang Son algorithms step-by-steps • L.O.6.2 - Describe sorting algorithms by using pseudocode • L.O.6.3 - Implement sorting algorithms using C/C++ • L.O.6.4 - Analyze the complexity and develop experiment (program) to evaluate sorting algorithms • L.O.6.5 - Use sorting algorithms for problems in real-life • L.O.8.4 - Develop recursive implementations for methods supplied for the following structures: list, tree, heap, searching, and graphs • L.O.1.2 - Analyze algorithms and use Big-O notation to Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort characterize the computational complexity of algorithms composed by using the following control structures: sequence, branching, and iteration (not recursion) 10.2 Contents Sorting Luong The Nhan, Tran Giang Son Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-and-Conquer Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort Quick Sort Merge Sort 10.3 Sorting Luong The Nhan, Tran Giang Son Sorting concepts Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.4 Sorting Sorting Luong The Nhan, Tran Giang Son One of the most important concepts and common applications in computing Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.5 Sorting Sorting Luong The Nhan, Tran Giang Son Sort stability: data with equal keys maintain their relative input order in the output Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.6 Sorting Sorting Luong The Nhan, Tran Giang Son Sorting concepts Sort efficiency: a measure of the relative efficiency of a sort = number of comparisons + number of moves Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.7 Sorting Sorting Luong The Nhan, Tran Giang Son Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.8 Sorting Luong The Nhan, Tran Giang Son Sorting concepts Insertion Sort Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.9 Straight Insertion Sort Sorting Luong The Nhan, Tran Giang Son • 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 Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.10 Exchange Sort Efficiency Sorting Luong The Nhan, Tran Giang Son Sorting concepts • Bubble sort: f (n) = n(n + 1)/2 = O(n2 ) Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.49 Sorting Luong The Nhan, Tran Giang Son Sorting concepts Devide-and-Conquer Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.50 Devide-and-Conquer Sort Sorting Luong The Nhan, Tran Giang Son Algorithm DevideAndConquer() if the list has length > then partition the list into lowlist and highlist lowlist.DevideAndConquer() highlist.DevideAndConquer() combine(lowlist, highlist) end End DevideAndConquer Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.51 Devide-and-Conquer Sort Sorting Luong The Nhan, Tran Giang Son Partition Combine Merge Sort easy hard Quick Sort hard easy Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.52 Quick Sort Sorting Luong The Nhan, Tran Giang Son Algorithm QuickSort() Sorts the contiguous list using quick sort Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort recursiveQuickSort(0, count - 1) End QuickSort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.53 Quick Sort Algorithm recursiveQuickSort(val left , val right ) Sorts the contiguous list using quick sort Pre: left and right are valid positions in the list Post: list sorted Sorting Luong The Nhan, Tran Giang Son Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort Selection Sort if left < right then pivot_position = Partition(left, right) recursiveQuickSort(left, pivot_position - 1) recursiveQuickSort(pivot_position + 1, right) end End recursiveQuickSort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.54 Quick Sort Sorting Luong The Nhan, Tran Giang Son Given a pivot value, the partition rearranges the entries in the list as the following figure: Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.55 Quick Sort Efficiency Sorting Luong The Nhan, Tran Giang Son Sorting concepts • Quick sort: O(nlog2 n) Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.56 Merge Sort Sorting Luong The Nhan, Tran Giang Son Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.57 Merge Sort Sorting Luong The Nhan, Tran Giang Son Algorithm MergeSort() Sorts the linked list using merge sort Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort recursiveMergeSort(head) End MergeSort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.58 Merge Sort Sorting Luong The Nhan, Tran Giang Son Algorithm recursiveMergeSort(ref sublist ) Sorts the linked list using recursive merge sort Sorting concepts if sublist is not NULL AND sublist->link is not NULL then Divide(sublist, second_list) recursiveMergeSort(sublist) recursiveMergeSort(second_list) Merge(sublist, second_list) end End recursiveMergeSort Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.59 Merge Sort Algorithm Divide(val sublist , ref second_list ) Divides the list into two halves midpoint = sublist position = sublist->link midpoint chạy position chạy while position is not NULL position = position->link if position is not NULL then midpoint = midpoint->link position = position->link end end second_list = midpoint->link midpoint->link = NULL End Divide Sorting Luong The Nhan, Tran Giang Son Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.60 Merge two sublists Sorting Luong The Nhan, Tran Giang Son Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.61 Merge two sublists Algorithm Merge(ref first , ref second ) Merges two sorted lists into a sorted list lastSorted = address of combined while first is not NULL AND second is not NULL if first->data.key data.key then lastSorted->link = first lastSorted = first first = first->link else lastSorted->link = second lastSorted = second second = second->link end so sánh giá trị đầu mảng end lấy nhỏ ghép vào lastSorted Sorting Luong The Nhan, Tran Giang Son Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort // 10.62 Merge two sublists Sorting Luong The Nhan, Tran Giang Son // if first is NULL then lastSorted->link = second second = NULL else lastSorted->link = first end first = combined.link End Merge Sorting concepts Insertion Sort Straight Insertion Sort Shell Sort Selection Sort Straight Selection Sort Heap Sort Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10.63 ... Quick Sort Merge Sort 10 .23 Choosing incremental values Sorting Luong The Nhan, Tran Giang Son • Incremental values may be: 1, 4, 13 , 40, 12 1, kt = ki? ?1 = ∗ ki + t = | log3 n| − Sorting concepts... Exchange Sort Bubble Sort Devide-andConquer Quick Sort Merge Sort 10 .11 Straight Insertion Sort Sorting Luong The Nhan, Tran Giang Son Sorting concepts Insertion Sort Straight Insertion Sort Shell... L.O.6 .1 - Depict the working steps of sorting Sorting Luong The Nhan, Tran Giang Son algorithms step-by-steps • L.O.6.2 - Describe sorting algorithms by using pseudocode • L.O.6.3 - Implement sorting

Ngày đăng: 25/03/2023, 07:21

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan