The Bubble Sort Mr. Dave Clausen La Cañada High School

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

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.
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,
Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
Chapter 9: Searching, Sorting, and Algorithm Analysis
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Visual C++ Programming: Concepts and Projects
The Quick Sort Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen.
CSE 373: Data Structures and Algorithms
Overview Sort – placing data in an array in some order (usually decreasing or increasing order) Bubble Sort More efficient bubble sort.
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.
Algorithm Efficiency and Sorting
Searching Arrays Linear search Binary search small 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.
The Insertion Sort Mr. Dave Clausen La Cañada High School.
The Binary Search Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
CSE 373 Data Structures and Algorithms
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
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.
12. Sorting Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Sorting Many computer applications.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
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.
Bubble Sort.
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.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
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.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
The Selection Sort Mr. Dave Clausen La Cañada High School.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
SORTING Sorting is storage of data in some order, it can be in ascending or descending order. The term Sorting comes along-with the term Searching. There.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
Searching and Sorting Algorithms
Sorting Mr. Jacobs.
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
The Sequential Search (Linear Search)
10.3 Bubble Sort Chapter 10 - 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.
Linear and Binary Search
Algorithm Efficiency and Sorting
Algorithm design and Analysis
Describing algorithms in pseudo code
Mr. Dave Clausen La Cañada High School
Shuttle Sort Example 1st pass Comparisons: 1
Searching and Sorting Arrays
Ken Lambert & Doug Nance
Mr. Dave Clausen La Cañada High School
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.
Searching.
Searching and Sorting Arrays
The Binary Search by Mr. Dave Clausen
Shuttle Sort Example 1st pass Comparisons: 1
The Sequential Search (Linear Search)
The Sequential Search (Linear Search)
CS 165: Project in Algorithms and Data Structures Michael T. Goodrich
Module 8 – Searching & Sorting Algorithms
Mr. Dave Clausen La Cañada High School
Presentation transcript:

The Bubble Sort Mr. Dave Clausen La Cañada High School 1/23/2018 The Bubble Sort Mr. Dave Clausen La Cañada High School Mr. Dave Clausen

Objectives Understand and use the Bubble Sort to sort data in a program. Understand and know Big-O notation for the Bubble Sort. Mr. Dave Clausen

The Bubble Sort Algorithm 1/23/2018 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order. Each pair of adjacent elements is compared and swapped until the smallest element “bubbles” to the top. Repeat this process each time stopping one indexed element less, until you compare only the first (or last) two elements in the list. The largest element will have “sunk” to the bottom. Mr. Dave Clausen Mr. Dave Clausen

A Bubble Sort Example Compare We start by comparing the first two elements in the List. This list is an example of a “worst case” scenario for the Bubble Sort, because the List is in the exact opposite order from the way we want it sorted (the computer program does not know that it is sorted at all). Mr. Dave Clausen

A Bubble Sort Example Swap Mr. Dave Clausen

A Bubble Sort Example Compare Mr. Dave Clausen

A Bubble Sort Example Swap Mr. Dave Clausen

A Bubble Sort Example Compare Mr. Dave Clausen

A Bubble Sort Example Swap Mr. Dave Clausen

A Bubble Sort Example Compare Mr. Dave Clausen

A Bubble Sort Example Swap Mr. Dave Clausen

A Bubble Sort Example Compare Mr. Dave Clausen

A Bubble Sort Example As you can see, the largest number has “bubbled” down, or sunk to the bottom of the List after the first pass through the List. Swap Mr. Dave Clausen

A Bubble Sort Example Compare For our second pass through the List, we start by comparing these first two elements in the List. Mr. Dave Clausen

A Bubble Sort Example Swap Mr. Dave Clausen

A Bubble Sort Example Compare Mr. Dave Clausen

A Bubble Sort Example Swap Mr. Dave Clausen

A Bubble Sort Example Compare Mr. Dave Clausen

A Bubble Sort Example Swap Mr. Dave Clausen

A Bubble Sort Example Compare Mr. Dave Clausen

A Bubble Sort Example At the end of the second pass, we stop at element number n - 1, because the largest element in the List is already in the last position. This places the second largest element in the second to last spot. Swap Mr. Dave Clausen

A Bubble Sort Example Compare We start with the first two elements again at the beginning of the third pass. Mr. Dave Clausen

A Bubble Sort Example Swap Mr. Dave Clausen

A Bubble Sort Example Compare Mr. Dave Clausen

A Bubble Sort Example Swap Mr. Dave Clausen

