Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting Sanghyun Park Fall 2002 CSE, POSTECH. Sorts To Consider Selection sort Bubble sort Insertion sort Merge sort Quick sort Why do we care about sorting?

Similar presentations


Presentation on theme: "Sorting Sanghyun Park Fall 2002 CSE, POSTECH. Sorts To Consider Selection sort Bubble sort Insertion sort Merge sort Quick sort Why do we care about sorting?"— Presentation transcript:

1 Sorting Sanghyun Park Fall 2002 CSE, POSTECH

2 Sorts To Consider Selection sort Bubble sort Insertion sort Merge sort Quick sort Why do we care about sorting?

3 Selection Sort One of the most intuitive sorts Find the largest item and swap it with the item currently in the last position. Repeat this step on the “unsorted” part of the collection.

4 Selection Sort initial array :29 10 14 37 13 after 1st swap :29 10 14 13 37 after 2nd swap:13 10 14 29 37 after 3rd swap:13 10 14 29 37 after 4th swap:10 13 14 29 37

5 Selection Sort void SelectionSort(int a[], int n) { // sort the n elements a[0:n-1] for (int size = n; size > 1; size--) { int j = Max(a, size); Swap(a[j], a[size-1]); } Complexity O(n 2 )

6 Bubble Sort Not a particularly good algorithm Compare adjacent items and exchange them if they are out of order. Repeat. The largest item will eventually “bubble” to the correct position.

7 Bubble Sort Pass 1Pass 2 29 10 14 37 1310 14 29 13 37 10 29 14 37 1310 14 29 13 37 10 14 29 37 1310 14 29 13 37 10 14 29 37 1310 14 13 29 37 10 14 29 13 37

8 Bubble Sort void BubbleSort(int a[], int n) {// Sort a[0:n-1] using bubble sort for (int size = n; size > 1; size--) Bubble(a, size); } void Bubble(int a[], int n) {// Bubble largest element in a[0:n-1] to right for (int i = 0; i < n -1; i++) if (a[i] > a[i+1]) Swap(a[i], a[i+1]); } O(n 2 )for worst and average cases, O(n)for best case (already sorted)

9 Insertion Sort Imagine arranging a hand of cards, where you pick up one card at a time and insert into its proper position. For arrays of data, partition the array into a sorted region and an unsorted region.

10 Insertion Sort Initial Array:Action: 29 10 14 37 13Copy 10 29 29 14 37 13Shift 29 10 29 14 37 13Insert 10, copy 14 10 29 29 37 13Shift 29 10 14 29 37 13Insert 14, copy 37, Insert 37 10 14 29 37 13Copy 13 10 14 14 29 37Shift 14, 29, 37 10 13 14 29 37Insert 13

11 Insertion Sort void InsertionSort(int a[], int n) {// Sort a[0:n-1] using insertion sort for (int i = 1; i < n; i++) { int t = a[i]; Insert(a, i, t); }

12 Insertion Sort void Insert(int a[], int n, int x) {// insert x into the sorted array a[0:n-1] int i; for (i = n-1; i >= 0 && x < a[i]; i--) a[i+1] = a[i]; a[i+1] = x; } O(n 2 )for worst and average cases, O(n)for best case (already sorted)

13 Mergesort Divide and Conquer … recursive solution. Divide the array in half, sort each half, then merge. Require the use of a temporary array. Complexity O(n log n) Original array:8 1 4 3 2 Divide:8 1 43 2 Sort:1 4 82 3 Merge:1 2 3 4 8

14 Quicksort Divide and Conquer … recursive solution. Partition the array by establishing a pivot point. Concatenate the two sorted arrays. O(n log n) for best and average cases and O(n 2 ) for worst case (sorted, nearly sorted, or reverse sorted)

15 Summary AlgorithmWorst CaseAverage Case Selection sortn 2 n 2 Bubble sortn 2 n 2 Insertion sortn 2 n 2 Merge sortn log nn log n Quick sortn 2 n log n

16 Summary Why not always use Mergesort … on paper it’s the most efficient? On the average, Mergesort is not quite as fast as quicksort. Mergesort also requires extra storage equal to the size of the array to be sorted. Why bother with selection sort, bubble sort, and insertion sort? They can suffix for smaller data sets.

17 Final Exam Location: 공학 2 동 108 호 Date & time: 12/13 ( 금요일 ) 7 pm – 8:30 pm (key 를 미리 받아야 함 )

18 Coverage Priority Queues – definitions of priority queues, max (min) tree, max (min) heap – heap operations and their complexity – heap sort – LPT machine scheduling with a min priority queue Leftist Trees – definitions of extended binary tree and the function s() – definitions and properties of (height-balanced) leftist trees – operations (especially meld) of leftist trees

19 Coverage Tournament Trees – definition of winner tree – sorting with winner tree – bin packing with winner tree Binary Search Trees – definition of BST, indexed BST, balanced BST (AVL tree) – remove operation in BST get(rank) operation in indexed BST put and rotation operations in AVL tree

20 Coverage Graphs – definitions (digraph, complete graph, connected graph, …) – graph properties (sum of v degrees, numbers of v and e) – graph representation (adjacency matrix and adjacency list) – search methods (BFS and DFS, study with an example) Greedy Method – definition – Machine scheduling, container loading, and 0/1 knapsack problem with greedy method – Dijkstra’s algorithm for ‘single source all destinations’ shortest path problem (study with an example)

21 Coverage Minimum Cost Spanning Tree – definition and properties – Kruskal’s method, Prim’s method, Sollin’s method Divide and Conquer – principle – merge sort and its complexity – quick sort and its complexity

22 Coverage Divide and Conquer – principle – recurrence equation for 0/1 knapsack problem – recurrence equation for other problems Miscellaneous – sorting algorithms – study homework 4, 5, 6 – be creative …


Download ppt "Sorting Sanghyun Park Fall 2002 CSE, POSTECH. Sorts To Consider Selection sort Bubble sort Insertion sort Merge sort Quick sort Why do we care about sorting?"

Similar presentations


Ads by Google