Fundamentals of Programming I Sort Algorithms

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

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
Garfield AP Computer Science
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Chapter 9: Searching, Sorting, and Algorithm Analysis
Visual C++ Programming: Concepts and Projects
Computer Science 112 Fundamentals of Programming II Finding Faster Algorithms.
CSE 373: Data Structures and Algorithms
C++ Plus Data Structures
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )
Searching and Sorting Arrays
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
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.
Sorting. Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
12. Sorting Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Sorting Many computer applications.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
Lists in Python Selection Sort Algorithm. Sorting A very common activity for computers Not just in business, sorting is used in databases, graphics, simulations,
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Computer Science 112 Fundamentals of Programming II Searching, Sorting, and Complexity Analysis.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Searching and Sorting Searching algorithms with simple arrays
Sort Algorithm.
Searching and Sorting Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
Week 9 - Monday CS 113.
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Computer Science 112 Fundamentals of Programming II
CSc 110, Autumn 2017 Lecture 37: searching and sorting
Design and Analysis of Algorithms
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Algorithm Efficiency and Sorting
Algorithm design and Analysis
Objectives At the end of the class, students are expected to be able to do the following: Understand the purpose of sorting technique as operations on.
Describing algorithms in pseudo code
Selection Sort Sorted Unsorted Swap
And now for something completely different . . .
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Searching and Sorting Topics Sequential Search on an Unordered File
CSc 110, Spring 2017 Lecture 39: searching and sorting
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
Sorting.
Standard Version of Starting Out with C++, 4th Edition
C++ Plus Data Structures
Searching and Sorting Topics Sequential Search on an Unordered File
Fundamentals of Programming I Sort Algorithms
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
Searching and Sorting Arrays
Review of Searching and Sorting Algorithms
Searching and Sorting Arrays
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

Fundamentals of Programming I Sort Algorithms Computer Science 111 Fundamentals of Programming I Sort Algorithms

Sorting One of the most fundamental and widespread ways of organizing information Can be a very costly process (time performance), so much ingenuity is devoted to the development of sorting strategies

Sorting 89 56 63 72 41 34 95 0 1 2 3 4 5 6 Puts the elements of a list into some kind of order, usually ascending Elements must be comparable using the operators <, >, and == 34 41 56 63 72 89 95 0 1 2 3 4 5 6

Simple Sorting The sort() method modifies a list by sorting its elements into ascending order The elements must be comparable using the operators <, >, and == Returns None

Examples >>> names = ['Hillary', 'Donald', 'Tim', 'Mike'] >>> names.sort() >>> names ['Donald', 'Hillary', 'Mike', 'Tim'] >>> numbers = [88, 44, 99, 33, 44, 22] >>> numbers.sort() >>> numbers [22, 33, 44, 44, 88, 99]

How Does sort Work? sort uses a fast but very sophisticated algorithm Let’s examine two simpler but slower algorithms called selection sort and bubble sort There is often a tradeoff between the complexity of an algorithm and its performance

Bubble Sort Strategy Compare the first two elements and if they are out of order, exchange them Repeat this process for the second and third elements, etc. At the end of this process, the largest element will have bubbled down to the end of the list Repeat this process for the unsorted portion of the list, etc.

Formalize the Strategy set n to the length of the list while n > 1 bubble the elements from position 0 to position n - 1 decrement n

Formalize the Strategy set n to the length of the list while n > 1 for each position i from 1 to n - 1 if the elements at i and i - 1 are out of order exchange them decrement n

Define bubbleSort def bubbleSort(lyst): n = len(lyst) while n > 1: # Do n - 1 bubbles n -= 1

Define bubbleSort def bubbleSort(lyst): n = len(lyst) while n > 1: # Do n - 1 bubbles i = 1 # Start each bubble while i < n: i += 1 n -= 1

Define bubbleSort def bubbleSort(lyst): n = len(lyst) while n > 1: # Do n - 1 bubbles i = 1 # Start each bubble while i < n: if lyst[i] < lyst[i - 1]: # Exchange if needed temp = lyst[i] lyst[i] = lyst[i - 1] lyst[i - 1] = temp i += 1 n -= 1

Add a Trace Option def bubbleSort(lyst, traceOn = False): n = len(lyst) while n > 1: # Do n - 1 bubbles i = 1 # Start each bubble while i < n: if lyst[i] < lyst[i - 1]: # Exchange if needed temp = lyst[i] lyst[i] = lyst[i - 1] lyst[i - 1] = temp if traceOn: print(lyst) i += 1 n -= 1

A Tester Function def testSort(sortFunction, size = 1, traceOn = False): lyst = range(1, size + 1) random.shuffle(lyst) print(lyst) sortFunction(lyst, traceOn)

A Tester Function def testSort(sortFunction, size = 1, traceOn = False): lyst = range(1, size + 1) random.shuffle(lyst) print(lyst) sortFunction(lyst, traceOn) >>> testSort(sortFunction = bubbleSort, size = 5) [1, 4, 5, 3, 2] [1, 2, 3, 4, 5]

