Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed http://informationtechnology.pk/pucit abdul.hameed@pucit.edu.pk.

Slides:



Advertisements
Similar presentations
Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 Bubblesort compares the numbers in pairs from left to right exchanging.
Advertisements

Greedy Algorithms.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture22.
Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
CSE115/ENGR160 Discrete Mathematics 02/28/12
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Algorithm Efficiency and Sorting
Analysis of Algorithms CS 477/677
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting CS 202 – Fundamental Structures of Computer Science II Bilkent.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Data Structures/ Algorithms and Generic Programming Sorting Algorithms.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
By: Vishal Kumar Arora AP,CSE Department, Shaheed Bhagat Singh State Technical Campus, Ferozepur. Different types of Sorting Techniques used in Data Structures.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
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.
Jessie Zhao Course page: 1.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
The Fundamentals: Algorithms, the Integers & Matrices.
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Comparison-Based Sorting & Analysis Smt Genap
Chapter Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors.
Section 3.1. Section Summary Properties of Algorithms Algorithms for Searching and Sorting Greedy Algorithms Halting Problem.
Example { 1 – –1} Use Bubble Sort (sort in increasing order} After first pass { –1 50} After Second Pass { } After.
CS 103 Discrete Structures Lecture 12
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.
Bubble Sort Example
ALGORITHMS.
Sorting Algorithm Analysis. Sorting  Sorting is important!  Things that would be much more difficult without sorting: –finding a phone number in the.
SORTING ALGORITHMS King Saud University College of Applied studies and Community Service CSC 1101 By: Nada Alhirabi 1.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
CS1022 Computer Programming & Principles Lecture 2.2 Algorithms.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Sorting  Selection Sort  Bubble Sort  Insertion Sort  Merge Sort (chap. 14)  Quick Sort (chap. 14)  Heap Sort (chap. 9)
Greedy Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Sort Algorithm.
CSE15 Discrete Mathematics 03/06/17
Introduction to Sorting
Sorting With Priority Queue In-place Extra O(N) space
May 17th – Comparison Sorts
The Greedy Method and Text Compression
Algorithms and Data Structures
CS 2210 Discrete Structures Algorithms and Complexity
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.
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.
Analysis of Algorithms CS 477/677
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.
Sorting Algorithms IT12112 Lecture 07.
Bubble, Selection & Insertion sort
8/04/2009 Many thanks to David Sun for some of the included slides!
Greedy Algorithms.
Greedy Algorithms TOPICS Greedy Strategy Activity Selection
Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 Bubblesort compares the numbers in pairs from left to right exchanging.
Algorithms and Data Structures
Analysis of Algorithms
CSCE 222 Discrete Structures for Computing
CENG 351 Data Management and File Structures
Analysis of Algorithms
Algorithms Sorting.
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
CS 2210 Discrete Structures Algorithms and Complexity
Presentation transcript:

Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed http://informationtechnology.pk/pucit abdul.hameed@pucit.edu.pk Lecture 11

Sorting: Definition Sorting: an operation that segregates items into groups according to specified criterion. A = { 3 1 6 2 1 3 4 5 9 0 } A = { 0 1 1 2 3 3 4 5 6 9 }

Sorting Sorting = ordering. Sorted = ordered based on a particular way. Generally, collections of data are presented in a sorted manner. Examples of Sorting: Words in a dictionary are sorted (and case distinctions are ignored). Files in a directory are often listed in sorted order. The index of a book is sorted (and case distinctions are ignored).

Sorting: Cont’d Many banks provide statements that list checks in increasing order (by check number). In a newspaper, the calendar of events in a schedule is generally sorted by date. Musical compact disks in a record store are generally sorted by recording artist. Why? Imagine finding the phone number of your friend in your mobile phone, but the phone book is not sorted.

Review of Complexity Most of the primary sorting algorithms run on different space and time complexity. Time Complexity is defined to be the time the computer takes to run a program (or algorithm in our case). Space complexity is defined to be the amount of memory the computer needs to run a program.

Complexity (cont.) Complexity in general, measures the algorithms efficiency in internal factors such as the time needed to run an algorithm. External Factors (not related to complexity): Size of the input of the algorithm Speed of the Computer Quality of the Compiler

Types of Sorting Algorithms There are many, many different types of sorting algorithms, but the primary ones are: Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort Shell Sort Radix Sort Swap Sort Heap Sort

Bubble Sort: Idea Idea: bubble in water. How? Bubble in water moves upward. Why? How? When a bubble moves upward, the water from above will move downward to fill in the space left by the bubble.

Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12, 3, 7 6, 2, 9, 11, 9, 3, 12, 7 6, 2, 9, 11, 9, 3, 7, 12

Bubble Sort Example First Pass 6, 2, 9, 11, 9, 3, 7, 12 Second Pass 2, 6, 9, 9, 3, 7, 11, 12 2, 6, 9, 9, 3, 11, 7, 12 2, 6, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 11, 3, 7, 12 6, 2, 9, 11, 9, 3, 7, 12 Notice that this time we do not have to compare the last two numbers as we know the 12 is in position. This pass therefore only requires 6 comparisons.

Bubble Sort Example First Pass 6, 2, 9, 11, 9, 3, 7, 12 Second Pass 2, 6, 9, 9, 3, 7, 11, 12 Third Pass 2, 6, 9, 9, 3, 7, 11, 12 2, 6, 9, 3, 7, 9, 11, 12 2, 6, 9, 3, 9, 7, 11, 12 This time the 11 and 12 are in position. This pass therefore only requires 5 comparisons.

Bubble Sort Example First Pass 6, 2, 9, 11, 9, 3, 7, 12 Second Pass 2, 6, 9, 9, 3, 7, 11, 12 Third Pass 2, 6, 9, 3, 7, 9, 11, 12 Fourth Pass 2, 6, 3, 7, 9, 9, 11, 12 2, 6, 9, 3, 7, 9, 11, 12 2, 6, 3, 9, 7, 9, 11, 12 Each pass requires fewer comparisons. This time only 4 are needed.

Bubble Sort Example First Pass 6, 2, 9, 11, 9, 3, 7, 12 Second Pass 2, 6, 9, 9, 3, 7, 11, 12 Third Pass 2, 6, 9, 3, 7, 9, 11, 12 Fourth Pass 2, 6, 3, 7, 9, 9, 11, 12 Fifth Pass 2, 6, 3, 7, 9, 9, 11, 12 2, 3, 6, 7, 9, 9, 11, 12 The list is now sorted but the algorithm does not know this until it completes a pass with no exchanges.

Bubble Sort Example First Pass 6, 2, 9, 11, 9, 3, 7, 12 Second Pass 2, 6, 9, 9, 3, 7, 11, 12 Third Pass 2, 6, 9, 3, 7, 9, 11, 12 Fourth Pass 2, 6, 3, 7, 9, 9, 11, 12 Fifth Pass This pass no exchanges are made so the algorithm knows the list is sorted. It can therefore save time by not doing the final pass. With other lists this check could save much more work. 2, 3, 6, 7, 9, 9, 11, 12 Sixth Pass 2, 3, 6, 7, 9, 9, 11, 12

Bubble Sort Example Quiz Time Which number is definitely in its correct position at the end of the first pass? Answer: The last number must be the largest. How does the number of comparisons required change as the pass number increases? Answer: Each pass requires one fewer comparison than the last. How does the algorithm know when the list is sorted? Answer: When a pass with no exchanges occurs. What is the maximum number of comparisons required for a list of 10 numbers? Answer: 9 comparisons, then 8, 7, 6, 5, 4, 3, 2, 1 so total 45

Bubble Sort: Example 40 2 1 43 3 65 -1 58 42 4 65 2 1 40 3 43 -1 58 42 -1 58 42 4 1 2 65 2 1 40 3 43 -1 58 42 4 1 2 3 40 -1 43 42 4 3 65 58 4 1 2 3 40 65 -1 43 58 42 4 Notice that at least one element will be in the correct position each iteration.

Bubble Sort: Example 1 2 3 -1 40 65 43 58 42 4 1 -1 3 2 65 43 58 42 40 3 -1 40 65 43 58 42 4 1 -1 3 2 65 43 58 42 40 4 6 -1 1 2 65 3 43 58 42 40 4 7 8 -1 1 2 65 3 43 58 42 40 4

Selection Sort: Idea We have two group of items: sorted group, and unsorted group Initially, all items are in the unsorted group. The sorted group is empty. We assume that items in the unsorted group unsorted. We have to keep items in the sorted group sorted.

Selection Sort: Cont’d Select the “best” (eg. largest) item from the unsorted group, then put the “best” item at the end of the sorted group. Repeat the process until the unsorted group becomes empty.

Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted

Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted

Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted

Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted

Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted

Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted

Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted

Selection Sort 5 1 3 4 6 2  Largest Comparison Data Movement Sorted

Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted

Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted

Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted

Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted

Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted

Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted

Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted

Selection Sort 5 1 3 4 2 6  Largest Comparison Data Movement Sorted

Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted

Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted

Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted

Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted

Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted

Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted

