44
Bubble and Selecon Sorts

Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

  • Upload
    vonhi

  • View
    218

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

Bubble and Selection Sorts

Page 2: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

2

Objectives

In this Week, you will learn to:Identify the algorithms that can be used to sortdataSort data by using bubble sort Sort data by using selection sort

Page 3: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

3

Sorting is the process of arranging data insome pre-defined order or sequence. Theorder can be either ascending or descending. If the data is ordered, you can directly go tothe section , thereby reducing the number ofrecords to be traversed.

Sorting Data

Page 4: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

4

Sorting is implemented in a program by usingan algorithm.Some sorting algorithms are:

Bubble sort Selection sort Insertion sortShell sort Merge sortQuick sort

Selecting a Sorting Algorithm

Page 5: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

5

Bubble sort algorithm:Is one of the simplest sorting algorithmsHas a quadratic order of growth and istherefore suitable for sorting small lists onlyWorks by repeatedly scanning through thelist, comparing adjacent elements, andswapping them if they are in the wrongorder

Sorting data by Using Bubble Sort

Page 6: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

6

To understand the implementation of bubblesort algorithm, consider an unsorted list ofnumbers stored in an array.

Implementing Bubble Sort Algorithm

arr

210 43

2 6 75 3

Page 7: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

7

Let us sort this unsorted list.

Implementing Bubble Sort Algorithm(Contd.)

arr

210 43

2 6 75 3

Page 8: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

8

Pass 1n = 5

Compare the element stored at index 0 with the element storedat index 1.

arr

210 43

2 6 75 3

Implementing Bubble Sort Algorithm(Contd.)

Page 9: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

9

Pass 1n = 5

Swap the values if they are not in the correct order.

Implementing Bubble Sort Algorithm (Contd.)

arr

210 43

6 75 3

Swap

22 5

Page 10: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

10

Pass 1n = 5

Compare the element stored at index 1 with the element storedat index 2 and swap the values if the value at index 1 is greaterthan the value at index 2.

Implementing Bubble Sort Algorithm(Contd.)

arr

210 43

6 7 32 5

No Change

Page 11: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

11

Pass 1n = 5

Compare the element stored at index 2 with the element storedat index 3 and swap the values if the value at index 2 is greaterthan the value at index 3.

No Change

Implementing Bubble Sort Algorithm(Contd.)

arr

210 43

6 7 32 5

Page 12: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

12

Pass 1n = 5

Compare the element stored at index 3 with the element storedat index 4 and swap the values if the value at index 3 is greaterthan the value at index 4.

Swap

Implementing Bubble Sort Algorithm (Contd.)

arr

210 43

6 7 32 5 3 7

Page 13: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

13

Pass 1n = 5

Compare the element stored at index 3 with the element storedat index 4 and swap the values if the value at index 3 is greaterthan the value at index 4.

Largest element is placed at its correct position after Pass 1

Implementing Bubble Sort Algorithm (Contd.)

arr

210 43

6 3 72 5

Page 14: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

14

Pass 2n = 5

Compare the element stored at index 0 with the element storedat index 1 and swap the values if the value at index 0 is greaterthan the value at index 1.

No Change

Implementing Bubble Sort Algorithm (Contd.)

arr

210 43

6 3 72 5

Page 15: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

15

Pass 2n = 5

Compare the element stored at index 1 with the element storedat index 2 and swap the values if the value at index 1 is greaterthan the value at index 2.

No Change

Implementing Bubble Sort Algorithm (Contd.)

arr

210 43

6 3 72 5

Page 16: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

16

Pass 2n = 5

Compare the element stored at index 2 with the element storedat index 3 and swap the values if the value at index 2 is greaterthan the value at index 3.

Swap

Implementing Bubble Sort Algorithm (Contd.)

arr

210 43

6 3 72 5 3 6

Page 17: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

17

Pass 2n = 5

Compare the element stored at index 2 with the element storedat index 3 and swap the values if the value at index 2 is greaterthan the value at index 3.