A Tester Function def testSort(sortFunction, size = 1, traceOn = False): lyst = range(1, size + 1) random.shuffle(lyst) print(lyst) sortFunction(lyst, traceOn) >>> testSort(sortFunction = bubbleSort, size = 5) [1, 4, 5, 3, 2] [1, 2, 3, 4, 5] >>> testSort(sortFunction = bubbleSort, size = 5, traceOn = True) [1, 4, 2, 5, 3] [1, 2, 4, 5, 3] [1, 2, 4, 3, 5]

Selection Sort Strategy Search for the smallest element in the list Exchange that element with the element in the first position Repeat this process with the rest of the list after the first position, etc.

Formalize the Strategy for each position i in a list of length n select the smallest element from positions i to n - 1 exchange that element with the ith element

Define selectionSort def selectionSort(lyst): i = 0 while i < len(lyst) - 1: # Do n - 1 searches i += 1

Define selectionSort def selectionSort(lyst): i = 0 while i < len(lyst) - 1: # Do n - 1 searches minPos = i # Start each search probe = i + 1 while probe < len(lyst): if lyst[probe] < lyst[minPos]: minPos = probe probe += 1 i += 1

Define selectionSort def selectionSort(lyst): i = 0 while i < len(lyst) - 1: # Do n - 1 searches minPos = i # Start each search probe = i + 1 while probe < len(lyst): if lyst[probe] < lyst[minPos]: minPos = probe probe += 1 if minPos != i: # Exchange if needed temp = lyst[i] lyst[i] = lyst[minPos] lyst[minPos] = temp i += 1

Add traceOn def selectionSort(lyst, traceOn = False): i = 0 while i < len(lyst) - 1: # Do n - 1 searches minPos = i # Start each search probe = i + 1 while probe < len(lyst): if lyst[probe] < lyst[minPos]: minPos = probe probe += 1 if minPos != i: # Exchange if needed temp = lyst[i] lyst[i] = lyst[minPos] lyst[minPos] = temp if traceOn: print(lyst) i += 1

Testing selectionSort >>> testSort(selectionSort, 5, True) [4, 5, 1, 2, 3] [1, 5, 4, 2, 3] [1, 2, 4, 5, 3] [1, 2, 3, 5, 4] [1, 2, 3, 4, 5] >>> selectionSort([1,2,3,4], traceOn = True) >>>

Using Sort Criteria We might want to sort elements into descending order We might want to sort them by a criterion, such as account name, account balance, etc. We can use the same strategy that we employed in searching: package the comparison as a specialized function and pass it as an extra argument to the sort function

Simple bubbleSort The default test uses the < operator def bubbleSort(lyst): n = len(lyst) while n > 1: i = 1 while i < n: if lyst[i] < lyst[i - 1]: temp = lyst[i] lyst[i] = lyst[i - 1] lyst[i - 1] = temp i += 1 n -= 1 The default test uses the < operator

Generic bubbleSort Pass the test as a function argument def bubbleSort(lyst, test): n = len(lyst) while n > 1: i = 1 while i < n: if test(lyst[i], lyst[i - 1]): temp = lyst[i] lyst[i] = lyst[i - 1] lyst[i - 1] = temp i += 1 n -= 1 Pass the test as a function argument

Generic bubbleSort def bubbleSort(lyst, test = lambda x, y: x < y): n = len(lyst) while n > 1: i = 1 while i < n: if test(lyst[i], lyst[i - 1]): temp = lyst[i] lyst[i] = lyst[i - 1] lyst[i - 1] = temp i += 1 n -= 1 Better still, retain the default by using a lambda function

Generic bubbleSort def bubbleSort(lyst, test = lambda x, y: x < y): n = len(lyst) while n > 1: i = 1 while i < n: if test(lyst[i], lyst[i - 1]): temp = lyst[i] lyst[i] = lyst[i - 1] lyst[i - 1] = temp i += 1 n -= 1 >>> bubbleSort(lyst) # Default order >>> bubbleSort(lyst, lambda x, y: x > y) # Descending order

Generic bubbleSort By balance, from highest to lowest def bubbleSort(lyst, test = lambda x, y: x < y): n = len(lyst) while n > 1: i = 1 while i < n: if test(lyst[i], lyst[i - 1]): temp = lyst[i] lyst[i] = lyst[i - 1] lyst[i - 1] = temp i += 1 n -= 1 >>> bubbleSort(accounts, lambda x, y: x.getBalance() > y.getBalance()) By balance, from highest to lowest

Generic bubbleSort By name, alphabetically def bubbleSort(lyst, test = lambda x, y: x < y): n = len(lyst) while n > 1: i = 1 while i < n: if test(lyst[i], lyst[i - 1]): temp = lyst[i] lyst[i] = lyst[i - 1] lyst[i - 1] = temp i += 1 n -= 1 >>> bubbleSort(accounts, lambda x, y: x.getName() < y.getName()) By name, alphabetically

Performance of Sorts Bubble sort and selection sort perform adequately for small data sets (list length < 10000) Faster but more sophisticated sort algorithms should be used with larger data sets Many of these can be simplified by using recursive methods, which we examine next term

Networks and client/server programming Chapter 10 For Monday Networks and client/server programming Chapter 10