Selection Sort 2 1 3 4 5 6  Largest Comparison Data Movement Sorted

Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted

Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted

Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted

Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted

Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted

Selection Sort 2 1 3 4 5 6  Largest Comparison Data Movement Sorted

Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted

Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted

Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted

Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted

Selection Sort 2 1 3 4 5 6  Largest Comparison Data Movement Sorted

Selection Sort 1 2 3 4 5 6 Comparison Data Movement Sorted

Selection Sort 1 2 3 4 5 6 DONE! Comparison Data Movement Sorted

Selection Sort: Example 40 2 1 43 3 65 -1 58 42 4 40 2 1 43 3 4 -1 58 3 42 65 40 2 1 43 3 4 -1 42 3 58 65 40 2 1 3 3 4 -1 42 43 58 65

Selection Sort: Example 40 2 1 3 3 4 -1 42 43 58 65 -1 2 1 3 3 4 40 42 43 58 65 -1 2 1 3 3 4 40 42 43 58 65 -1 2 1 3 3 4 40 42 43 58 65

Selection Sort: Example -1 2 1 3 3 4 40 42 43 58 65 -1 1 2 3 3 4 40 42 43 58 65 -1 1 2 3 3 4 40 42 43 58 65 -1 1 2 3 3 4 40 42 43 58 65 -1 1 2 3 3 4 40 42 43 58 65

Insertion Sort: Idea 8 | 5 9 2 6 3 5 8 | 9 2 6 3 5 8 9 | 2 6 3 Idea: sorting cards. 8 | 5 9 2 6 3 5 8 | 9 2 6 3 5 8 9 | 2 6 3 2 5 8 9 | 6 3 2 5 6 8 9 | 3 2 3 5 6 8 9 |

Insertion Sort: Idea We have two group of items: sorted group, and unsorted group Initially, all items in the unsorted group and the sorted group is empty. We assume that items in the unsorted group unsorted. We have to keep items in the sorted group sorted. Pick any item from, then insert the item at the right position in the sorted group to maintain sorted property. Repeat the process until the unsorted group becomes empty.

Insertion Sort: Example 40 40 2 1 43 3 65 -1 58 42 4 2 40 1 43 3 65 -1 58 3 42 4 1 2 40 43 3 65 -1 58 3 42 4

Insertion Sort: Example 1 2 40 43 3 65 -1 58 3 42 4 1 2 3 40 43 65 -1 58 3 42 4 1 2 3 40 43 65 -1 58 3 42 4

Insertion Sort: Example 1 2 3 40 43 65 -1 58 3 42 4 1 2 3 40 43 65 -1 58 3 42 4 -1 1 1 2 2 3 3 40 43 40 43 65 65 58 3 42 4

Insertion Sort: Example -1 1 1 2 2 3 3 40 40 43 43 65 58 65 3 42 4 -1 1 2 1 2 3 3 40 3 43 40 43 65 43 58 58 65 65 42 4 -1 1 1 2 2 3 3 40 3 43 40 65 43 42 65 43 58 65 4 1 2 3 40 43 65 42 -1 58 4

A Lower Bound Bubble Sort, Selection Sort, Insertion Sort all have worst case of O(N2). Turns out, for any algorithm that exchanges adjacent items, this is the best worst case: Ω(N2) In other words, this is a lower bound!

Greedy Algorithm Many algorithms are designed to solve optimization problems. The goal of such problems is to find a solution to the given problem that either minimizes or maximizes the value of some parameter. For Example: Finding a route between two cities with smallest total mileage, Determining a way to encode messages using the fewest bits possible etc

Simplest approaches often leads to a solution of an optimization problem. This approach selects the best choice at each step, instead of considering all sequences of steps that may lead to an optimal solution. Algorithms that make what seems to be the "best“ choice at each step are called greedy algorithms.

Designing a Greedy Algorithm Cast the problem so that we make a greedy (locally optimal) choice and are left with one subproblem Prove there is always a (globally) optimal solution to the original problem that makes the greedy choice Show that the choice together with an optimal solution to the subproblem gives an optimal solution to the original problem

Example Consider the problem of making n cents change with quarters, dimes, nickels, and pennies, and using the least total number of coins. We can devise a greedy algorithm for making change for n cents by making a locally optimal choice at each step; that is, at each step we choose the coin of the largest denomination possible to add to the pile of change without exceeding n cents

For example, to make change for 67 cents, we first select a quarter (leaving 42 cents). We next select a second quarter (leaving 1 7 cents), followed by a dime (leaving 7 cents), followed by a nickel (leaving 2 cents), followed by a penny (leaving 1 cent), followed by a penny.