Second largest element is placed at its correct position after Pass 2

Implementing Bubble Sort Algorithm (Contd.)

arr

210 43

3 6 72 5

Page 18: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

18

Pass 3n = 5

Compare the element stored at index 0 with the element storedat index 1 and swap the values if the value at index 0 is greaterthan the value at index 1.

No Change

Implementing Bubble Sort Algorithm (Contd.)

arr

210 43

3 6 72 5

Page 19: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

19

Pass 3n = 5

Compare the element stored at index 1 with the element storedat index 2 and swap the values if the value at index 1 is greaterthan the value at index 2.

Swap

Implementing Bubble Sort Algorithm (Contd.)

arr

210 43

3 6 72 53 5

Page 20: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

20

Pass 3n = 5

Compare the element stored at index 2 with the element storedat index 3 and swap the values if the value at index 2 is greaterthan the value at index 3.

Third largest element is placed at its correct position after Pass 3

Implementing Bubble Sort Algorithm (Contd.)

arr

210 43

5 6 72 3

Page 21: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

21

Pass 4n = 5

Compare the element stored at index 0 with the element storedat index 1 and swap the values if the value at index 0 is greaterthan the value at index 1.

No Change

Implementing Bubble Sort Algorithm (Contd.)

arr

210 43

5 6 72 3

Page 22: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

22

Pass 4n = 5

Compare the element stored at index 0 with the element storedat index 1 and swap the values if the value at index 0 is greaterthan the value at index 1.

Fourth largest element is placed at its correct position after Pass 4

Implementing Bubble Sort Algorithm (Contd.)

arr

210 43

5 6 72 3

Page 23: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

23

Pass 4n = 5

At the end of Pass 4, the elements are sorted.

Implementing Bubble Sort Algorithm (Contd.)

arr

210 43

5 6 72 3

Page 24: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

24

Algorithm BubbleSort ( A [],n ) { 1. for ( pass = 1 to n-1 )

1.1 for ( j= 0 to n – 1 – pass ) 1.1.1 If ( A [j] > A [j+1] ) 1.1.1.1 t = A [j] 1.1.1.2 A [j] = A [j+1] 1.1.1.3 A [j+1] = t }

Implementing Bubble Sort Algorithm (Contd.)

Page 25: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

25

The efficiency of a sorting algorithm is measured in terms of number ofcomparisons.In bubble sort, there are n – 1 comparisons in Pass 1, n – 2 comparisons inPass 2, and so on.Total number of comparisons = (n – 1) + (n – 2) + (n – 3) + … + 3 + 2 + 1 = n(n– 1)/2. n(n – 1)/2 is of O(n2) order. Therefore, the bubble sort algorithm is of theorder O(n2).

Determining the Efficiency of Bubble Sort Algorithm

Page 26: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

26

What is the order of growth of the bubble sort algorithm?

Just a minute

Answer:The bubble sort algorithm has a quadratic order of growth.

Page 27: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

27

While implementing bubble sort algorithm, how manycomparisons will be performed in Pass 1.

Just a minute

Answer:n – 1 comparisons

Page 28: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

28

Selection sort algorithm:Has a quadratic order of growth and is therefore suitable for sortingsmall lists onlyScans through the list iteratively, selects one item in each scan, andmoves the item to its correct position in the list

Sorting Data by Using Selection Sort

Page 29: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

29

To understand the implementation of selection sort algorithm,consider an unsorted list of numbers stored in an array.

Implementing Selection Sort Algorithm

arr

210 43

120 10 200105 20

Page 30: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

30

Let us sort this unsorted list.

Implementing Selection Sort Algorithm (Contd.)

arr

210 43

120 10 200105 20

Page 31: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

31

Pass 1n = 5

Search the minimum value in the array, arr[0] to arr[n – 1].

Implementing Selection Sort Algorithm (Contd.)

arr

210 43

120 10 200105 20

min

Page 32: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

32

Pass 1n = 5

