1 / 108

Sorting

Sorting. Sorting. Sorting is one of the most fundamental tasks that programmers can learn. It requires arrays. Data may be sorted in one of two ways ascending order (low to high) descending order (high to low). Sorting methods. There are many different ways to sort a list of items.

devonn
Télécharger la présentation

Sorting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sorting

  2. Sorting • Sorting is one of the most fundamental tasks that programmers can learn. • It requires arrays. • Data may be sorted in one of two ways • ascending order (low to high) • descending order (high to low)

  3. Sorting methods • There are many different ways to sort a list of items. • Selection sort • Insertion sort • Bubble sort • Quick sort • Merge sort

  4. Selection sort • There are many versions of this sort. • Your text shows you how to write one that looks for the smallest item and moves it up during a set of passes through the array (p. 571-572) • Here is another version. It searches for the largest unsorted element in each pass and moves it down into place.

  5. Selection sort algorithm • Assume n element list • Start by assuming that the largest item is the first one. • Go down the list and, if any item is larger than the first one then save it’s location and make it the largest. • When you have gone through all elements exchange the last element and the element in the location of the largest. • Now you have one item in order.

  6. Algorithm (continued) • Makes n-1 passes through the list, searching for the largest and swap it with the next bottom element. • When you have made n-1 passes the array is guaranteed to be sorted.

  7. Key algorithm: Swap • At the heart of every sort routine is a subroutine that allows you to swap the contents of two different elements. • This is a critical operation, but must be implemented the right way or you will only succeed in scrambling up your data.

  8. A bad Swap subroutine SUBROUTINE Swap(a, b) INTEGER, INTENT(INOUT) :: a, b a = b b = a END SUBROUTINE Swap Swapping seems obvious. Take the contents of one variable and place them in another and the contents of the other and place them in the first. Unfortunately, there is a problem.

  9. Illustrated bad swap a b 6 4

  10. Illustrated bad swap a b 6 4 a = b

  11. Illustrated bad swap a b 4 4 The value 6, in a, is replaced by the value 4, in b.

  12. Illustrated bad swap a b 4 4 a = b b = a This makes no sense because the value originally in a has been destroyed!

  13. The Swap subroutine SUBROUTINE Swap(a, b) INTEGER, INTENT(INOUT) :: a, b INTEGER temp temp = a a = b b = temp END SUBROUTINE SelectionSort

  14. Illustrated good swap a b 6 4 temp ?

  15. Illustrated good swap a b 6 4 temp temp = a ?

  16. Illustrated good swap a b 6 4 temp temp = a 6

  17. Illustrated good swap a b 6 4 temp temp = a a = b 6

  18. Illustrated good swap a b 4 4 temp temp = a a = b 6

  19. Illustrated good swap a b 4 4 temp temp = a a = b b = temp 6

  20. Illustrated good swap a b 4 6 temp temp = a a = b b = temp 6

  21. The swap is finished a b 4 6 temp temp = a a = b b = temp 6

  22. The SelectionSort subroutine SUBROUTINE SelectionSort(A, n) INTEGER, INTENT(IN) :: n INTEGER, DIMENSION(n), INTENT(INOUT) :: A INTEGER maxPos, currentPos, Max, i, j, last DO i = 1, n-1 maxPos = I last = n+1-i DO currentPos = 2, last IF (A(currentPos) > A(maxPos)) maxPos = currentPos ENDDO SWAP(A(maxPos), A(last)) ENDDO END SUBROUTINE SelectionSort

  23. The operation of Selection Sort

  24. Selection Sort example A(1) A(2) A(3) A(4) A(5) 2 0 maxPos 5 n 7 3 1 currPos 5 4 last 6

  25. Selection Sort example A(1) A(2) A(3) A(4) A(5) 2 1 maxPos 5 n 7 3 2 currPos 5 5 last 6

  26. Selection Sort example A(1) A(2) A(3) A(4) A(5) 2 2 maxPos 5 n 7 3 3 currPos 5 5 last 6

  27. Selection Sort example A(1) A(2) A(3) A(4) A(5) 2 2 maxPos 5 n 7 3 3 currPos 5 5 last 6

  28. Selection Sort example A(1) A(2) A(3) A(4) A(5) 2 2 maxPos 5 n 7 3 5 currPos 5 5 last 6

  29. Swap, after one pass A(1) A(2) A(3) A(4) A(5) 2 2 maxPos 5 n 6 3 5 currPos 5 5 last 7

  30. Selection Sort A(1) A(2) A(3) A(4) A(5) 2 1 maxPos 5 n 6 3 2 currPos 5 4 last 7

  31. Selection Sort A(1) A(2) A(3) A(4) A(5) 2 2 maxPos 5 n 6 3 2 currPos 5 4 last 7

  32. Selection Sort A(1) A(2) A(3) A(4) A(5) 2 2 maxPos 5 n 6 3 3 currPos 5 4 last 7

  33. Selection Sort A(1) A(2) A(3) A(4) A(5) 2 2 maxPos 5 n 6 3 4 currPos 5 4 last 7

  34. Selection Sort A(1) A(2) A(3) A(4) A(5) 2 2 maxPos 5 n 5 3 4 currPos 6 5 last 7

  35. Selection Sort A(1) A(2) A(3) A(4) A(5) 2 1 maxPos 5 n 5 3 2 currPos 6 3 last 7

  36. Selection Sort A(1) A(2) A(3) A(4) A(5) 2 2 maxPos 5 n 5 3 2 currPos 6 3 last 7

  37. Selection Sort A(1) A(2) A(3) A(4) A(5) 2 2 maxPos 5 n 5 3 3 currPos 6 3 last 7

  38. Selection Sort A(1) A(2) A(3) A(4) A(5) 2 2 maxPos 5 n 3 5 3 currPos 6 3 last 7

  39. Selection Sort A(1) A(2) A(3) A(4) A(5) 2 1 maxPos 5 n 3 5 2 currPos 6 2 last 7

  40. Selection Sort A(1) A(2) A(3) A(4) A(5) 2 2 maxPos 5 n 3 5 2 currPos 6 2 last 7

  41. Selection Sort A(1) A(2) A(3) A(4) A(5) 2 1 maxPos 5 n 3 5 1 currPos 6 1 last 7

  42. Result after each pass A(1) A(2) A(3) A(4) A(5) 2 2 7 6 3 3 5 5 6 7

  43. Result after each pass A(1) A(2) A(3) A(4) A(5) 2 2 2 7 6 5 3 3 3 5 5 6 6 7 7

  44. Result after each pass A(1) A(2) A(3) A(4) A(5) 2 2 2 7 6 5 3 3 3 5 5 6 6 7 7

  45. Result after each pass A(1) A(2) A(3) A(4) A(5) 2 2 2 2 7 6 5 3 3 3 3 5 5 5 6 6 6 7 7 7

  46. Result after each pass A(1) A(2) A(3) A(4) A(5) 2 2 2 2 7 6 5 3 3 3 3 5 5 5 6 6 6 7 7 7

  47. Result after each pass A(1) A(2) A(3) A(4) A(5) 2 2 2 2 2 7 6 5 3 3 3 3 3 5 5 5 5 6 6 6 6 7 7 7 7

  48. Result after each pass A(1) A(2) A(3) A(4) A(5) 2 2 2 2 2 7 6 5 3 3 3 3 3 5 5 5 5 6 6 6 6 7 7 7 7

  49. Result after each pass A(1) A(2) A(3) A(4) A(5) 2 2 2 2 2 2 7 6 5 3 3 3 3 3 3 5 5 5 5 5 6 6 6 6 6 7 7 7 7 7

  50. Shortcuts for selection sort • For your next lab you will implement the selection sort from pages 571-572. • This uses some interesting new FORTRAN functions.

More Related