Sorting: why?  We do it A LOT!  Makes finding the largest and smallest value easier  Makes finding how many of a certain value are in a list easier.

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Searching and Sorting Algorithms Based on D. S
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
Sorting Algorithms and Average Case Time Complexity
CMPS1371 Introduction to Computing for Engineers SORTING.
Chapter 19: Searching and Sorting Algorithms
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
 1 Sorting. For computer, sorting is the process of ordering data. [ ]  [ ] [ “Tom”, “Michael”, “Betty” ]  [ “Betty”, “Michael”,
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Recitation 9 Programming for Engineers in Python.
1 Sorting/Searching CS308 Data Structures. 2 Sorting means... l Sorting rearranges the elements into either ascending or descending order within the array.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
CHAPTER 11 Sorting.
C++ Plus Data Structures
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
(c) , University of Washington
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
1 Lecture 16: Lists and vectors Binary search, Sorting.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
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.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
1 CSE 326: Data Structures A Sort of Detour Henry Kautz Winter Quarter 2002.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting 1. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step.
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.
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])
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.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Sorting and Searching Algorithms CS Sorting means... l The values stored in an array have keys of a type for which the relational operators are.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
CPS120: Introduction to Computer Science Sorting.
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.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Data Structures and Algorithms Instructor: Tesfaye Guta [M.Sc.] Haramaya University.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Searching and Sorting Searching algorithms with simple arrays
Sorting Mr. Jacobs.
Week 9 - Monday CS 113.
CSCI 104 Sorting Algorithms
Design and Analysis of Algorithms
Chapter 13: Searching and Sorting
Divide and Conquer.
Adapted from slides by Marty Stepp and Stuart Reges
Sorting means The values stored in an array have keys of a type for which the relational operators are defined. (We also assume unique keys.) Sorting.
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.
CSc 110, Spring 2017 Lecture 39: searching.
Adapted from slides by Marty Stepp and Stuart Reges
C++ Plus Data Structures
24 Searching and Sorting.
Sub-Quadratic Sorting Algorithms
Searching and Sorting Arrays
CS 1114: Sorting and selection (part two)
Chapter 10 Sorting Algorithms
Workshop for CS-AP Teachers
Presentation transcript:

Sorting: why?  We do it A LOT!  Makes finding the largest and smallest value easier  Makes finding how many of a certain value are in a list easier  Using binary search, makes finding anything much quicker  Plus it’s a nice way to show differences in efficiency in algorithms

First: SelectionSort 1. Idea: Go through and find the smallest value in a list. 2. Switch it with the first value on the list  Or remove it and append it to a new list.  Repeat steps 1 and 2 until the entire list is sorted  This algorithm is very easy to conceive of and to implement, but it isn’t terribly efficient.  For every value we put in place, we have to go through the whole (rest of the) list and find the smallest value.  We have to do this for every value.  Thus analysis of running time is estimated to be n 2 (assuming there are n values in the list)

def SelectionSort(ls): for i in range(len(ls)): s=ls[i] si = i for j in range(i, len(ls)): if (ls[j]<s): s=ls[j] si=j ls[si]=ls[i] ls[i]=s return ls a=[3,5,2,7,1] print (a) print ("=>", SelectionSort(a)) Analysis? a = [1,2,3,4,5] a = [5,4,3,2,1]

Next: BubbleSort 1. Idea: compare the first and second number in the list. If the first is greater than the second, switch them (this is known as “bubbling up”). 2. Compare the (possibly new) second and third number in the list. If the second is greater than the third, switch them. 3. Compare the third and fourth numbers in the list. If the third is greater than the fourth, switch them. 4. Continue like this to the end of the list.  Repeat steps 1-4 again and again, until there are no more switches (bubbles). Analysis : Worst case: n 2 (with n being the length of the list)  Best case: when the list is already sorted  Then you only do steps time, so analysis is only n  This algorithm is a bit more efficient than selection sort, but also just a bit more difficult to write in code (not much!)

