TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) The Sorting Problem Sorting: Input: – Intro: aspects of sorting, different strategies • A list L of data items with keys Output: • A list L’ of the same data items in increasing order of keys, – Insertion Sort, Selection Sort, – Quick Sort – Heap Sort, Caution! • Don’t over use sorting! • Do you really need to have it sorted, or will a dictionary do fine instead of a sorted array? – Merge Sort – Theoretical lower bound for comparison-based sorting, – Digital Sorting: BucketSort, RadixSort Jan Maluszynski - HT 2006 8.1 Jan Maluszynski - HT 2006 TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) Aspects of Sorting: Strategies 8.2 • Insertion sorts: • • • • For each new element to add to the sorted set, look for the right place in that set to put the element... Linear insertion, Shell sort, ... Internal / External In-place / With additional memory Stable / unstable Comparison-based / Digital • Selection sorts: In each iteration, search the unsorted set for the smallest (largest) remaining item to add to the end of the sorted set Straight selection, Heap sort, ... • Exchange sorts: Browse back and forth in some pattern, and whenever we are looking at a pair with wrong relative order, swap them... Quick sort, Merge sort... Jan Maluszynski - HT 2006 8.3 Jan Maluszynski - HT 2006 8.4 1 TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) (Linear) insertion sort Analysis of Insertion Sort ”In each iteration, insert the first item from unsorted part 1 2 3 4 5 Its proper place in the sorted part” In-place A[0..n-1] ! Data stored in A[0..n-1] from i =1 to n-1: – Sorted data in A[0.. i -1] – Unsorted data in A[i..n-1] • Scan sorted part for index s for insertion of the selected item • Increase i i • • • • • • • s i Jan Maluszynski - HT 2006 8.5 TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) (Straight) selection sort Analysis of selection sort remaining item to add to the end of the sorted set” 1 2 3 4 5 In-place A[0..n-1] ! from i =1 to n-1: • t1: n-1 passes t2: n-1 passes... t3: I = worst case no. of iterations in inner loop: I = 1+2+…+n-1 = n(n-1)/2 thus O(n2) t4: I passes t5: n-1 passes T: t1+t2+t3+t4+t5 thus O(n2) in worst case, but …. good if file almost sorted Jan Maluszynski - HT 2006 ”In each iteration, search the unsorted set for the smallest • Procedure InsertionSort (table A[0..n-1]): for i from 1 to n-1 do j ← i; x ← A[i] while j ≥1 and A[j-1]>x do A[j] ← A[j-1] ; j ← j-1 A[j] ↔ x i – Sorted data in A[0.. i -1] – Unsorted data in A[i..n-1] Find index s of smallest key Swap places for A[i] and A[s] i Procedure Selectionsort (table A[0..n-1]): for i from 0 to n-2 do s←i for j from i+1 to n-1 do if A[j] < A[s] then s ← j A[i] ↔ A[s] • t1: n-1 passes • t2: n-1 passes... • t3: I = no. of iterations in inner loop: I = n-2 + n-3 + n-4 +...+1 = (n-2)(n-1)/2 = (n2-3n+2)/2 • t4: I passes • t5: n-1 passes • T: t1+t2+t3+t4+t5 = 3*(n-1)+2*(n2-3n+2)/2 ...thus O(n2) ...rather bad! s i i is is s Jan Maluszynski - HT 2006 8.6 8.7 Jan Maluszynski - HT 2006 8.8 2