Algorithms and their Applications CS2004 (2012-2013) Professor Jasna Kuljis (adapted from Dr Steve Swift) 6.1 Classic Algorithms - Sorting.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

Analysis of Algorithms
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CSCE 3110 Data Structures & Algorithm Analysis
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 17 Sorting.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 24 Sorting.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
CPSC 171 Introduction to Computer Science More Efficiency of Algorithms.
Chapter 19: Searching and Sorting Algorithms
CS 171: Introduction to Computer Science II Quicksort.
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
Sorting Chapter 10.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Analysis of Algorithms CS 477/677
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
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.
Sorting HKOI Training Team (Advanced)
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
1 Lecture 16: Lists and vectors Binary search, Sorting.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
Elementary Sorting Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Analysis of Algorithms CS 477/677
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Sorting CS 105 See Chapter 14 of Horstmann text. Sorting Slide 2 The Sorting problem Input: a collection S of n elements that can be ordered Output: the.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
Sorting CS 110: Data Structures and Algorithms First Semester,
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.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Elementary Sorting 30 January Simple Sort // List is an array of size == n for (i = 1; i < n; i++) for (j = i+1; j List[j])
Data Structures and Algorithms Lecture 17, 18 and 19 (Sorting) Instructor: Quratulain Date: 10, 13 and 17 November, 2009 Faculty of Computer Science, IBA.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 25 Sorting.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Chapter 16: Searching, Sorting, and the vector Type.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Chapter 4, Part I Sorting Algorithms. 2 Chapter Outline Insertion sort Bubble sort Shellsort Radix sort Heapsort Merge sort Quicksort External polyphase.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 23 Sorting.
Advanced Sorting 7 2  9 4   2   4   7
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
CSCI 104 Sorting Algorithms
Algorithm Efficiency and Sorting
Algorithm design and Analysis
Bubble, Selection & Insertion sort
Ch 7: Quicksort Ming-Te Chi
Sorting Algorithms Ellysa N. Kosinaya.
Algorithm Efficiency and Sorting
Algorithms Sorting.
CS203 Lecture 15.
Algorithm Efficiency and Sorting
Presentation transcript:

Algorithms and their Applications CS2004 ( ) Professor Jasna Kuljis (adapted from Dr Steve Swift) 6.1 Classic Algorithms - Sorting

Previously On CS2004… So far we have looked at:  Concepts of Computation and Algorithms  Comparing algorithms  Some mathematical foundation  The Big-Oh notation  Computational Complexity  Last week we also looked at data structures Classic Algorithms - SortingSlide 2

A Note on Laboratory Worksheets ■ You should all have done the mathematics test at least once now ■ You should by now have worksheet four (4) signed off ■ Remember not to let these worksheets “pile up”  Do not leave any sheet longer than 4 weeks Classic Algorithms - SortingSlide 3

Classic Algorithms - Sorting ■ Within this lecture we are going to look at three sorting algorithms in detail  Bubble Sort  Quick Sort  Radix Sort ■ We will then look briefly at other sorting algorithms Classic Algorithms - SortingSlide 4

A Note of Growth Rates Classic Algorithms - SortingSlide 5 A plot of n against a number of possible T(n) The y axis is in logarithmic scale so that all the data can be viewed We are using log base 10, but usually log base 2 is used When n = 100 log(n) = 2, n = 100, nlog(n) = 200, n 2 = 10,000 and n 3 = 1,000,000

Why Bother With Sorting? ■ Sorting applications are one of the most common algorithms ■ There are implemented in  computer games, network routing software, operating systems, AI algorithms, bin packing algorithms, etc…. ■ The sorting problem itself is easily understood  It does not require a large amount of background knowledge before you can start implementing it ■ The algorithms are quite small and manageable  They can be implemented in a short period of time  The are relatively simple to analyse Classic Algorithms - SortingSlide 6

Formal Definition of Sorting ■ The sorting problem is a mapping from x to y, where  x and y are both n length real vectors (lists and/or arrays)  x is a permutation of y (different ordering)  y i < y i+1 for all i=0,...,n-1 ■ All of the algorithms will apply to whether we want the list sorted  from smallest to largest (ascending order), or  from largest to smallest (descending order) or ■ The algorithms can easily be applied to integers or character vectors  Sorting whole numbers (numerical order)  Sorting names and/or addresses (alphabetic order) Classic Algorithms - SortingSlide 7

