Chapter 4 Sorting Sorting Problem input A sequence
Chapter 4 Sorting
Sorting Problem • • input: A sequence of n numbers <a 1 , . . . , an> output: A permutation (reordering) < a 1’ , . . . , an’> of the input sequence such that a 1’ a 2’ … an’ • example: • input: <31, 41, 59, 26, 41, 58> • output: <26, 31, 41, 58, 59>
Types of Sorting Algorithms • in place • only a constant number of elements of the input array are ever stored outside the array [insertion sort] • comparison based • the only operation we can perform on keys is to compare two keys [insertion sort, merge sort, quick sort, … ] • A non-comparison based sorting algorithm – looks at values of individual elements – requires some prior knowledge – [counting sort, radix sort, bucket sort]
Insertion Sort Pass over Array, picking elements 1 by 1 For each element insert it into correct place in array of already sorted elements Until last element inserted into place
Insertion Sort
Insertion Sorted Not Sorted
Insertion Sort
Insertion Sort Insertion-Sort(A) 1 for i 2 to length[A] 2 key A[i] 3 j i-1 4 while j>0 and A[j]>key 5 A[j+1] A[j] 6 j j-1 7 A[j+1] key
Insertion Sort (Best Case) • A[i] > key always false • The number of loop is only the number of for loop • A[j] will not move • O(n)
Insertion Sort (Worst Case) • A[i] > key always true • The number of while loop will always be computed • A[j] will move to the first position • O(n^2)
Insertion Sort (Average Case) • On average: • Depends on A[j] that whether it will be inserted in what position • Give the possibility that jth data will be inserted is the same • O(n^2)
Selection Sort
Selection Sort • • Find the maximum value in data set Swap between the maximum value and the last data in data set Delete data set size one by one Do 1 -3 recursively until data set size = 1
Selection Sort • • • 524613| 524316 52431|6 12435 6 1243|56 1234 5 6 123|456 123 4 5 6 12|3456 12 3 4 5 6 1|23456 12 3 4 5 6
Selection Sort Selection-Sort(A [1, …, n]) for ( i = n downto 2) { j=1; for (k=2; k i; k++) { if (A[j] < A[k] ) j = k; Swap (A, i, j) ; n n-1 (n(n+1)/2)-1 n(n-1)/2 n-1
Selection Sort • • 01 Selection. Sort (A [1 … n]) 02 { 03 if ( n 1) return; 04 j = index. Max ( A [1 …n]); 05 Swap (A, n, j); 06 Selection. Sort (A [1 … n-1]); 07 } t(n) = t (n-1) + (n) n > 1; t(1) = (1)
Selection Sort (Worst Case) t(n) = t (n-1) + (n) n > 1; t(1) = (1)
Selection Sort (Best Case) t(n) = t (n-1) + (1) n > 1; t(1) = (1)
Selection Sort (Average Case) • On average: t(n) = t (n-1) + (1/2(n+1)) n > 1; t(1) = (1)
Merge Sort
Merge Sort • • Merge-sort on an input sequence S with n elements consists of three steps: 1: Split the list into 2 equal size sub-lists 2: Recursively sort each of these sub-lists (using Merge-Sort) • 3: Merge the two sorted sub-lists to make a single sorted list
Merge Sort Algorithm
Merge Sort Algorithm
Merging Two Sorted Sequences • The third step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B • Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time
Merging Two Sorted Sequences Algorithm
Merging Two Sorted Sequences Algorithm
Merge-Sort Tree • An execution of merge-sort is depicted by a binary tree – each node represents a recursive call of mergesort and stores • unsorted sequence before the execution and its partition • sorted sequence at the end of the execution – the root is the initial call – the leaves are calls on subsequences of size 0 or 1
Merge-Sort Tree
Execution Example • Partition
Execution Example • Recursive call, Partition
Execution Example • Recursive call, Partition
Execution Example • Recursive call, Partition
Execution Example • Recursive call, Base case
Execution Example • Recursive call, Base case
Execution Example • Merge
Execution Example • Merge
Execution Example • Recursive call, …, Base case, Merge
Execution Example • Merge
Execution Example • Recursive call, …, Merge
Running Time The height h of the merge-sort tree is O(log n) at each recursive call we divide in half the sequence, The overall amount or work done at the nodes of depth i is O(n) we partition and merge 2 i sequences of size n/2 i we make 2 i+1 recursive calls
Running Time • • • T(n) = Tsplit(n) + 2 T(n/2) + TMerge(n) n = 1 => T(n) = 1 For n > 1 : T(n) = 2 T(n/2) + O(n) => T(n) = O(nlogn)
Quick sort
Quick sort • Step 1: • Re-arrange A[left, right] • partition it to 2 parts: – A[left, p], A[p+1, right] – so that x A[left. . p] x min A[(p+1). . right] • Step 2: – Quick-Sort(A[left, p]) – Quick-Sort(A[p+1, right])
Quick sort Algorithm quicksort( void *a, int low, int high ) { int pivot; /* Termination condition! */ if ( high > low ) { pivot = partition( a, low, high ); quicksort( a, low, pivot-1 ); quicksort( a, pivot+1, high ); } }
Partition Pick some element of A - e = A[left] • scan from left until A[L] e is found • scan from right until A[R]<=e is found • if L < R swap A[L] and A[R] • continue scanning and exchanging until R>L then return p R
Partition p i j <=C i >=C j 4 2 5 9 4 3 7 0 8 6 i r i j 4 2 5 9 4 3 7 0 8 6 j 0 2 5 9 4 3 7 4 8 6 i j 0 2 3 9 4 5 7 4 8 6 i j 0 2 3 4 9 5 7 4 8 6 i j 0 2 3 9 4 5 7 4 8 6 j i 0 2 3 4 9 5 7 4 8 6
Partition Algorithm
Partition Algorithm int partition( void *a, int low, int high ) /* right is final position for the pivot */ { int left, right; a[low] = a[right]; void *pivot_item; a[right] = pivot_item; pivot_item = a[low]; return right; pivot = left = low; } right = high; while ( left < right ) { /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++; /* Move right while item > pivot */ while( a[right] > pivot_item ) right--; if ( left < right ) SWAP(a, left, right); }
Partition Running Time: Condition will stop when L R L-R at the first loop is n+1 (L=0, R=n+1) and decrease 1 each T(n) = O(n)
Quick-Sort - Running Time depends on the position p in A[left. . right]
Quick. Sort – Worst case
Quick. Sort – Worst case
Quick. Sort – Best case
Balance Partitioning
Average Case
Average Case
Average Case
Average Case
Average Case
Average Case
Average Case
Heap Sort Running time - O(nlgn) like. . . Merge Sort unlike. . . Insertion Sort • In place like. . . Insertion Sort unlike. . . Merge Sort • Uses a heap
(Binary) Heaps Complete binary tree (may be missing some rightmost leaves) • each node contains a key • heap property: a node’s key its children’s keys ³
Binary Tree An array implementation: A[1. . n] • root - at A[1] • parent(i) is in A[ ] – Left(i) is in A[2 i] – Right(i) is in A[2 i+1] Reminder height of a node = longest path down to a leaf height of the tree = height of the root
Heap - an Implementation
Heapify (A, i) l ← left [i] r ← right [i] if l ≤ heap-size [A] and A[l] > A[i] then largest ← l else largest ← i if r ≤ heap-size [A] and A[i] > A[largest] then largest ← r if largest ≠ i then exchange A[i] ↔ A[largest] Heapify (A, largest)
Build-Heap BUILD_HEAP (A) heap-size (A) ← length [A] For i ← floor(length[A]/2) down to 1 do Heapify (A, i)
Heap-sort HEAPSORT (A) BUILD_HEAP (A) for i ← length (A) down to 2 do exchange A[1] ↔ A[i] heap-size [A] ← heap-size [A] - 1 Heapify (A, 1)
Heap Operations Heapify – restoring the heap property Build-Heap – build a heap from an array of keys
Example
Heapify - Running Time •
Build-Heap (running time) •
Heap-Sort - Example •
- Slides: 73