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

The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mar 12, 2018

Download

Documents

hahanh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

The Bubble Sortbyby

Mr. Dave ClausenMr. Dave ClausenLa Cañada High SchoolLa Cañada High School

Page 2: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 2

The Bubble SortAlgorithm

The Bubble Sort compares adjacent elements in a The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order. list, and “swaps” them if they are not in order. Each pair of adjacent elements is compared and Each pair of adjacent elements is compared and swapped until the largest element “bubbles” to the swapped until the largest element “bubbles” to the bottom. Repeat this process each time stopping bottom. Repeat this process each time stopping one indexed element less, until you compare only one indexed element less, until you compare only the first two elements in the list. We know that the first two elements in the list. We know that the array is sorted after both of the nested loops the array is sorted after both of the nested loops have finished.have finished.

Page 3: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 3

A Bubble Sort Example

654321

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).

Page 4: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 4

A Bubble Sort Example

564321

Swap

Page 5: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 5

A Bubble Sort Example

564321

Compare

Page 6: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 6

A Bubble Sort Example

546321

Swap

Page 7: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 7

A Bubble Sort Example

546321

Compare

Page 8: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 8

A Bubble Sort Example

543621

Swap

Page 9: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 9

A Bubble Sort Example

543621

Compare

Page 10: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 10

A Bubble Sort Example

543261

Swap

Page 11: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 11

A Bubble Sort Example

543261

Compare

Page 12: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 12

A Bubble Sort Example

543216

Swap

As you can see, the largest number has “bubbled” down to the bottom of the List after the first pass through the List.

Page 13: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 13

A Bubble Sort Example

543216

Compare

For our second pass through the List, we start by comparing these first two elements in the List.

Page 14: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 14

A Bubble Sort Example

453216

Swap

Page 15: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 15

A Bubble Sort Example

453216

Compare

Page 16: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 16

A Bubble Sort Example

435216

Swap

Page 17: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 17

A Bubble Sort Example

435216

Compare

Page 18: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 18

A Bubble Sort Example

432516

Swap

Page 19: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 19

A Bubble Sort Example

432516

Compare

Page 20: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 20

A Bubble Sort Example

432156

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.

Page 21: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 21

A Bubble Sort Example

432156

Compare

We start with the first two elements again at the beginning of the third pass.

Page 22: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 22

A Bubble Sort Example

342156

Swap

Page 23: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 23

A Bubble Sort Example

342156

Compare

Page 24: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 24

A Bubble Sort Example

324156

Swap

Page 25: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 25

A Bubble Sort Example

324156

Compare

Page 26: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 26

A Bubble Sort Example

321456

Swap

At the end of the third pass, we stop comparing and swapping at element number n - 2.

Page 27: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 27

A Bubble Sort Example

321456

Compare

The beginning of the fourth pass...

Page 28: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 28

A Bubble Sort Example

231456

Swap

Page 29: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 29

A Bubble Sort Example

231456

Compare

Page 30: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 30

A Bubble Sort Example

213456

Swap

The end of the fourth pass stops at element number n - 3.

Page 31: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 31

A Bubble Sort Example

213456

Compare

The beginning of the fifth pass...

Page 32: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 32

A Bubble Sort Example

123456

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.

Page 33: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 33

What “Swapping” Means654321

TEMP

Place the first element into the Temporary Variable.

6

Page 34: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 34

What “Swapping” Means554321

TEMP

Replace the first element with the second element.

6

Page 35: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 35

What “Swapping” Means564321

TEMP

Replace the second element with the Temporary Variable.

6

Page 36: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 36

Java Code For Bubble Sortpublic 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;

}}

}

Page 37: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 37

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;

}

Page 38: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 38

C++ Code For Bubble Sort

void Bubble_Sort (int array, int length)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 = lengthfor (index = length--1; index >= element; 1; index >= element; ----index)index)if (array[indexif (array[index--1] > array[index])1] > array[index])Swap_Data (array[indexSwap_Data (array[index--1], array[index]);1], array[index]);

} //} //BubbleSortBubbleSort

Page 39: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 39

C++ Code for Swap Procedure

void Swap_Data (int &number1, int &number2)void Swap_Data (int &number1, int &number2){{

int temp;int temp;

temp = number1;temp = number1;number1 = number2;number1 = number2;number2 = temp;number2 = temp;

} } //Swap_Data//Swap_Data

Page 40: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 40

Pascal Code For Bubble Sort

procedureprocedure BubbleSortBubbleSort ((var IntArrayvar IntArray:: IntNumbersIntNumbers););varvarelement, index: integer;element, index: integer;

beginbeginfor element := 1 tofor element := 1 to MaxNumMaxNum dodofor index :=for index := MaxNum downtoMaxNum downto (element + 1) do(element + 1) doifif IntArrayIntArray [index] <[index] < IntArrayIntArray [index [index -- 1]1]then Swap (then Swap (IntArrayIntArray [index],[index], IntArrayIntArray [index [index -- 1])1])

end; {end; {BubbleSortBubbleSort}}

Page 41: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 41

Pascal Code for Swap Procedure

procedure Swap (procedure Swap (varvar number1, number2: integer);number1, number2: integer);varvartemp: integer;temp: integer;

beginbegintemp := number1;temp := number1;number1 := number2;number1 := number2;number2 := tempnumber2 := temp

end; {Swap}end; {Swap}

Page 42: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 42

BASIC Code For Bubble Sort8000 REM #################8000 REM #################8010 REM Bubble Sort8010 REM Bubble Sort8020 REM #################8020 REM #################8030 FOR ELEMENT = 1 TO MAXNUM 8030 FOR ELEMENT = 1 TO MAXNUM -- 118040 ::FOR INDEX = 1 TO MAXNUM 8040 ::FOR INDEX = 1 TO MAXNUM -- 118050 ::::::IF N (INDEX) < = N (INDEX + 1) THEN GOTO 80908050 ::::::IF N (INDEX) < = N (INDEX + 1) THEN GOTO 80908060 ::::::TEMP = N (INDEX + 1)8060 ::::::TEMP = N (INDEX + 1)8070 ::::::N (INDEX + 1) = N (INDEX)8070 ::::::N (INDEX + 1) = N (INDEX)8080 ::::::N (INDEX) = TEMP8080 ::::::N (INDEX) = TEMP8090 ::NEXT INDEX8090 ::NEXT INDEX8100 NEXT ELEMENT 8100 NEXT ELEMENT 8110 RETURN 8110 RETURN

Page 43: The Bubble Sort - Clausen · PDF fileMr. Dave Clausen 2 The Bubble Sort Algorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order

Mr. Dave Clausen 43

Big - O Notation

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