Bubble Sort ■ Imagine that the items to be sorted are kept in an array held vertically.  The items with low values are “light” and bubble up to the top.  We make repeated passes over the array, from bottom to top. If two adjacent elements are out of order (the “lighter” one is below) we swap them. The effect of this operation is that on the first pass the “lightest” item rises all the way to the top. On the second pass, the second lowest key rises to the second position, and so on. Classic Algorithms - SortingSlide 8

Bubble Sort – Algorithm 1 INPUT: List X of n numbers (1) for i=0 to n-2 (2) for j=n-1 downto i (3) if x j < x j-1 then (4) Swap xj and xj-1 (5) End if (6) End if OUTPUT: List X sorted in ascending order Classic Algorithms - SortingSlide 9

Bubble Sort – Example Classic Algorithms - SortingSlide s s s s s s

Bubble Sort - Performance ■ How many comparisons do we need to make  in the first pass (placing the smallest element to the top)  in the second pass  in the last pass ■ The best case (the list is already sorted)  How many passes?  How many swaps? ■ The worst case (the list is sorted in the reverse order)  How many passes?  How many swaps? Classic Algorithms - SortingSlide 11

Bubble Sort: Summary ■ If we compare pairs of adjacent elements and none are out of order  the list is sorted  if any are out of order we must have to swap them to get an ordered list ■ Bubble sort will make passes though the list swapping any elements that are out of order ■ After the first pass, we know that  the smallest element must be in the correct place ■ After the second pass, we know that  the second smallest element must be in the correct place ■ Because of this, we can shorten each successive pass of the comparison loop Classic Algorithms - SortingSlide 12

Slide 13 Bubble Sort Input: X - a list of n numbers (1) Let NoSwaps = False (2) While NoSwaps = False (3) Let NoSwaps = True (4) For i = n-1 downto 0 (5) If x i-1 < x i then (6) Swap x i-1 and x i (7) Let NoSwaps = False (8) End If (9) End For (10) End While Output: X – sorted (ascending) The algorithm works by running through the list of numbers, swapping pairs that are out of order The algorithm terminates when no swaps need to be made Classic Algorithms - Sorting

Bubble Sort – Example Classic Algorithms - SortingSlide 14 swap 3 swaps swap 2 swaps No swaps

Bubble Sort: Best-Case Analysis ■ If the elements are in sorted order at the start,  the for loop will compare the adjacent pairs but will make no swaps ■ This means that the NoSwaps variable will remain true  The While loop is only done once ■ There are n-1 comparisons in the best case  B(n)  O(n) Classic Algorithms - SortingSlide 15

Bubble Sort: Worst –Case Analysis ■ Worst case  the while loop must be done as many times as possible  each pass of the For loop must make at least one swap of the elements ■ The number of comparisons will be: Classic Algorithms - SortingSlide 16

Bubble Sort: Average-Case Analysis (Part 1) ■ Much more difficult… ■ How many passes do we need? ■ We can potentially stop after any of the (at most) n-1 passes of the For loop ■ This means that we have n-1 possibilities ■ Our average case is given by Classic Algorithms - SortingSlide 17 where C ( i ) is the work done in the first i passes

Bubble Sort: Average-Case Analysis (Part 2) ■ On the first pass  we do n-1 comparisons ■ On the second pass  we do n-2 comparisons ■ The number of comparisons in the first i passes, in other words C(i), is given by: Classic Algorithms - SortingSlide 18

Bubble Sort: Average-Case Analysis (Part 3) ■ Putting the equation for C(i) into the equation for A(n) we get: Classic Algorithms - SortingSlide 19

Quick Sort ■ Quicksort is a divide and conquer algorithm ■ Quicksort picks an element from the list as the pivot, and partitions the list into two:  Those elements smaller than the pivot value (not necessarily in order)  Those elements larger than the pivot value (not necessarily in order) ■ Quicksort is then applied recursively on both sub-lists as long as they consist of more than 1 element Classic Algorithms - SortingSlide 20

Quick Sort: Example Pivot: Pivot: Pivot: Slide 21Classic Algorithms - Sorting

Quick Sort: Example pivot partition low high partition combine with pivot Slide 22Classic Algorithms - Sorting pivot

The Quick Sort Algorithm Recursive Algorithm QuickSort(List,First,Last) Input: List, the elements to be put into order First, the index of the first element Last, the index of the last element (1) If First < Last Then (2) Let Pivot = PivotList(List,First,Last) (3) Call QuickSort(List,First,Pivot-1) (4) Call QuickSort(List,Pivot+1,Last) (5) End If Output: List in a sorted order PivotList is defined later Slide 23Classic Algorithms - Sorting

