1  Searching  Linear O(N)  bisection O(log(N))  dictionary O(log(log(N))  Sorting  Insertion, bubble, selection O(N 2 )  Merge, Quick, Heap O(Nlog.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

§7 Quicksort -- the fastest known sorting algorithm in practice 1. The Algorithm void Quicksort ( ElementType A[ ], int N ) { if ( N < 2 ) return; pivot.
CSC 2300 Data Structures & Algorithms March 16, 2007 Chapter 7. Sorting.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Using Divide and Conquer for Sorting
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Spring 2015 Lecture 5: QuickSort & Selection
CSE 373: Data Structures and Algorithms
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
Chapter 19: Searching and Sorting Algorithms
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
Sorting Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
1 TCSS 342, Winter 2005 Lecture Notes Sorting Weiss Ch. 8, pp
Sorting Algorithms Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Sorting. Introduction Assumptions –Sorting an array of integers –Entire sort can be done in main memory Straightforward algorithms are O(N 2 ) More complex.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
Lecture 8 Sorting. Sorting (Chapter 7) We have a list of real numbers. Need to sort the real numbers in increasing order (smallest first). Important points.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting CS 202 – Fundamental Structures of Computer Science II Bilkent.
Lecture 8 Sorting. Sorting (Chapter 7) We have a list of real numbers. Need to sort the real numbers in increasing order (smallest first). Important points.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
CSE 373 Data Structures Lecture 19
Data Structures/ Algorithms and Generic Programming Sorting Algorithms.
Sorting What makes it hard? Chapter 7 in DS&AA Chapter 8 in DS&PS.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
DIVIDE & CONQUR ALGORITHMS Often written as first as a recursive algorithm Master’s Theorem: T(n) = aT(n/b) + cn i, for some constant integer i, and constants.
Sorting in Linear Time Lower bound for comparison-based sorting
CSE 373 Data Structures Lecture 15
95-771: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 13: Sorting II Data Structures.
Sorting and Lower bounds Fundamental Data Structures and Algorithms Ananda Guna January 27, 2005.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
1 Chapter 7: Sorting (Insertion Sort, Shellsort) CE 221 Data Structures and Algorithms Izmir University of Economics Text: Read Weiss, § 7.1 – 7.4.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Analysis of Algorithms CS 477/677
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
1 Sorting Algorithms Sections 7.1 to Comparison-Based Sorting Input – 2,3,1,15,11,23,1 Output – 1,1,2,3,11,15,23 Class ‘Animals’ – Sort Objects.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Sorting What makes it hard? Chapter 7 in DS&AA Chapter 8 in DS&PS.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
COSC 3101A - Design and Analysis of Algorithms 6 Lower Bounds for Sorting Counting / Radix / Bucket Sort Many of these slides are taken from Monica Nicolescu,
CSC 201 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
IS 2610: Data Structures Discuss HW 2 problems Binary Tree (continued) Introduction to Sorting Feb 9, 2004.
Sorting Algorithms Merge Sort Quick Sort Hairong Zhao New Jersey Institute of Technology.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
1 Chapter 7: Sorting (Insertion Sort, Shellsort) CE 221 Data Structures and Algorithms Izmir University of Economics Text: Read Weiss, § 7.1 – 7.4.
Chapter 5 Sorting There are several easy algorithms to sort in O(N 2 ), such as insertion sort. There is an algorithm, Shellsort, that is very simple to.
Sorting & Lower Bounds Jeff Edmonds York University COSC 3101 Lecture 5.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting So Far Insertion sort: –Easy to code –Fast on small inputs (less than ~50 elements) –Fast on nearly-sorted.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Description Given a linear collection of items x1, x2, x3,….,xn
Chapter 4: Divide and Conquer
Quick Sort (11.2) CSE 2011 Winter November 2018.
CSc 110, Spring 2017 Lecture 39: searching.
slides adapted from Marty Stepp
CSE 326: Data Structures Sorting
CSE 332: Data Abstractions Sorting I
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
The Selection Problem.
Sorting Popular algorithms:
Presentation transcript:

1  Searching  Linear O(N)  bisection O(log(N))  dictionary O(log(log(N))  Sorting  Insertion, bubble, selection O(N 2 )  Merge, Quick, Heap O(Nlog (N))  Shell O(N log(N) log(N))  Bin, Bucket, Radix O(N)  Proof:  (N 2 ) near neighbor exchange  Proof:  (N log(N)) Comparison search  Median (or k quick selection) Problem Summary of Algorithms

2 Sorting References. Read Wiese Chapter 7 (Heap sort we do last) Also see Course Documents on for notes by Clifford Schaffer (Often better than text!)

3 Searching & Sorting: Fundamental 1-D Data Structure  Array type a[0],a[1],…,a[N-1] Essentially a model of Memory: a[i] = base address + offset base = a offset = i x sizeof(type) i = 0,...,N-1 Lecture 4, 2006

4 int a[0], a[1],a[2],a[3],.... a[m],.... a[2],a[N-1] Searching: “Why Sort at All?” Three Algorithms: Linear Search  (after Sorting) Bisection Search  Dictionary Search  O(log(N)). O(N) O(log[log[N]])

5 int a[0], a[1],a[2],a[3],.... a[m],.... a[N-2],a[N-1] Bisection Search of Sorted List i= 0; j= N-1; m = N/2 while(b!=a[m] && i!=j ){ if(b>a[m]) i = m; if(b<a[m]) j = m; m = (j-i)/2 + i;} if(b==a[m])) “found it” else “not found” i j T(N) = T(N/2) + c 0  T(N) » Log(N) Choose mid point

6 int a[0], a[1],a[2],a[3],.... a[m],.... a[2],a[N-1] Dictionary: Sorted and Uniform Extra Knowledge Helps: % Error » 1/N 1/2 i j T(N) = T(N 1/2 ) + c 0  T(N) » Log(Log(N)) m Dictionary: Same code EXCEPT estimate location of b x = fractional distance (0<x<1) x = (b-a[i])/(a[j] – a[i]) ; m = x (j-i) + i ;

7 Classic Problem: Comparison Sorting y Local Exchange  Recursive  Shell Sort  Can Prove  y Problem = MAX[  k k a[k]]

8 Insertion Sort --- Deck of Cards Insertion Sort(a[0:N-1]): for (i=1; i < n; i ++) for (j = i; (j>0) && (a[j]<a[j-1]); j--) swap a[j] and a[j-1] ; Worst case  (N 2 ) number of “swaps” ( i.e. time)

9 Outer loop trace for Insertion Sort a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] ( Swaps) 6 | (1) 5   | (2) 2   6 2   | (0) | (3) | (3) | 7 1 (1) | 1 (7) (17)

10 Bubble Sort --- Sweep R to L Bubble Sort(a[0:N-1]): for i=0 to n-1 for j = n-1 to i + 1 if a[j]<a[j-1] then swap a[i] and a[j] Worst case  (N 2 ) swaps (time)

11 Outer loop trace for Bubble Sort a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] ( Swaps) (7) 1 | (3) 1 2 | (3) | (3) | (1) | (0) | 7 8 (0) (17)  NOTE SAME # OF SWAPS? WHY?

12 Average Number of N(N-1)/4 swaps Best Case: sorted order  0 swaps Worst Case: reverse order  N(N-1)/2 swaps since N-1 = N(N-1)/2 Average Case: Pair up each of the N! permutations with its reverse order  Every pair must swap in one or the other: Thus average is half of all swaps  (1/2) N(N-1)/2 q.e.d.

13 Selection Sort --- (Bubble only the index) Selection Sort(a[0:N-1]): for i=1 to n-2 { min = i for j = n-1 to i + 1 if a[j]<a[min] then min = j; swap a[i] and a[min]; } worst case  (N) swaps +  (N 2 ) comparisons

14 Outer loop trace for Selection Sort a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] ( Swaps) (1) 1   (1) 2  | (1) | (1) | (0) | (1) | 7 8 (0) (5)  NOTE SAME # OF SWAPS IS DRASTICALLY REDUCED!

