1 Computer Algorithms Lecture 8 Sorting Algorithms Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

CSE 3101: Introduction to the Design and Analysis of Algorithms
Counting Sort Non-comparison sort. Precondition: n numbers in the range 1..k. Key ideas: For each x count the number C(x) of elements ≤ x Insert x at output.
Lower bound for sorting, radix sort COMP171 Fall 2005.
Sorting. Sorting Considerations We consider sorting a list of records, either into ascending or descending order, based upon the value of some field of.
CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis.
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.
Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Comp 122, Spring 2004 Elementary Sorting Algorithms.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Merge sort, Insertion sort
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) Jan Maluszynski - HT Sorting: –Intro: aspects of sorting, different strategies –Insertion.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
Computer Programming Sorting and Sorting Algorithms 1.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort or bubble sort 1. Find the minimum value in the list 2. Swap it with the value.
Analysis of Algorithms CS 477/677
CSE 373 Data Structures Lecture 19
Data Structures/ Algorithms and Generic Programming Sorting Algorithms.
Computer Algorithms Lecture 11 Sorting in Linear Time Ch. 8
Sorting in Linear Time Lower bound for comparison-based sorting
CSE 373 Data Structures Lecture 15
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Elementary Sorting Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Sorting Fun1 Chapter 4: Sorting     29  9.
Analysis of Algorithms CS 477/677
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
1 Data Structures and Algorithms Sorting I Gal A. Kaminka Computer Science Department.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort (iterative, recursive?) * Bubble sort.
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
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.
SORTING 2014-T2 Lecture 13 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
Sorting CSIT 402 Data Structures II. 2 Sorting (Ascending Order) Input ›an array A of data records ›a key value in each data record ›a comparison function.
Algorithms IS 320 Spring 2015 Sorting. 2 The Sorting Problem Input: –A sequence of n numbers a 1, a 2,..., a n Output: –A permutation (reordering) a 1.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Sorting.
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,
Introduction to Algorithms Sorting and Order Statistics– Part II Lecture 5 CIS 670.
1 Algorithms CSCI 235, Fall 2015 Lecture 11 Elementary Sorts.
Lecture 4 1 Advance Analysis of Algorithms. Selection Sort 2 Summary of Steps Find the smallest element in the array Exchange it with the element in the.
Static block can be used to check conditions before execution of main begin, Suppose we have developed an application which runs only on Windows operating.
CSE 326: Data Structures Lecture 23 Spring Quarter 2001 Sorting, Part 1 David Kaplan
Today’s Material Sorting: Definitions Basic Sorting Algorithms
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort.
Sorting: Implementation Fundamental Data Structures and Algorithms Margaret Reid-Miller 24 February 2004.
Sorting. 2 The Sorting Problem Input: A sequence of n numbers a 1, a 2,..., a n Output: A permutation (reordering) a 1 ’, a 2 ’,..., a n ’ of the input.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4.
Algorithms Sorting – Part 3.
Sorting.
Analysis of Algorithms CS 477/677
Sorting by Tammy Bailey
Analysis of Algorithms CS 477/677
Bubble, Selection & Insertion sort
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Elementary Sorting Algorithms
Analysis of Algorithms
Algorithms Sorting.
Presentation transcript:

1 Computer Algorithms Lecture 8 Sorting Algorithms Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR

The Sorting Problem Input: –A sequence of n numbers a 1, a 2,..., a n Output: –A permutation (reordering) a 1 ’, a 2 ’,..., a n ’ of the input sequence such that a 1 ’ ≤ a 2 ’ ≤ · · · ≤ a n ’

Why Study Sorting Algorithms? There are a variety of situations that we can encounter –Do we have randomly ordered keys? –Are all keys distinct? –How large is the set of keys to be ordered? –Need guaranteed performance? Various algorithms are better suited to some of these situations

Sorting – Definitions Sorting: determine a permutation  = (p 1, …, p n ) of n records that puts the keys in non-decreasing order K p 1 < … < K p n. Permutation: a one-to-one function from {1, …, n} onto itself. There are n! distinct permutations of n items. Rank: Given a collection of n keys, the rank of a key is the number of keys that precede it. That is, rank(K j ) = |{K i | K i < K j }|. If the keys are distinct, then the rank of a key gives its position in the output file.

Sorting Terminology Internal (the file is stored in main memory and can be randomly accessed) vs. External (the file is stored in secondary memory & can be accessed sequentially only) Comparison-based sort: uses only the relation among keys, not any special property of the representation of the keys themselves. Stable sort: records with equal keys retain their original relative order; i.e., i < j & Kp i = Kp j  p i < p j (next slide) Array-based (consecutive keys are stored in consecutive memory locations) vs. List-based sort (may be stored in nonconsecutive locations in a linked manner) In-place sort: needs only a constant amount of extra space in addition to that needed to store keys.

