Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Sorting CS-240 Dick Steflik. Selection Sort Strategy Find the largest item by making n-1 compares, swap the largest item with the last item.

Similar presentations


Presentation on theme: "Selection Sorting CS-240 Dick Steflik. Selection Sort Strategy Find the largest item by making n-1 compares, swap the largest item with the last item."— Presentation transcript:

1 Selection Sorting CS-240 Dick Steflik

2 Selection Sort Strategy Find the largest item by making n-1 compares, swap the largest item with the last item. Do this n-1 times, swapping the largest item with the n-2, n-3…2 item

3 9572 Set biggest to first item 9 Biggest

4 Selection Sort 9572 Compare biggest to 5; biggest is greater than, do nothing 9 Biggest

5 Selection Sort 9572 Compare biggest to 7; biggest is greater than, do nothing 9 Biggest

6 9572 Compare biggest to 2; biggest is greater than, do nothing 9 Biggest

7 9572 Pass one done, swap biggest item with item at end of list 9 Biggest

8 9572 Notice that 9 is in the right place, start pass 2 9 Biggest

9 Selection Sort 9572 Set biggest to first item 2 Biggest

10 Selection Sort 9572 Compare biggest to 5; 5 is greater, set biggest to 5 5 Biggest

11 Exchange Sort 9 5 72 Compare 9 to 7, 9 is greater than 7 so swap them

12 Exchange Sort 9 5 7 2 Compare 9 to 2, 9 is greater than 2 so swap them

13 Exchange Sort 9572 Notice that 9 is where it belongs Start back at the first element...

14 Exchange Sort 9572 Compare 5 to 7, 5 is less than 7 so leave them alone

15 Exchange Sort 9572 Compare 7 to 2, 7 is greater than 2 so swap them

16 Exchange Sort 9572 Notice that 7 is now in the right place Start back at the first element

17 Exchange Sort 9572 Compare 5 to 2, 5 is grerater than 2 so swap them

18 Exchange Sort 9572 End of n-1 pass, sort is done.


Download ppt "Selection Sorting CS-240 Dick Steflik. Selection Sort Strategy Find the largest item by making n-1 compares, swap the largest item with the last item."

Similar presentations


Ads by Google