15 void mergesort(int a[ ], int l, int r) if (r > l) { m = (r+l)/2; mergesort(a, l, m); mergesort(a, m+1, r); for (i = l; i < m+1; I++) b[i] = a[i]; for (j = m; j < r; j++) b[r+m-j] = a[j+1]; // reverse for (k = l; k <= r; k++) a[k] = (b[i] < b[j]) ? b[i++] : b[j--]; } Merge Sort: Worst Case  (Nlog(N)) Week 3: Lecture 6, 2006

16 Outer loop trace for Merge Sort a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]

17 Quick Sort: Average  (Nlog(N)) void quicksort(int a[], int l, int r) if (r > l){ v = a[r]; i = l-1; j = r; for (;;){ while (a[++i] < v); // move first i to right while (a[--j] > v) ; // then mover j left if (i >= j) break ; // ERROR HERE! swap(&a[i], &a[j]); } // swap i & j swap(&a[i], &a[r]); // move pivot in to center quicksort(a, l, i-1); quicksort(a, i+1, r); }

18 Outer loop trace for Quick Sort ( i moves before j ) a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] j i

19 Worst Case:  T(N) = T(N-1) + c N  T(N) =O(N 2 ) Best Case:  T(N) = 2 T(N/2) + cN  T(N) = O(N log(N)) Average Case:  T(N) = 2[T(0) + T(1) + … T(N-1) ]/N + c N  T(N) = O(N log (N))  Using Calculus if you are lazy! (x = N) See Weiss Sec 7.7.5

20 Proof of  (Nlog(N)) a, b, c a<b, c a< b,a<c c< a < b b< a < c b<c< a b < a,c<a c < b <a a< c < b a<b< c b < a, c a<b a<c b<c c< a b<cb<c Binary decisions: 3! = 6 possible outcomes. Longest path: log(3!) Week 4: Lecture 7, Sept 23, 2006 Decision Tree

21 Lower Bound Theorem for Camparision Sort Proof: Compute the maximum depth D of decision tree? Need N! leaves to get all possible outcomes of a sorting routine. Each level at most doubles: Consequently for D levels: Number of bits to encode any (initial) state is information ( - Entropy)

22 Shell Sort: void shellsort(int a[], int N) //Kunth 1969 int i, j, h, v; for (h = 1; h <= N/9; h = 3*h+1) ; // Find Largest h for ( ; h > 0; h = h/3) // Descending skip distance for (i = h; i < N; i++) { v = a[i]; // Insertion sort for ( j = i; (j>=h) && (a[j-h] > v) { j -= h; a[j] = a[j-h]; } a[j] = v; } Use insertion sort skip lists a[i] <a[i+h] in descending order

23 Shell’s sequence:  h = 1, 2, 4, 8, N  Worst Case: O(N 2 ) Hibbards seqence:  1, 3, 7,15, k –1  Average Case: O(N 5/4 ),  Worst: O(N 3/2 ) Theorem: A “h = p” sorted list remains p sorted after a “h =q” sort! (Proof is hard -- for me anyway) Properties of Shell Sort

24 Each sort finds at most one adjacent elment, a[(i-1) h], out order!  Each pass O(N) a[(i-5)h], a[(i-4)h], a[(i-3)h], a[(i-2)h], a[(i-1)h],a[i h] All other are (2 n + 3 m) h away! (e.g 5 = 2 + 3). The number of increments h’s smaller than O(N) is the area: O(log(N) log(N)). q.e.d. Cute increment : T(N) =  (N log 2 (N)) for Shell Sort! h-triangle: 2x/3x for left/right child  x 3x x = log 2 (N) and y = log 3 (N)  Area = x * y/2 =O(log(N)*log(N))

25 O(N): Bin, Bucket & Radix BIN Sort – make histogram:  N integers 0 < a[i] < M in the range v= 0,...,M-1.  Count number of occurrences in a[i] for(v=0; v<M; v++) bin[ v ] =0; for(i=0;i<N; i++) bin[a[i]] ++; j=0; for(v=0; v< M; v++) { for(i=0; i<bin[v]; i++) a[ j ] = v; j++; }  O(M + N) so if M » N it is O(N)

26 Bucket Sort Choose B Buckets as bins for high digits of a[i]  place N numbers in a[i] in buckets  Sort average of N/B elements in each bucket.  O( N + B*(N/B log(N/B) ) = O( N + N log(N/B)) B = O(N) Linked list: Bucket:

27 Radix Sort (IBM Card Sorter!) Represent integers in a[i] in base B: n 0 + n 1 B + n 2 B n p B P Sort into buckets by low digits first: n 0, then n 1, etc. O( N P ) where B P = M or P= log(M) / log(B) = O(1) Queues: B= Bucket: Example: 64, 8, 216, 512, 27, 729, 0, 1, 343, #1 #2 #3

28 Median Finding: Quick Select Median is the element a[m] so that half is less/equal Generalize to finding k-th smallest in set S Quick(S,k): |S| = size of S  If |S| = 1, the k = 1 in S  Pick pivot v 2 S & Partition S – {v} into S L & S H  If k< |S L | + 1 then k-th 2 S L : Quick(S L,k)  If k = |S L | + 1 k-th is v : exit  If k > |S L | + 1 then k-th 2 S R : Quick(S R,k- |S L |-1) Now: T(N) = O(N) is average performance T(N) = [T(0) + T(1) + … T(N-1) ]/N + c N

29 Can do better-- Worst Case O(N) ! Approximate Media Selector for pivot v:  Partition in 5 rows of N/5  Sort each column  Find (Exact) Medium of Middle list! Result pivot v is smaller than (3/5)(1/2)N elements K-th find is O(N) --- Double recursion!  Sort of N/5 col  Find media of T(N/5)  Find k-th in T(7N/10) at worst T(N) < K * (N/5) + T(N/5) + T(7N/10) + K’ * N  Try solution: T = C N  C(N – N/5 – 7N/10) = C N/10 = K N/5 + K’ N  C = 2 K + 10 K’

30 5 row of N/5 Columns X 3/5 * 1/2 N Boxes smaller than X Exact Medium of Middle Row larger