1. Trang chủ
  2. » Giáo án - Bài giảng

Data Structure and Algorithms CO2003 Chapter 10 Sort

63 469 0

Đ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

Thông tin cơ bản

Định dạng
Số trang 63
Dung lượng 1,39 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Straight Insertion Sort• The list is divided into two parts:sortedandunsorted.. Shell Sort• For the value of K in each iteration, sort the K segments.. Shell SortAlgorithm SortSegmentval

Trang 1

Data Structure and Algorithms [CO2003]

Chapter 10 - Sort

Lecturer: Duc Dung Nguyen, PhD

Contact: nddung@hcmut.edu.vn

October 31, 2016

Faculty of Computer Science and Engineering

Hochiminh city University of Technology

Trang 3

• L.O.6.1 - Depict the working steps of sorting 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 sortingalgorithms

• 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 characterize the computationalcomplexity of algorithms composed by using the following control structures: sequence,

branching, and iteration (not recursion)

Trang 4

Sorting concepts

Trang 5

One of the mostimportant conceptsandcommon applicationsin computing

Trang 6

Sort stability: data with equal keys maintain their relative input order in the output

Trang 7

Sort efficiency: a measure of the relative efficiency of a sort = number ofcomparisons+

number ofmoves

Trang 8

Sorting

Trang 9

Insertion Sort

Trang 10

Straight Insertion Sort

• The list is divided into two parts:sortedandunsorted

• In each pass, the first element of the unsorted sublist is insertedinto the sorted sublist

Trang 11

Straight Insertion Sort

Trang 12

Straight Insertion Sort

Trang 13

Straight Insertion Sort

Trang 14

Straight Insertion Sort

Trang 15

Straight Insertion Sort

Trang 16

Straight Insertion Sort

Trang 17

Straight Insertion Sort

Trang 18

Shell Sort

• Named after its creator Donald L Shell (1959)

• Given a list of N elements, the list is divided into K segments(K is called theincrement)

• Each segment contains N/K or more elements

• Segments are dispersed throughout the list

• Also is called diminishing-increment sort

Trang 19

Shell Sort

Trang 20

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

Trang 21

Example of Shell Sort

Trang 22

Example of Shell Sort

Trang 23

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

Trang 24

Choosing incremental values

• Incremental values may be:

Trang 26

Shell Sort

Algorithm SortSegment(val segment <int>, val k <int>)

Sorts the segment beginning at segment using insertion sort, step between elements in the

Trang 27

Insertion Sort Efficiency

• Straight insertion sort:

f (n) = n(n + 1)/2 = O(n2)

• Shell sort:

O(n1.25) (Empirical study)

Trang 28

Selection Sort

Trang 29

Selection Sort

In each pass, the smallest/largest item isselectedand placed in a sorted list

Trang 30

Straight Selection Sort

• The list is divided into two parts:sortedandunsorted

• In each pass, in the unsorted sublist, the smallest element is selectedandexchangedwiththe first element

Trang 31

Straight Selection Sort

Trang 32

Straight Selection Sort

Trang 33

Straight Selection Sort

Trang 34

Straight Selection Sort

Trang 35

Straight Selection Sort

Trang 36

Straight Selection Sort

Trang 37

Straight Selection Sort

while walker < count do

if data [walker].key < data [smallest].key then

Trang 38

Heap Sort

• The unsorted sublist is organized into a heap

• In each pass, in the unsorted sublist, the largest element is selectedandexchangedwiththe last element

• The the heap isreheaped

Trang 39

Heap Sort

Trang 40

Heap Sort

Trang 42

Selection Sort Efficiency

• Straight selection sort:

O(n2)

• Heap sort:

O(nlog2n)

Trang 43

Exchange Sort

Trang 44

Exchange Sort

• In each pass, elements that are out of order are exchanged, until the entire list is sorted

• Exchange is extensively used

Trang 45

Bubble Sort

• The list is divided into two parts:sortedandunsorted

• In each pass, the smallest element isbubbledfrom the unsorted sublist and moved to thesorted sublist

Trang 46

Bubble Sort

Trang 47

Bubble Sort

Trang 48

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 < data [walker-1].key then

flag = False

swap(walker, walker - 1)

end

walker = walker - 1

Trang 49

Exchange Sort Efficiency

• Bubble sort:

f (n) = n(n + 1)/2 = O(n2)

Trang 50

Devide-and-Conquer

Trang 51

Devide-and-Conquer Sort

Algorithm DevideAndConquer()

if the list has length > 1 then

partition the list into lowlist and highlist

Trang 52

Devide-and-Conquer Sort

Partition Combine

Merge Sort easy hard

Quick Sort hard easy

Trang 54

Quick Sort

Algorithm recursiveQuickSort(val left <int>, val right <int>)

Sorts the contiguous list using quick sort

Pre: left and right are valid positions in the list

Post: list sorted

if left < right then

pivot_position =Partition(left, right)

recursiveQuickSort(left, pivot_position - 1)

recursiveQuickSort(pivot_position + 1, right)

end

Trang 55

Quick Sort

Given a pivot value, the partition rearranges the entries in the list as the following figure:

Trang 56

Quick Sort Efficiency

• Quick sort:

O(nlog2n)

Trang 57

Merge Sort

Trang 59

Merge Sort

Algorithm recursiveMergeSort(ref sublist <pointer>)

Sorts the linked list using recursive merge sort

if sublist is not NULL AND sublist->link is not NULL then

Divide(sublist, second_list)

Trang 60

Merge Sort

Algorithm Divide(val sublist <pointer>, ref second_list <pointer>)

Divides the list into two halves

Trang 61

Merge two sublists

Trang 62

Merge two sublists

Algorithm Merge(ref first <pointer>, ref second <pointer>)

Merges two sorted lists into a sorted list

lastSorted = address of combined

while first is not NULL AND second is not NULL do

if first->data.key <= second->data.key then

Trang 63

Merge two sublists

Ngày đăng: 29/03/2017, 18:21

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN