SORTING

SORTING - SORTING Problem sort a list of numbers(or comparable objects Solution An algorithm The problem is interesting for its theoretical value

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
SORTING Problem: sort a list of numbers (or comparable objects). Solution: An algorithm. The problem is interesting for its theoretical value, and for its practical utility. Many algorithms are available for the purpose. Bubble Sort BubleSort (A) .1 for i=1 through n do .2 for j=n through i+1 do .3 if A[j] < A[j-1] then .4 exchange A[j] < - > A[j-1] End algorithm. Lemma : Lines 2 through 4 get the smallest element of A[i] through A[n] at the i-th position of the array. Loop invariant for lines 2 through 4 is the property that A[j-1] A[j] Proof : Initialization: Starts with A[n]. Maintenance: After j=k-th iteration, for some i<k n, A[k- 1] A[k].
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Termination: The loop terminates at j=i+1. At that point A[i] A[i+1], A[i] A[i+2], …, A[i] A[n]. So, A[i] is the smallest element between A[i] through A[n]. QED Theorem: BubbleSort correctly sorts the input array A. Loop invariant for lines 1 through 4 is that A[i] A[i+1]. Initialization: Starts with A[1]. Maintenance: After the iteration i=p, for some 1<p n, the above lemma for lines 2 through 4 guarantees that A[p] is the smallest element from A[p] through A[n]. So, A[p] A[p+1]. Termination: The loop 1 through 4 terminates at i=n. At that point A[1] A[2] . . . A[n]. Hence, the correctness theorem is proved. QED Complexity: i=1 n j=i+1 n 1 = i=1 n (n –i) = n 2 – n(n+1)/2 = n 2 /2 – n/2 = Θ (n 2 ) Insertion Sort The way cards are sorted in the pack: pick up objects one by one from one end and insert it in its correct position in the partially sorted part. Based on inductive proof technique:
Background image of page 2
Induction base: only one element in array – it is already sorted (p=1) Induction hypothesis: assume the algorithm works correctly up to k-th iteration, i.e., we have sorted array 1 through k (p=k) Induction step: a[k+1] is correctly inserted in the sorted portion a[1. .k] (step 3 -5 in the algorithm below does this) So, a[1. .k+1] is sorted. QED . Algorithm insertion-sort (an array A of n comparable objects) .(1) for p = 2 through n do .(2) temp = A[p]; .(3) for j = p though 2 do until A[j-1]<temp .(4) A[j] = A[j-1]; // shift all larger elements right .(5) A[j-1] = temp; // insert it in its correct position in the // already sorted portion End Algorithm. Example: 34 8 64 51 32 21 8 34 64 51 32 21 8 34 64 51 32 21 8 34 51 64 32 21 8 32 34 51 64 21
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 21 32 34 51 64. Worst-case Complexity: 2 + 3 + 4 + … + n = n(n+1)/2 -1 = (n 2 )/2 + n/2 -1 = O(n 2 ) (For reverse sorted array). Actual run time could be better (loop may terminate earlier than running for p-1 times! The best case scenario (sorted array as input): (n). Average case complexity: Some observations: (1) There are 9 "inversions" in the above original list: (34, 8), (34, 32), … (51, 32), (51, 31), … [FIND OTHERS]. (2)
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/10/2012 for the course CSE 5211 taught by Professor Dmitra during the Spring '12 term at FIT.

Page1 / 27

SORTING - SORTING Problem sort a list of numbers(or comparable objects Solution An algorithm The problem is interesting for its theoretical value

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online