The PivotList Algorithm PivotList(list,first,last) Input: List- the elements to be put into order First- the index of the first element Last- the index of the last element 1) PivotValue = list[first] 2) PivotPoint = first 3) For index = first+1 to last 4) If list[index] < PivotValue Then 5) PivotPoint=PivotPoint+1 6) Swap(list[PivotPoint],list[index]) 7) End if 8) End For 9) Swap(list[first],list[PivotPoint]) Output: PivotPoint pivot < pivot ≥ pivot unknown First Pivot pointIndexLast Slide 24Classic Algorithms - Sorting Pick the first element as the pivot Set the pivot point as the first location of the list Move through the list comparing the pivot element to the rest If an element is smaller i ncrease the pivot point Swap this element into the new pivot point location Move pivot value into correct place

Quick Sort: Worst-case Analysis ■ PivotList will do n-1 comparisons, but creates one partition that has n-1 elements and the other will have no elements  When will this happen? ■ Because we wind up just reducing the partition by one element each time, the worst case is given by: Classic Algorithms - SortingSlide 25

Quick Sort: Best-case Analysis ■ PivotList creates two parts that are the same size ■ And then all subsequent parts are the same size as the algorithm calls itself.  this can be modelled as a binary tree ■ Summing up over the partitions we get  B(n)=O(nLog 2 (n)) Classic Algorithms - SortingSlide 26

Quick Sort: Average-Case Analysis Part 1 ■ In the average case, we need to consider all of the possible places where the pivot point winds up ■ Because for one partition of the list, there are n-1 comparisons done, and there are n ways to partition the list, we have: Classic Algorithms - SortingSlide 27

Quick Sort: Average-Case Analysis Part 2 ■ Algebra can be used to simplify this recurrence relation to: ■ This will then solve to: ■ Therefore, A(n)  O(nLog 2 (n)) Classic Algorithms - SortingSlide 28

Comparison of Sorting Algorithms MethodBestAverageWorst Bubble sortO(n)O(n 2 ) Insertion sortO(n)O(n 2 ) Merge sortO(nlog 2 (n)) Quick sortO(nlog 2 (n)) O(n 2 ) Selection sortO(n 2 ) Classic Algorithms - SortingSlide 29

Radix Sort ■ The Radix sort method works only on binary or integer data ■ In a computer all numbers are represented as binary ■ Radix sort works by using a binary bucket sort for each binary digit ■ Integers in Java are stored using 32 bits Classic Algorithms - SortingSlide 30

Radix Sort – Example part 1 ■ Consider sorting the following list  331, 454, 230, 34, 343, 45, 59, 453, 345, 231, 9 ■ Apply the bucket sort on the last radix position ■ After being removed from the bucket, the elements are in the following order  230, 331, 231, 343, 453, 454, 34, 45, 345, 59, 9 ■ Apply the bucket sort on the second-to last last radix position, and the elements are put in the buckets as follows Classic Algorithms - SortingSlide [0][1][2][3][4][5][6][7][8][9]

Radix Sort – Example part 2 ■ After being removed from the bucket the elements are in the following order  9, 230, 331, 231, 34, 343, 45, 345, 453, 454, 59 ■ Apply the bucket sort on the third-to last last radix position, and the elements are put in the buckets as follows ■ After removal from the bucket the elements are in the following order  9, 34, 45, 59, 230, 231, 331, 343, 345, 453, 454 Classic Algorithms - SortingSlide [0][1][2][3][4][5][6][7][8][9] [0][1][2][3][4][5][6][7][8][9]

Radix Sort – Part 3 ■ This algorithm is applied on a list of binary numbers, a decade equivalent would be similar RadixSort(List,Bits) Input: List- the elements to be put into order Bits- the number of bits in each List[i] (1) For i = 0 to Bits-1 (2) Let b = 2 i (3) Bucket sort list on bit mask b (4) End If Output: List in a sorted order Slide 33Classic Algorithms - Sorting

Radix Sort – Part 4 ■ The Radix sort takes O(dn) time complexity  n is the number of items  d is the maximum number of radix positions  Best, worse and average case ■ Can be used for alphabetical sorting, i.e. strings ■ It is very, very fast! Classic Algorithms - SortingSlide 34

This Weeks Laboratory ■ This laboratory does contribute towards your overall grade… ■ You will be conducting some experiments which test three sorting algorithms Classic Algorithms - SortingSlide 35

Next Lecture ■ We will be looking at classic graph based algorithms ■ This will be in two weeks time ■ Next week is ASK week (effective learning week /reading week)  no lectures and no labs are scheduled in that week Classic Algorithms - SortingSlide 36