Successfully reported this slideshow.
Upcoming SlideShare
×

# 3.4 selection sort

1,923 views

Published on

Data Structure-Selection Sort

Published in: Education
• Full Name
Comment goes here.

Are you sure you want to Yes No

Are you sure you want to  Yes  No

Are you sure you want to  Yes  No

Are you sure you want to  Yes  No

Are you sure you want to  Yes  No

### 3.4 selection sort

1. 1. Selection Sort
2. 2. Selection Sort: Idea 1. We have two group of items:  sorted group, and  unsorted group 1. Initially, all items are in the unsorted group. The sorted group is empty.  We assume that items in the unsorted group unsorted.  We have to keep items in the sorted group sorted.
3. 3. Selection Sort: Cont’d 1. Select the “best” (eg. smallest) item from the unsorted group, then put the “best” item at the end of the sorted group. 2. Repeat the process until the unsorted group becomes empty.
4. 4. Selection Sort Example 35 65 30 60 20 scan 0-4, smallest 20 swap 35 and 20 20 65 30 60 35 scan 1-4, smallest 30 swap 65 and 30 20 30 65 60 35 scan 2-4, smallest 35 swap 65 and 35 Chapter 10: Sorting 4
5. 5. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
6. 6. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
7. 7. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
8. 8. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
9. 9. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
10. 10. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
11. 11. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
12. 12. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted  Largest
13. 13. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
14. 14. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
15. 15. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
16. 16. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
17. 17. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
18. 18. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
19. 19. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
20. 20. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted  Largest
21. 21. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
22. 22. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
23. 23. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
24. 24. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
25. 25. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
26. 26. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
27. 27. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted  Largest
28. 28. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
29. 29. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
30. 30. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
31. 31. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
32. 32. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
33. 33. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted  Largest
34. 34. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
35. 35. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
36. 36. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
37. 37. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
38. 38. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted  Largest
39. 39. Selection Sort 1 2 3 4 5 6 Comparison Data Movement Sorted
40. 40. Selection Sort 1 2 3 4 5 6 Comparison Data Movement Sorted DONE!
41. 41. 4240 2 1 3 3 4 0 -1 655843 40 2 1 43 3 4 0 -1 42 65583 40 2 1 43 3 4 0 -1 58 3 6542 40 2 1 43 3 65 0 -1 58 3 42 4 Selection Sort: Example
42. 42. 4240 2 1 3 3 4 0 655843-1 42-1 2 1 3 3 4 0 65584340 42-1 2 1 3 3 4 655843400 42-1 2 1 0 3 4 655843403 Selection Sort: Example
43. 43. 1 42-1 2 1 3 4 6558434030 42-1 0 3 4 6558434032 1 42-1 0 3 4 6558434032 1 420 3 4 6558434032-1 1 420 3 4 6558434032-1 Selection Sort: Example
44. 44. Selection Sort: Analysis  Running time: − Worst case: O(N2 ) − Best case: O(N2 )