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

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

CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
 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.
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.
Algorithm Efficiency and Sorting
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.
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.
EFFICIENCY & SORTING CITS1001. Listen to the sound of sorting Various algorithms Quicksort
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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.
Computer Science Searching & Sorting.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
CSE 373 Data Structures and Algorithms
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
Sorting. Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array.
BUBBLE SORT. Introduction Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to.
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.
Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:
12. Sorting Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Sorting Many computer applications.
CSCI 51 Introduction to Programming March 12, 2009.
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.
Sorting an array bubble and selection sorts. Sorting An arrangement or permutation of data An arrangement or permutation of data May be either: May be.
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.
Python: Sorting - Bubblesort Damian Gordon. Sorting: Bubblesort The simplest algorithm for sort an array is called BUBBLE SORT. It works as follows for.
3 – SIMPLE SORTING ALGORITHMS
SORTING Chapter 8 CS Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following.
Sorting Algorithms: Selection, Insertion and Bubble.
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.
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])
Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
CSCI 51 Introduction to Programming March 10, 2009.
The Selection Sort Mr. Dave Clausen La Cañada High School.
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.
12. Searching/Sorting Programming in C++ Computer Science Dept Va Tech August, 2000 © Barnette ND, McQuain WD, Keenan MA 1 Simple Searching Many.
Algorithm Definition An algorithm is a step-by-step solution to a problem.
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.
The Bubble Sort Mr. Dave Clausen La Cañada High School
Sorting Mr. Jacobs.
Introduction to Search Algorithms
The Sequential Search (Linear Search)
Design and Analysis of Algorithms
Describing algorithms in pseudo code
Mr. Dave Clausen La Cañada High School
Introduction to Programming
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.
Sorting Example Bubble Sort
The Binary Search by Mr. Dave Clausen
The Sequential Search (Linear Search)
The Sequential Search (Linear Search)
Module 8 – Searching & Sorting Algorithms
Sorting and Complexity
Mr. Dave Clausen La Cañada High School
Presentation transcript:

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

Mr. Dave Clausen2 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 largest element “bubbles” to the bottom. Repeat this process each time stopping one indexed element less, until you compare only the first two elements in the list. We know that the array is sorted after both of the nested loops have finished.

Mr. Dave Clausen3 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 sorting, because the List is in the exact opposite order from the way we want it sorted (the computer program does not know this).

Mr. Dave Clausen4 A Bubble Sort Example Swap

Mr. Dave Clausen5 A Bubble Sort Example Compare

Mr. Dave Clausen6 A Bubble Sort Example Swap

Mr. Dave Clausen7 A Bubble Sort Example Compare

Mr. Dave Clausen8 A Bubble Sort Example Swap

Mr. Dave Clausen9 A Bubble Sort Example Compare

Mr. Dave Clausen10 A Bubble Sort Example Swap

Mr. Dave Clausen11 A Bubble Sort Example Compare

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

Mr. Dave Clausen13 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 Clausen14 A Bubble Sort Example Swap

Mr. Dave Clausen15 A Bubble Sort Example Compare

Mr. Dave Clausen16 A Bubble Sort Example Swap

Mr. Dave Clausen17 A Bubble Sort Example Compare

Mr. Dave Clausen18 A Bubble Sort Example Swap

Mr. Dave Clausen19 A Bubble Sort Example Compare

Mr. Dave Clausen20 A Bubble Sort Example Swap 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.

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

Mr. Dave Clausen22 A Bubble Sort Example Swap

Mr. Dave Clausen23 A Bubble Sort Example Compare

Mr. Dave Clausen24 A Bubble Sort Example Swap

Mr. Dave Clausen25 A Bubble Sort Example Compare

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

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

Mr. Dave Clausen28 A Bubble Sort Example Swap

Mr. Dave Clausen29 A Bubble Sort Example Compare

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

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

Mr. Dave Clausen32 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 Clausen33 What “Swapping” Means TEMP Place the first element into the Temporary Variable. 6

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

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

Mr. Dave Clausen36 Python Code For Bubble Sort

Mr. Dave Clausen37 Python Code for Swap Procedure

Mr. Dave Clausen38 Java Code For Bubble Sort public static void bubbleSort(int[ ] list){ int k = 0; boolean exchangeMade = true; // Make up to n - 1 passes through array, exit early if no exchanges // are made on previous pass while ((k < list.length - 1) && exchangeMade){ exchangeMade = false; k++; for (int j = 0; j < list.length - k; j++) if (list[j] > list[j + 1]){ swap(list, j, j + 1); exchangeMade = true; }

Mr. Dave Clausen39 Java Code for Swap Procedure public static void swap(int[] list, int x, int y) { int temp = list[x]; list[x] = list[y]; list[y] = temp; }

Mr. Dave Clausen40 C++ Code For Bubble Sort void Bubble_Sort (int array, int length) { int element, index; int element, index; for (element = 1; element < length; ++element) for (element = 1; element < length; ++element) for (index = length-1; index >= element; --index) for (index = length-1; index >= element; --index) if (array[index-1] > array[index]) if (array[index-1] > array[index]) Swap_Data (array[index-1], array[index]); Swap_Data (array[index-1], array[index]); } //BubbleSort

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

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

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

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

Mr. Dave Clausen45 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(n 2 ), because it takes approximately n 2 passes to sort the elements.