Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 number  Swap the marked and smallest numbers 8 14 6172214

2 14 17 22 1722 17 8 22 12 22 8 12 22 617 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 14 612

3 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 n 2 comparisons Why might selection sort be a good choice?  Simple to write code  Intuitive

4 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 153111947 Try one!

5 148 22 17 8 14 8 17 14 8 1266 1422 14 12 22 17 8 22 14 8 22 8 8 612 6 6 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. 612 17

6 8 12 8 8 8 8 14 22 17 1266 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 14 22 17 66

7 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 n 2 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

8 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 153111947 Try one!

9 Sorting – Discussion Questions Data 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?


Download ppt "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."

Similar presentations


Ads by Google