1. Trang chủ
  2. » Tất cả

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

63 4 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

Tiêu đề Sorting
Tác giả Luong The Nhan, Tran Giang Son
Trường học University of Technology, VNU-HCM
Chuyên ngành Data Structures and Algorithms
Thể loại lecture notes
Thành phố Ho Chi Minh City
Định dạng
Số trang 63
Dung lượng 1,47 MB

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

Nội dung

Luong The Nhan, Tran Giang SonSorting concepts Insertion Sort Straight Insertion SortShell Sort Devide-and-Quick SortMerge Sort Selection Sort In each pass, the smallest/largest item is

Trang 1

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Chapter 10

Sorting

Data Structures and Algorithms

Luong The Nhan, Tran Giang Son Faculty of Computer Science and Engineering

University of Technology, VNU-HCM

Trang 2

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

• 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

characterize the computational complexity of algorithms

Trang 3

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Trang 4

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Sorting concepts

Trang 5

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Sorting

One of the most important concepts and

common applications in computing.

Trang 6

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Sorting

Sort stability: data with equal keys maintain

their relative input order in the output.

Trang 7

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Sorting

Sort efficiency: a measure of the relative

efficiency of a sort = number of comparisons +

number of moves.

Trang 8

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Sorting

Trang 9

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Insertion Sort

Trang 10

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge 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.

Trang 11

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Straight Insertion Sort

Trang 12

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Straight Insertion Sort

Trang 13

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Straight Insertion Sort

Trang 14

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Straight Insertion Sort

Trang 15

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Straight Insertion Sort

Trang 16

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Straight Insertion Sort

Trang 17

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Straight Insertion Sort

Trang 18

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

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 the

increment).

• Each segment contains N/K or more

elements.

• Segments are dispersed throughout the list.

• Also is called diminishing-increment sort.

Trang 19

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Shell Sort

Trang 20

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

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

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Example of Shell Sort

Trang 22

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Example of Shell Sort

Trang 23

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

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

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Choosing incremental values

• Incremental values may be:

Trang 25

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Trang 26

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

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.

Trang 27

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Insertion Sort Efficiency

• Straight insertion sort:

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

• Shell sort:

O(n 1.25 ) (Empirical study)

Trang 28

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Selection Sort

Trang 29

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Selection Sort

In each pass, the smallest/largest item is

selected and placed in a sorted list.

Trang 30

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

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.

Trang 31

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Straight Selection Sort

Trang 32

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Straight Selection Sort

Trang 33

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Straight Selection Sort

Trang 34

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Straight Selection Sort

Trang 35

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Straight Selection Sort

Trang 36

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Straight Selection Sort

Trang 37

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Straight Selection Sort

while walker < count do

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

smallest = walker end

Trang 38

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

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.

Trang 39

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Heap Sort

Trang 40

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Heap Sort

Trang 41

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Trang 42

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Selection Sort Efficiency

• Straight selection sort:

O(n 2 )

• Heap sort:

O(nlog 2 n)

Trang 43

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Exchange Sort

Trang 44

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

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

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

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.

Trang 46

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Bubble Sort

Trang 47

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Bubble Sort

Trang 48

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

while walker > current do

if data [walker].key < data [walker-1].key then

flag = False swap(walker, walker - 1) end

walker = walker - 1

end

current = current + 1

Trang 49

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Exchange Sort Efficiency

• Bubble sort:

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

Trang 50

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Devide-and-Conquer

Trang 51

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Devide-and-Conquer Sort

Algorithm DevideAndConquer()

if the list has length > 1 then

partition the list into lowlist and highlist

Trang 52

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Devide-and-Conquer Sort

Partition Combine

Trang 53

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Trang 54

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

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)

Trang 55

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Quick Sort

Given a pivot value, the partition rearranges

the entries in the list as the following figure:

Trang 56

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Quick Sort Efficiency

• Quick sort:

O(nlog 2 n)

Trang 57

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Merge Sort

Trang 58

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Trang 59

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

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

Trang 60

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Trang 61

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Merge two sublists

Trang 62

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

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

Luong The Nhan, Tran Giang Son

Sorting concepts Insertion Sort

Straight Insertion SortShell Sort

Devide-and-Quick SortMerge Sort

Merge two sublists

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

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

TÀI LIỆU LIÊN QUAN