A Bubble Sort Example Compare Mr. Dave Clausen

A Bubble Sort Example At the end of the third pass, we stop comparing and swapping at element number n - 2. Swap Mr. Dave Clausen

A Bubble Sort Example Compare The beginning of the fourth pass... Mr. Dave Clausen

A Bubble Sort Example Swap Mr. Dave Clausen

A Bubble Sort Example Compare Mr. Dave Clausen

A Bubble Sort Example Swap The end of the fourth pass stops at element number n - 3. Mr. Dave Clausen

A Bubble Sort Example Compare The beginning of the fifth pass... Mr. Dave Clausen

A Bubble Sort Example Swap The last pass compares only the first two elements of the List. After this comparison and possible swap, the smallest element has “bubbled” to the top. Mr. Dave Clausen

What “Swapping” Means 6 TEMP Place the first element into the Temporary Variable. Mr. Dave Clausen

What “Swapping” Means 6 TEMP Replace the first element with the second element. Mr. Dave Clausen

What “Swapping” Means 6 TEMP Replace the second element with the Temporary Variable. Mr. Dave Clausen

C + + Examples of The Bubble Sort C++ Animation For Bubble Sort: Here Are Some Animated Examples: On the Net: http://compsci.exeter.edu/Winter99/CS320/Resources/sortDemo.html http://www.aist.go.jp/ETL/~suzaki/AlgorithmAnimation/index.html Mr. Dave Clausen

C++ Code for Swap Procedure void Swap_Data (int &number1, int &number2) { int temp; temp = number1; number1 = number2; number2 = temp; } Mr. Dave Clausen

C++ Code For Bubble Sort void Bubble_Sort (vector <int> & v) { int element, index; for (element = 1; element < v.size( ); ++element) for (index = v.size( )-1; index >= element; --index) if ( v [index-1] > v [index]) Swap_Data ( v [index-1], v [index]); } Mr. Dave Clausen

A More Efficient Algorithm Initialize counter k to zero Initialize boolean exchange_made to true While (k < n - 1) and exchange_made Set exchange_made to false Increment counter k For each j from 0 to n - k If item in jth position > item in (j + 1)st position Swap these items Set exchange_made to true Mr. Dave Clausen

More Efficient C++ Source Code void Bubble_Sort(vector<int> &v) { int k = 0; bool exchange_made = true; while ((k < v.size() - 1) && exchange_made) { exchange_made = false; ++k; for (int j = 0; j < v.size() - k; ++j) if (v[j] > v[j + 1]) Swap_Data(v[j], v[j + 1]); exchange_made = true; } } // Make up to n - 1 passes through vector, exit early if no exchanges } // are made on previous pass Mr. Dave Clausen

Swap_Data Helper Function void Swap_Data (int &number1, int &number2) { int temp; temp = number1; number1 = number2; number2 = temp; } // End of Swap_Data function Mr. Dave Clausen

Pascal Code for Swap Procedure procedure Swap (var number1, number2: integer); var temp: integer; begin temp := number1; number1 := number2; number2 := temp end; {Swap} Mr. Dave Clausen

Pascal Code For Bubble Sort procedure BubbleSort (var IntArray: IntNumbers); var element, index: integer; begin for element := 1 to MaxNum do for index := MaxNum downto (element + 1) do if IntArray [index] < IntArray [index - 1] then Swap (IntArray [index], IntArray [index - 1]) end; {BubbleSort} Mr. Dave Clausen

BASIC Code For Bubble Sort 8000 REM ################# 8010 REM Bubble Sort 8020 REM ################# 8030 FOR ELEMENT = 1 TO MAXNUM - 1 8040 ::FOR INDEX = 1 TO MAXNUM - 1 8050 ::::::IF N (INDEX) < = N (INDEX + 1) THEN GOTO 8090 8060 ::::::TEMP = N (INDEX + 1) 8070 ::::::N (INDEX + 1) = N (INDEX) 8080 ::::::N (INDEX) = TEMP 8090 ::NEXT INDEX 8100 NEXT ELEMENT 8110 RETURN Mr. Dave Clausen

Big - O Notation Big - O notation is used to describe the efficiency of a search or sort. The actual time necessary to complete the sort varies according to the speed of your system. Big - O notation is an approximate mathematical formula to determine how many operations are necessary to perform the search or sort. The Big - O notation for the Bubble Sort is O(n2), because it takes approximately n2 passes to sort the elements. Mr. Dave Clausen