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?
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.
Selection Sort initial array : after 1st swap : after 2nd swap: after 3rd swap: after 4th swap:
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 )
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.
Bubble Sort Pass 1Pass
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)
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.
Insertion Sort Initial Array:Action: Copy Shift Insert 10, copy Shift Insert 14, copy 37, Insert Copy Shift 14, 29, Insert 13
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); }
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)
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: Divide: Sort: Merge:
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)
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
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.
Final Exam Location: 공학 2 동 108 호 Date & time: 12/13 ( 금요일 ) 7 pm – 8:30 pm (key 를 미리 받아야 함 )
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
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
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)
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
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 …