12 8 8 8 6 66 1722 Selection Sort Given n numbers to sort: Repeat the following n-1 times: Mark the...

9
12 8 12 12 8 6 12 6 6 12 12 17 22 Selection Sort Given n numbers to sort: Repeat the following n-1 times: Mark the first unsorted number Find the smallest unsorted number Swap the marked and smallest numbers 8 14 6 17 22 14

Transcript of 12 8 8 8 6 66 1722 Selection Sort Given n numbers to sort: Repeat the following n-1 times: Mark the...

Page 1: 12 8 8 8 6 66 1722 Selection Sort Given n numbers to sort: Repeat the following n-1 times:  Mark the first unsorted number  Find the smallest unsorted.

12 8812128

612661212 1722

Selection Sort

Given n numbers to sort: Repeat the following n-1 times:

Mark the first unsorted numberFind the smallest unsorted numberSwap the marked and smallest numbers

814

6 1722 14

Page 2: 12 8 8 8 6 66 1722 Selection Sort Given n numbers to sort: Repeat the following n-1 times:  Mark the first unsorted number  Find the smallest unsorted.

1414 1722 172222178

2212 12228 12226 17

Selection Sort

Given n numbers to sort: Repeat the following n-1 times:

Mark the first unsorted numberFind the smallest unsorted numberSwap the marked and smallest numbers

14

6 12

Page 3: 12 8 8 8 6 66 1722 Selection Sort Given n numbers to sort: Repeat the following n-1 times:  Mark the first unsorted number  Find the smallest unsorted.

Selection Sort Given n numbers to sort: Repeat the following n-1 times:

Mark the first unsorted number Find the smallest unsorted number Swap the marked and smallest numbers

How efficient is selection sort? In general, given n numbers to sort, it performs n2 comparisons

Why might selection sort be a good choice? Simple to write code Intuitive

Page 4: 12 8 8 8 6 66 1722 Selection Sort Given n numbers to sort: Repeat the following n-1 times:  Mark the first unsorted number  Find the smallest unsorted.

Selection Sort Given n numbers to sort: Repeat the following n-1 times:

Mark the first unsorted number Find the smallest unsorted number Swap the marked and smallest numbers

15 3 11 19 4 7

Try one!

Page 5: 12 8 8 8 6 66 1722 Selection Sort Given n numbers to sort: Repeat the following n-1 times:  Mark the first unsorted number  Find the smallest unsorted.

148 22221717814148

17

1481266

142214

12

2217178 2214814228 8222261212 6126

Bubble Sort

Given n numbers to sort: Repeat the following n-1 times:

For each pair of adjacent numbers: If the number on the left is greater than the number

on the right, swap them.

6 12 17

Page 6: 12 8 8 8 6 66 1722 Selection Sort Given n numbers to sort: Repeat the following n-1 times:  Mark the first unsorted number  Find the smallest unsorted.

178 12128

8128 812 1414 222217171266 12

Bubble Sort

Given n numbers to sort: Repeat the following n-1 times:

For each pair of adjacent numbers: If the number on the left is greater than the number

on the right, swap them.

8

1414 22221766

Page 7: 12 8 8 8 6 66 1722 Selection Sort Given n numbers to sort: Repeat the following n-1 times:  Mark the first unsorted number  Find the smallest unsorted.

Bubble Sort Given n numbers to sort: Repeat the following n-1 times:

For each pair of adjacent numbers: If the number on the left is greater than the number on the right,

swap them

How efficient is bubble sort? In general, given n numbers to sort, it performs n2

comparisons The same as selection sort

Is there a simple way to improve on the basic bubble sort? Yes! Stop after going through without making any swaps This will only help some of the time

Page 8: 12 8 8 8 6 66 1722 Selection Sort Given n numbers to sort: Repeat the following n-1 times:  Mark the first unsorted number  Find the smallest unsorted.

Bubble Sort Given n numbers to sort: Repeat the following n-1 times:

For each pair of adjacent numbers: If the number on the left is greater than the number on the

right, swap them

15 3 11 19 4 7

Try one!

Page 9: 12 8 8 8 6 66 1722 Selection Sort Given n numbers to sort: Repeat the following n-1 times:  Mark the first unsorted number  Find the smallest unsorted.

Sorting – Discussion QuestionsData on elementary school students is stored sorted by the age of the student. In preparation for track and field day, you wish to sort the students by their heights. Which of the sorting methods discussed today would you use? Explain.

While running a computer program to sort student exam scores using selection sort, the power goes out. Fortunately, the program was designed so it saved the scores after each swap. Just from looking at the saved scores, what information, if any, can you learn about the exam scores?

Would anything change if bubble sort was used instead?