Search the minimum value in the array, arr[0] to arr[n – 1].Swap the minimum value with the value at index 0.

Implementing Selection Sort Algorithm (Contd.)

arr

210 43

120 10 200105 20

min

Swap

10510

Page 33: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

33

Pass 1n = 5

Search the minimum value in the array, arr[0] to arr[n – 1].Swap the minimum value with the value at index 0.

Implementing Selection Sort Algorithm (Contd.)

arr

210 43

120 105 20010 20

The smallest value is placed at its correct location after Pass 1

Page 34: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

34

Pass 2n = 5

Search the minimum value in the array, arr[1] to arr[n – 1].Swap the minimum value with the value at index 1.

Implementing Selection Sort Algorithm (Contd.)

arr

210 43

120 105 20010 20

min

20 120

Swap

Page 35: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

35

Pass 2n = 5

Search the minimum value in the array, arr[1] to arr[n – 1].Swap the minimum value with the value at index 1.

Implementing Selection Sort Algorithm (Contd.)

arr

210 43

120 200 2010510arr

210 43

20 200 120

The second smallest value is placed at its correct location after Pass 2

10510

Page 36: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

36

Pass 3n = 5

Search the minimum value in the array, arr[2] to arr[n – 1].Swap the minimum value with the value at index 2.

Implementing Selection Sort Algorithm (Contd.)

arr

210 43

120 200 2010510arr

210 43

20 200

min

10 120105

Page 37: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

37

Pass 3n = 5

Search the minimum value in the array, arr[2] to arr[n – 1].Swap the minimum value with the value at index 2.

Implementing Selection Sort Algorithm (Contd.)

arr

210 43

120 200 2010510arr

210 43

20 200

minThe third smallest value is placed at its correct location after Pass 3

12010510

Page 38: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

38

Pass 4n = 5

Search the minimum value in the array, arr[3] to arr[n – 1].Swap the minimum value with the value at index 3.

Implementing Selection Sort Algorithm (Contd.)

arr

210 43

120 200 2010510arr

210 43

20 200 12010 105

min

120 200

Swap

Page 39: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

39

Pass 4n = 5

Search the minimum value in the array, arr[3] to arr[n – 1].Swap the minimum value with the value at index 3.

Implementing Selection Sort Algorithm (Contd.)

arr

210 43

120 200 2010510arr

210 43

20

The fourth smallest value is placed at its correct location after Pass 4

120 20010 105

Page 40: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

40

Pass 4n = 5

The list is now sorted.

Implementing Selection Sort Algorithm (Contd.)

arr

210 43

120 200 2010510arr

210 43

20 20010 105 120

Page 41: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

41

Algorithm SelectionSort ( A [],n ) {

1. for( i = 0 to n-2 ) 1.1 Set min = i 1.2 for ( j= i+1 to n-1 )

1.2.1 if ( A [j] < A [min] ) 1.2.1.1 min = j 1.3 swap A [i] and A [min] }

Implementing Selection Sort Algorithm (Contd.)

Page 42: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

42

In selection sort, there are n – 1 comparisons during Pass 1 to findthe smallest element, n – 2 comparisons during Pass 2 to find thesecond smallest element, and so on. Total number of comparisons = (n – 1) + (n – 2) + (n – 3) + … + 3 +2 + 1 = n(n – 1)/2 n(n – 1)/2 is of O(n2) order. Therefore, the selection sortalgorithm is of the order O(n2).

Determining the Efficiency of Selection Sort Algorithm

Page 43: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

43

Read the following statement and specify whether it is true orfalse:

The efficiency of selection sort algorithm is same as that of thebubble sort algorithm.

Just a minute

Answer:True

Page 44: Bubble and Selection Sorts - ds.nathanielgmartin.comds.nathanielgmartin.com/wk02/W2L2-Bubble_Selection.pdf · 2 Objectives In this Week, you will learn to: Identify the algorithms

44

How many comparisons are performed in the second pass of theselection sort algorithm?

Just a minute

Answer:n – 2