Sorting CMSC 201. Sorting In computer science, there is often more than one way to do something. Sorting is a good example of this!

Slides:



Advertisements
Similar presentations
Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali : 1.Start at the beginning of the data set. 2.Compare the first two elements,
Advertisements

Sorting I Chapter 8 Kruse and Ryba. Introduction Common problem: sort a list of values, starting from lowest to highest. –List of exam scores –Words of.
Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
CS 171: Introduction to Computer Science II Simple Sorting Ymir Vigfusson.
Analysis And Algorithms CMSC 201. Search Sometimes, we use the location of a piece of information in a list to store information. If I have the list [4,
CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann.
CMPS1371 Introduction to Computing for Engineers SORTING.
Sorting Algorithms What is it: An algorithm that puts the elements of a list in a certain order.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Searching and Sorting SLA Computer Science 4/16/08 Allison Mishkin.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Lecture 14: A Programming Example: Sorting Yoni Fridman 7/24/01 7/24/01.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Lists in Python.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Computer Science Searching & Sorting.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
Computer Science 101 Introduction to Sorting. Sorting One of the most common activities of a computer is sorting data Arrange data into numerical or alphabetical.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
Centroids part 2 Getting rid of outliers and sorting.
Sorts Tonga Institute of Higher Education. Introduction - 1 Sorting – The act of ordering data Often, we need to order data.  Example: Order a list of.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
New Mexico Computer Science For All Sorting Algorithms Maureen Psaila-Dombrowski.
3 – SIMPLE SORTING ALGORITHMS
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Programming Abstractions Cynthia Lee CS106X. Today’s Topics Sorting! 1.The warm-ups  Selection sort  Insertion sort 2.Let’s use a data structure! 
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Intro To Algorithms Searching and Sorting. Searching A common task for a computer is to find a block of data A common task for a computer is to find a.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Sorting Algorithms Written by J.J. Shepherd. Sorting Review For each one of these sorting problems we are assuming ascending order so smallest to largest.
Aims: To learn about some simple sorting algorithms. To develop understanding of the importance of efficient algorithms. Objectives: All:Understand how.
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…
Lists and Sorting Algorithms
Sort Algorithm.
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Sorting.
Searching and Sorting Algorithms
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Last Class We Covered Sorting algorithms Searching algorithms
Algorithm Efficiency and Sorting
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
Design and Analysis of Algorithms
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Data Structures and Analysis (COMP 410)
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Sorting Algorithms Written by J.J. Shepherd.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Last Class We Covered Data representation Binary numbers ASCII values
Selection Sort Sorted Unsorted Swap
Quicksort analysis Bubble sort
Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
IT 4043 Data Structures and Algorithms
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Quadratic Sorts & Breaking the O(n2) Barrier
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
Presentation transcript:

Sorting CMSC 201

Sorting In computer science, there is often more than one way to do something. Sorting is a good example of this!

Sorting Here is a simple way of sorting a list: Find the smallest number in a list. Move that to the end of a new list. Repeat until the original list is empty. This is called selection sort!

Analysis What is the big O of finding the lowest number in a list (for a list of size N, what is the worst case number of elements you’d have to look through to find the min?) For a list of size N, how many times would we have to find the min to sort the list? What is the big O of this sorting algorithm?

Analysis What is the big O of finding the lowest number in a list (for a list of size N, what is the worst case number of elements you’d have to look through to find the min?) N For a list of size N, how many times would we have to find the min to sort the list? N What is the big O of this sorting algorithm? O(N 2 )

Bubble Sort Let’s think of some other options! How about this: We look at the first pair of items in the list, and if the first one is bigger than the second one, we swap them. Then we look at the second and third one and put them in order, and so on. Once we hit the end of the list, we start over at the beginning. We keep it up until the list is sorted!

Bubble Sort [ 4, 8, 1, 10, 13, 14, 6] First pass: 4 and 8 are in order 8 and 1 should be swapped: [ 4, 1, 8, 10, 13, 14, 6] 8 and 10 are in order. 10 and 13 are in order. 13 and 14 are in order. 6 and 14 should be swapped. [ 4, 1, 8, 10, 13, 6, 14]

Bubble Sort [ 4, 1, 8, 10, 13, 6, 14] Second pass: 4 and 1 should be swapped: [ 1, 4, 8, 10, 13, 6, 14] 4 and 8 are in order. 8 and 10 are in order. 10 and 13 are in order. 13 and 6 should be swapped: [ 1, 4, 8, 10, 6, 13, 14] 13 and 14 are in order.

Bubble Sort [ 4, 1, 8, 10, 6, 13, 14] It will take two more passes over the whole list to get the six in place.

Analysis For a list of size N, how much work do we do for a single pass? How may passes will we have to do? What is the big O of bubble sort?

Analysis For a list of size N, how much work do we do for a single pass? N How may passes will we have to do? N What is the big O of bubble sort? O(N 2)

Quicksort Quicksort: Start with the number on the far right. Put everything less than that number on the left of it and everything greater than it on the right of it. Quicksort the left side and the right side.

Analysis For a list of size N, how many steps does it take to move everything less than the last number to the left and everything greater than the last number to the right? How many times with the algorithm divide the list in half? What is the big O?

Analysis For a list of size N, how many steps does it take to move everything less than the last number to the left and everything greater than the last number to the right? N How many times with the algorithm divide the list in half? lg(N) What is the big O? O(Nlg(N))

Radix Sort Most of the time, O(nlg(n)) is the best we can do for sorting. However if we make the problem slightly easier, we can do even better! Imagine we know for a fact that the list we are sorting is only integers between 0 and 10.

Radix We can make an empty list filled with 10 zeroes. The first element of this list represents the number of zeroes we’ve seen so far in the list we’re sorting. The second number is the number of ones we’ve seen, and so far. So say we have the list: [0, 3, 2, 1, 6, 8] We make our counting list: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] And iterate over the list we want to sort. The first number is a zero, so we add one to the zeroth element of our counting list: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Radix [0, 3, 2, 1, 6, 8] The next number is a 3, so we add one to the third element of our counting list: [1, 0, 0, 1, 0, 0, 0, 0, 0, 0] Then 2: [1, 0, 1, 1, 0, 0, 0, 0, 0, 0] Then 1: [1, 1, 0, 1, 0, 0, 0, 0, 0, 0]

Radix [0, 3, 2, 1, 6, 8] When we’re done, the list looks like this: [1, 1, 1, 1, 0, 0, 1, 0, 1, 0] For an index i, we know if countList[i] == 1, there was one i in the original list. One pass over the counting list to figure out which numbers were there and we’ve sorted it!

Radix We do N operations to put the zeroes in the counting list, N operations to fill the counting list, and N operations to reconstruct the sorted list. Which gives us 3N operations, which is O(N)!