def bubblesort(list2): i = 0; swap_test = False while ((i < len(list2)-1) and (swap_test == False)): swap_test = True for j in range(0, len(list2) - i - 1): if list2[j] > list2[j + 1]: list2[j], list2[j + 1] = list2[j + 1], list2[j] swap_test = False i += 1 return(list2) ls = [3,2,4,7,1] print(bubblesort(ls))

Next: InsertionSort Idea: Always inserting into an already sorted portion of the list 1. Take the second card in a list and compare it to the first card. Make sure the first two cards are in order by possibly switching the first and second card. 2. Take the third card and compare it to the second card and possibly the first card, placing it into the correct order so the first, second, and third card are sorted.  Repeat steps 1-3 with each card in the list until the entire list is sorted. Analysis: Worst case: n 2 (with n being the length of the list)  Best case: when the list is already sorted  Then you do one comparison for each number in the list, so analysis is only n  In terms of efficiency, about the same as bubblesort

def insertionsort(ls): for i in range(0,len(ls)): x = ls[i] j = i-1 while (j >= 0) and (ls[j] > x): ls[j+1] = ls[j] j = j-1 ls[j+1] = x return(ls) ls=[3,1,2,6,4,7] insertionsort(ls) print(ls) Analysis? a = [1,2,3,4,5] a = [5,4,3,2,1]

QuickSort  Idea: select a pivot value in the list  All values less than the pivot go in the list below the pivot.  All values greater than the pivot go in the list above the pivot  Pivot is now in place.  Repeat (recursively) with the lesser list and the greater list. Analysis: Average case: nlog 2 n (with n being the length of the list)  Each time we’re dividing the list in half (hopefully)  So we compare the full list, then half the list, then ¼ the list, then 1/8 the list, etc.

QuickSort def main( aList ): quicksort( aList, 0, len( aList ) - 1 ) def quicksort( aList, first, last ): if first < last: pivot = partition( aList, first, last ) quicksort( aList, first, pivot - 1 ) quicksort( aList, pivot + 1, last ) def partition( aList, first, last ) : pivot = first + random.randrange( last - first + 1 ) swap( aList, pivot, last ) for i in range( first, last ): if aList[i] <= aList[last]: swap( aList, i, first ) first += 1 swap( aList, first, last ) return first def swap( A, x, y ): #could have just said, A[x],A[y] = A[y],A[x] tmp = A[x] A[x] = A[y] A[y] = tmp ls = [32,41,33,12,8,3,24,15,97,54,36,26,71,51,32,0,6,2] main(ls) print(ls)

Next: MergeSort  Idea: pretend each number in the list is its own list. 1. Merge each neighboring list into a list of two sorted elements. 2. Merge each sorted list of two elements into sorted lists of 4 elements. 3. Merge each sorted list of 4 elements into sorted lists of 8 elements. 4. Continue to merge neighboring lists until there’s only one list left. Analysis: Worst case: nlog 2 n (with n being the length of the list)  Each time we’re merging half the list. So we’re doing log 2 n merges.  We may need to compare each number when we merge, so that’s the n  This is the most efficient of the sorting algorithms we’ve seen!

MergeSort in Python: def merge(left, right): result = [] i,j = 0, 0 while i < len(left) and j < len(right): if left[i] <= right[j]: result.append(left[i]) i += 1 else: result.append(right[j]) j += 1 result += left[i:] result += right[j:] return result def mergesort(lista): if len(lista) <=1: return lista else: middle = len(lista) / 2 #keep dividing left side into 2 #lists half the size of the original left = mergesort(lista[:middle]) #keep dividing right side into 2 #lists half the size of the original right = mergesort(lista[middle:]) #now we have a bunch of small lists #that we need to keep merging #back into larger, sorted lists return merge(left, right) listb = [8,2,3,1,9,5,7,0,4,6] print(mergesort(listb))