sorting algorithms jyh-shing roger jang ( 張智星 ) csie dept, national taiwan university

14
Sorting Algorithms Jyh-Shing Roger Jang ( 張張張 ) CSIE Dept, National Taiwan University

Upload: blaze-hart

Post on 04-Jan-2016

238 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Sorting Algorithms Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

Sorting Algorithms

Jyh-Shing Roger Jang (張智星 )CSIE Dept, National Taiwan University

Page 2: Sorting Algorithms Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

2

About Sorting

We have covered Selection sort Insertion sort Bubble sort Heap sort

Other efficient sorting algorithms Merge sort O(n log n) in worse case Quick sort O(n log n) in average case

O(n2) in average case

O(n log n) in worse case

Page 3: Sorting Algorithms Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

3

TERMINOLOGIES FOR SORTING

In-place sorting Sorting a sequence with O(1) extra space to store intermediate results

Stable sorting If the same element is present multiple time, then they remain the original relative order of positions after sorting

External sorting Sorting records not present in memory

Page 4: Sorting Algorithms Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

4

C++ STL Sorting Functions

sort function template void sort(iterator begin, iterator end) void sort(iterator begin, iterator end, Comparator cmp) begin and end are start and end marker of a container (or a

range of it) Container needs to support random access such as vector Sort() is not a stable sorting

stable_sort() is stable

Page 5: Sorting Algorithms Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

5

Characteristic Diagrams for Sorting

before execution during execution after execution

Index

key value

N distinct keys within [1,N]

45-degree line after sorting

Page 6: Sorting Algorithms Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

6

Insertion Sorting a Random Permutation

Page 7: Sorting Algorithms Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

7

Selection Sorting a Random Permutation

Page 8: Sorting Algorithms Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

8

Bubble Sorting a Random Permutation

Page 9: Sorting Algorithms Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

9

Merge Sorting a Random Permutation

Page 10: Sorting Algorithms Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

10

Quicksort (recursive implementation, M=12)

Page 11: Sorting Algorithms Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

11

Heapsorting a Random Permutation: Construction

Page 12: Sorting Algorithms Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

12

Heapsorting (Sorting Phase)

Page 13: Sorting Algorithms Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

13

Stages of Straight Radix Sort

Page 14: Sorting Algorithms Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

14

Shell Sorting a Random Permutation