Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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,
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Understanding the Need for Sorting Records
Chapter 9: Advanced Array Manipulation
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Sorting. Sorting Considerations We consider sorting a list of records, either into ascending or descending order, based upon the value of some field of.
Chapter 9: Searching, Sorting, and Algorithm Analysis
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
Visual C++ Programming: Concepts and Projects
SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Selection Sort, Insertion Sort, Bubble, & Shellsort
An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays.
Programming Logic and Design Fourth Edition, Comprehensive
Searching and Sorting Arrays
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Insertion Sort & Shellsort By: Andy Le CS146 – Dr. Sin Min Lee Spring 2004.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
1 Arrays 2: Sorting and Searching Admin. §1) No class Thursday. §2) Will cover Strings next Tuesday. §3) Take in report. §4) Hand out program assignment.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Recursion, Complexity, and Sorting By Andrew Zeng.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
Searching and Sorting Chapter Sorting Arrays.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 12 Manipulating Larger Quantities of Data.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
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.
Bubble Sort. 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, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,
BUBBLE SORT. Introduction Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to.
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.
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.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
12. Sorting Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Sorting Many computer applications.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
CS 162 Intro to Programming II Bubble Sort 1. Compare adjacent elements. If the first is greater than the second, swap them. Do this for each pair of.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
ALGORITHMS.
SORTING ALGORITHMS King Saud University College of Applied studies and Community Service CSC 1101 By: Nada Alhirabi 1.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Sorting. Sorting Sorting is important! Things that would be much more difficult without sorting: –finding a telephone number –looking up a word in the.
An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Searching and Sorting Searching algorithms with simple arrays
Chapter 9: Sorting and Searching Arrays
Introduction to Search Algorithms
Arrays 2.
Design and Analysis of Algorithms
Chapter 5: Arrays: Lists and Tables
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.
Programming Logic and Design Fourth Edition, Comprehensive
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
CSE 373 Data Structures and Algorithms
Presentation transcript:

Bubble Sort

Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or state)?  No, it doesn’t. The computer converts all characters into a series of 0’s and 1’s. It’s the 0’s and 1’s that the computer uses to compare fields for sorting.  You can sort a file by  ascending order (lowest to highest)  descending order (highest to lowest)

Bubble Sort Logic  Let’s look at the logic to sort 5 test scores in a file  The mainline logic will consist of 3 subroutines  Housekeeping  Sort-Scores  Finish-Up

Starting the Sort-Scores Routine  When Housekeeping is done, the five scores from the In-Score file are in the Score array.  Let’s assume the scores are: Score(1) = 90 Score(2) = 85 Score(3) = 65 Score(4) = 95 Score(5) = 75 The next slide shows the start of the routine

Swapping Scores  To start the sort, the first two values in the array are compared. Score(1) = 90 Score(2) = 85 Score(3) = 65 Score(4) = 95 Score(5) = 75 If they are out of ascending order, switch (or swap) their positions in the array Score(1) = 85 Score(2) = 90 Score(3) = 65 Score(4) = 95 Score(5) = 75 The order is a little better than before

Swapping Dilemma  Here is our original score array  If we move the value 90 from Score(1) to Score(2),  both array elements would contain the value 90  the value 85 would be lost  If you move 85 to Score(1), then the value 90 would be lost  How can we solve this problem? Score(1) = 90 Score(2) = 85 Score(3) = 65 Score(4) = 95 Score(5) = 75

Swapping Solution  The solution: create a temporary variable to hold one of the scores  The solution work like this:  Move 85 - in Score(2) - to Temp –Move 90 - in Score(1) - to Score(2) –Move 85 - in Temp - the Score(1) Now Score(1) equals 85 and Score(2) equals 90

More on Swapping  The previous routine is written only to switch elements 1 and 2  Here is a more universal routine that can be used with any two adjacent elements to be swapped  Variable X represents the position of the first element  Value X+1 represents the position of the second element

Sorting Logic  The decision - Score(X) > Score(X+1) has to be executed four times.  Why not five times since there are five elements?  5 is the last element, so there is no element after it to compare

Following the Swapping Logic - Step 1  The original array Score(1) = 85 Score(2) = 90 Score(3) = 65 Score(4) = 95 Score(5) = 75 Score(1) = 90 Score(2) = 85 Score(3) = 65 Score(4) = 95 Score(5) = 75 The logic: –Set X to 1 –X < 5, so the loop is entered –Compare Score(X) = 90 to Score(X+1) = 85. They are out of order so swap them The result is:

Following the Swapping Logic - Step 2  Step 2 logic:  Add 1 to X; X now equals 2  X < 5, so the loop is entered  Compare Score(X) = 90 to Score(X+1) = 65. They are out of order so swap them  The result is: Score(1) = 85 Score(2) = 90 Score(3) = 65 Score(4) = 95 Score(5) = 75 Score(1) = 85 Score(2) = 65 Score(3) = 90 Score(4) = 95 Score(5) = 75

Following the Swapping Logic - Step 3  Step 3 logic:  Add 1 to X; X now equals 3  X < 5, so the loop is entered  Compare Score(X) = 90 to Score(X+1) = 95. They are in order. No swap is made  The result is: Score(1) = 85 Score(2) = 65 Score(3) = 90 Score(4) = 95 Score(5) = 75 Score(1) = 85 Score(2) = 65 Score(3) = 90 Score(4) = 95 Score(5) = 75

Following the Swapping Logic - Step 4  Step 4 logic:  Add 1 to X; X now equals 4  X < 5, so the loop is entered  Compare Score(X) = 95 to Score(X+1) = 75. They are out of order so swap them  The result is: Score(1) = 85 Score(2) = 65 Score(3) = 90 Score(4) = 95 Score(5) = 75 Score(1) = 85 Score(2) = 65 Score(3) = 90 Score(4) = 75 Score(5) = 95

Following the Swapping Logic - Step 5  Step 5 logic:  Add 1 to X; X now equals 5  X = 5, so the loop is not entered and the swapping logic is completed  Every element has been compared to the one adjacent to it Score(1) = 85 Score(2) = 65 Score(3) = 90 Score(4) = 75 Score(5) = 95 The largest amount (95) has sunk to the bottom of the list

Continuing the Swapping Logic  Although the list is in a better ascending order, it is still not in order  The five steps need to be performed again so the following values will swap places  85 and 65  90 and 75 Score(1) = 85 Score(2) = 65 Score(3) = 90 Score(4) = 75 Score(5) = 95 Score(1) = 65 Score(2) = 85 Score(3) = 75 Score(4) = 90 Score(5) = 95

Continuing the Swapping Logic  The five steps will need to be performed one more time to swap 85 and 75  The final result Score(1) = 65 Score(2) = 85 Score(3) = 75 Score(4) = 90 Score(5) = 95 Score(1) = 65 Score(2) = 75 Score(3) = 85 Score(4) = 90 Score(5) = 95

If the list were in the worst possible order....  The five steps would have to be performed four time to get the list in correct ascending order Score(1) = 95 Score(2) = 90 Score(3) = 85 Score(4) = 75 Score(5) = 65 Score(1) = 65 Score(2) = 75 Score(3) = 85 Score(4) = 90 Score(5) = 95 To go from this list to this list Have to pass through the loop 4 times

Complete Sort Logic  X is used to determine that all elements in the list have been compared for swapping Y is used to determine if there have been enough passes through the list to sort the list correctly

More on Bubble Sorts  Using a variable for the array size  Previously, a constant (5) was used to determine the end of loop processing  Using a variable means the code doesn’t change if the size of the array changes

Rules for Sorting  The greatest number of pair comparisons needed to be made during each loop is one fewer than the number of elements is the array  The pair comparison loop needs to be processed one time fewer than the number of elements in the array

Reducing Unnecessary Comparisons  In a bubble sort, the first pass through the list guarantees the last item on the list will be at the bottom  For second pass, the second to the last item will be in the correct position  Each pass will correctly place an item towards the bottom of the list  When performing each pass, why continue to compare items known to be correct?

Reducing Comparisons Logic  Pairs-To-Compare is set to X-1 the first time through the list Comparisons are done Pairs-To-Compare times 1 is subtracted from Pairs- To-Compare, so next comparison processing will pass one less time

Eliminating Unnecessary Passes Logic  To eliminate unneeded passes -  Set a flag (Switch-Occurred) to “No” before starting –When a switch occurs, set the flag to “Yes” –If a pass has no switches, the flag remains set to “No” –If the flag is “No” when exiting the loop, the sort is finished