Stability A STABLE sort preserves relative order of records with equal keys Sort file on first key: Sort file on second key: Records with key value 3 are not in order on first key!! 6

Sorting Categories Sorting by Insertion insertion sort Sorting by Exchange bubble sort, quicksort Sorting by Selection selection sort, heapsort Sorting by Merging merge sort Sorting by Distribution radix sort

Elementary Sorting Methods Easier to understand the basic mechanisms of sorting. Good for small files. Good for well-structured files that are relatively easy to sort, such as those almost sorted. Can be used to improve efficiency of more powerful methods.

Selection Sort Selection-Sort(A, n) 1. for i = n downto 2 do 2. max  i 3. for j = i – 1 downto 1 do 4. if A[max] < A[j] then 5. max  j 6. t  A[max] 7. A[max]  A[i] 8. A[i]  t Selection-Sort(A, n) 1. for i = n downto 2 do 2. max  i 3. for j = i – 1 downto 1 do 4. if A[max] < A[j] then 5. max  j 6. t  A[max] 7. A[max]  A[i] 8. A[i]  t

Insertion Sort InsertionSort(A, n) 1. for j = 2 to n do 2. key  A[j] 3. i  j – 1 4. while i > 0 and key < A[i] 5. A[i+1]  A[i] 6. i  i – 1 7. A[i+1]  key InsertionSort(A, n) 1. for j = 2 to n do 2. key  A[j] 3. i  j – 1 4. while i > 0 and key < A[i] 5. A[i+1]  A[i] 6. i  i – 1 7. A[i+1]  key

Worst-case Analysis The maximum number of comparisons while inserting A[i] is (i-1). So, the number of comparisons is C wc (n)   i = 2 to n (i -1) =  j = 1 to n-1 j = n(n-1)/2 =  (n 2 ) For which input does insertion sort perform n(n-1)/2 comparisons?

Bubble Sort (another elementary sorting algorithm) Idea: –Repeatedly pass through the array –Swaps adjacent elements that are out of order Easier to implement, but slower than Insertion sort 123n i j

Bubble Sort Alg.: BUBBLESORT(A) for i  1 to length[A] do for j  length[A] downto i + 1 do if A[j] < A[j -1] then exchange A[j]  A[j-1] i = 1j i

Example i = 1j j j j j j j i = 2j i = 3j i = 4j i = 5j i = 6j i = 7 j 14

Bubble Sort Alg.: BUBBLESORT(A) for i  1 to length[A] do for j  length[A] downto i + 1 do if A[j] < A[j -1] then exchange A[j]  A[j-1] i = 1j i

Bubble-Sort Running Time T(n) =  (n 2 ) Alg.: BUBBLESORT(A) for i  1 to length[A] do for j  length[A] downto i + 1 do if A[j] < A[j -1] then exchange A[j]  A[j-1] T(n) =c 1 (n+1) +c2c2 c3c3 c4c4 =  (n) + (c 2 + c 3 + c 4 ) Comparisons:  n 2 /2 Exchanges:  n 2 /2 16

Merge Sort Alg.: MERGE-SORT (A, p, r) if p < r Check for base case then q ←  (p + r)/2  Divide MERGE-SORT (A, p, q) Conquer MERGE-SORT (A, q + 1, r) Conquer MERGE (A, p, q, r) Combine Initial call: MERGE-SORT (A, 1, n) p r q

Sorting Insertion sort –Design approach: –Sorts in place: –Best case: –Worst case: – n 2 comparisons, n 2 exchanges Bubble Sort –Design approach: –Sorts in place: –Running time: – n 2 comparisons, n 2 exchanges Yes  (n)  (n 2 ) incremental Yes  (n 2 ) incremental

Sorting Selection sort –Design approach: –Sorts in place: –Running time: – n 2 comparisons, n exchanges Merge Sort –Design approach: –Sorts in place: –Running time: Yes  (n 2 ) incremental No divide and conquer  (n lgn)

Sorting Files with Huge Records and Small Keys Insertion sort or bubble sort? –NO, too many exchanges Selection sort? –YES, it takes linear time for exchanges Merge sort? –Probably not: needs extra space Example application: Reorganize your MP-3 files

Sorting Huge, Randomly - Ordered Files Selection sort? –NO, always takes quadratic time Bubble sort? –NO, always takes quadratic time Insertion sort? –NO, quadratic time for randomly-ordered keys Merge sort? –YES, it is designed for this problem Application: Process transaction record for a phone company

Sorting Files That are Almost in Order Selection sort? –NO, always takes quadratic time Bubble sort? –NO, always takes quadratic time Insertion sort? –YES, takes linear time for most definitions of “almost in order” Merge sort? –Probably not: takes nlgn Applications: Re-sort a huge database after a few changes Double-check that someone else sorted a file