12 8 8 8 6 66 1722 Selection Sort Given n numbers to sort: Repeat the following n-1 times: Mark the...
-
Upload
milton-tucker -
Category
Documents
-
view
214 -
download
1
Transcript of 12 8 8 8 6 66 1722 Selection Sort Given n numbers to sort: Repeat the following n-1 times: Mark the...
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
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
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
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!
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
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
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
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!
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?