1 / 25

Sorting Algorithms

Sorting Algorithms. Selection Sort: Array-Based Lists. List sorted by selecting elements in the list Select elements one at a time Move elements to their proper positions Selection sort operation Find location of the smallest element in unsorted list portion

weylin
Download Presentation

Sorting Algorithms

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 Algorithms

  2. Selection Sort: Array-Based Lists • List sorted by selecting elements in the list • Select elements one at a time • Move elements to their proper positions • Selection sort operation • Find location of the smallest element in unsorted list portion • Move it to top of unsorted portion of the list • First time: locate smallest item in the entire list • Second time: locate smallest item in the list starting from the second element in the list, and so on.

  3. List of 8 elements Elements of list during the first iteration Elements of list during the second iteration

  4. Quick Sort 0 1 2 3 4 5 6 7 5 7 11 6 15 9 3 2 Mid = (0 + 7) /2 = 3 Swap between List[0] and List[mid]

  5. Quick Sort 0 1 2 3 4 5 6 7 6 7 11 5 15 9 3 2 n Mid = (0 + 7) /2 = 3 Swap between List[0] and List[mid] smallIndex= 0 pivot = List [0]= 6 SmallIndex=0 for (n= 1 ; n<=7; n++) If(List[n] < pivot) { SmallIndex++; Swap between List[SmallIndex] and List [n] }

  6. Quick Sort 0 1 2 3 4 5 6 7 6 7 11 5 15 9 3 2 n Mid = (0 + 7) /2 = 3 Swap between List[0] and List[mid] smallIndex= 0 pivot = List [0]= 6 SmallIndex=0 for (n= 1 ; n<=7; n++) If(List[n] < pivot) { SmallIndex++; Swap between List[SmallIndex] and List [n] }

  7. Quick Sort 0 1 2 3 4 5 6 7 6 7 11 5 15 9 3 2 n Mid = (0 + 7) /2 = 3 Swap between List[0] and List[mid] smallIndex= 1 pivot = List [0]= 6 SmallIndex=0 for (n= 1 ; n<=7; n++) If(List[n] < pivot) { SmallIndex++; Swap between List[SmallIndex] and List [n] }

  8. Quick Sort 0 1 2 3 4 5 6 7 6 5 11 7 15 9 3 2 n Mid = (0 + 7) /2 = 3 Swap between List[0] and List[mid] smallIndex= 1 pivot = List [0]= 6 SmallIndex=0 for (n= 1 ; n<=7; n++) If(List[n] < pivot) { SmallIndex++; Swap between List[SmallIndex] and List [n] }

  9. Quick Sort 0 1 2 3 4 5 6 7 6 5 11 7 15 9 3 2 n Mid = (0 + 7) /2 = 3 Swap between List[0] and List[mid] smallIndex= 1 pivot = List [0]= 6 SmallIndex=0 for (n= 1 ; n<=7; n++) If(List[n] < pivot) { SmallIndex++; Swap between List[SmallIndex] and List [n] }

  10. Quick Sort 0 1 2 3 4 5 6 7 6 5 11 7 15 9 3 2 n Mid = (0 + 7) /2 = 3 Swap between List[0] and List[mid] smallIndex= 1 pivot = List [0]= 6 SmallIndex=0 for (n= 1 ; n<=7; n++) If(List[n] < pivot) { SmallIndex++; Swap between List[SmallIndex] and List [n] }

  11. Quick Sort 0 1 2 3 4 5 6 7 6 5 11 7 15 9 3 2 n Mid = (0 + 7) /2 = 3 Swap between List[0] and List[mid] smallIndex= 2 pivot = List [0]= 6 SmallIndex=0 for (n= 1 ; n<=7; n++) If(List[n] < pivot) { SmallIndex++; Swap between List[SmallIndex] and List [n] }

  12. Quick Sort 0 1 2 3 4 5 6 7 6 5 3 7 15 9 11 2 n Mid = (0 + 7) /2 = 3 Swap between List[0] and List[mid] smallIndex= 2 pivot = List [0]= 6 SmallIndex=0 for (n= 1 ; n<=7; n++) If(List[n] < pivot) { SmallIndex++; Swap between List[SmallIndex] and List [n] }

  13. Quick Sort 0 1 2 3 4 5 6 7 6 5 3 7 15 9 11 2 n Mid = (0 + 7) /2 = 3 Swap between List[0] and List[mid] smallIndex= 3 pivot = List [0]= 6 SmallIndex=0 for (n= 1 ; n<=7; n++) If(List[n] < pivot) { SmallIndex++; Swap between List[SmallIndex] and List [n] }

  14. Quick Sort 0 1 2 3 4 5 6 7 6 5 3 2 15 9 11 7 n Mid = (0 + 7) /2 = 3 Swap between List[0] and List[mid] smallIndex= 3 pivot = List [0]= 6 SmallIndex=0 for (n= 1 ; n<=7; n++) If(List[n] < pivot) { SmallIndex++; Swap between List[SmallIndex] and List [n] } The Last Step for this level is : Swap between List[smallindex] and List[0]

  15. Quick Sort 0 1 2 3 4 5 6 7 2 5 3 6 15 9 11 7 n Mid = (0 + 7) /2 = 3 Swap between List[0] and List[mid] smallIndex= 3 pivot = List [0]= 6 SmallIndex=0 for (n= 1 ; n<=7; n++) If(List[n] < pivot) { SmallIndex++; Swap between List[SmallIndex] and List [n] } The Last Step for this level is : Swap between List[smallindex] and List[0]

  16. Quick Sort 0 1 2 3 4 5 6 7 2 5 3 6 15 9 11 7 Now We are sure that number 6 is in the rigth place We need to complete the QuickSort 2 5 3 and 15 9 11 7

  17. Quick Sort 0 1 2 3 4 5 6 7 2 5 3 6 15 9 11 7 0 1 2 4 5 6 7 2 5 3 15 9 11 7 Mid= (0 + 2) /2 =1 Swap List[0] and List[mid] Pivot= List[0]= 5 For( n=1; n<=2; n++) If(List[n] < pivot) { SmallIndex++; Swap List[SamllIndext] with List[n] } Mid= (4 + 7) /2 =5 Swap List[4] and List[mid] Pivot= List[4]= 9 For( n=5; n<=7; n++) If(List[n] < pivot) { SmallIndex++; Swap List[SamllIndext] with List[n] }

  18. Quick Sort 0 1 2 3 4 5 6 7 2 5 3 6 15 9 11 7 0 1 2 4 5 6 7 5 2 3 15 9 11 7 Mid= (0 + 2) /2 =1 Swap List[0] and List[mid] Pivot= List[0]= 5 For( n=1; n<=2; n++) If(List[n] < pivot) { SmallIndex++; Swap List[SamllIndext] with List[n] } Mid= (4 + 7) /2 =5 Swap List[4] and List[mid] Pivot= List[4]= 9 For( n=5; n<=7; n++) If(List[n] < pivot) { SmallIndex++; Swap List[SamllIndext] with List[n] }

  19. Quick Sort 0 1 2 3 4 5 6 7 2 5 3 6 15 9 11 7 0 1 2 4 5 6 7 3 2 5 15 9 11 7 Mid= (0 + 2) /2 =1 Swap List[0] and List[mid] Pivot= List[0]= 5 For( n=1; n<=2; n++) If(List[n] < pivot) { SmallIndex++; Swap List[SamllIndext] with List[n] } Mid= (4 + 7) /2 =5 Swap List[4] and List[mid] Pivot= List[4]= 9 For( n=5; n<=7; n++) If(List[n] < pivot) { SmallIndex++; Swap List[SamllIndext] with List[n] }

  20. Quick Sort 0 1 2 3 4 5 6 7 2 5 3 6 15 9 11 7 0 1 2 4 5 6 7 3 2 5 9 15 11 7 Mid= (0 + 2) /2 =1 Swap List[0] and List[mid] Pivot= List[0]= 5 For( n=1; n<=2; n++) If(List[n] < pivot) { SmallIndex++; Swap List[SamllIndext] with List[n] } Mid= (4 + 7) /2 =5 Swap List[4] and List[mid] Pivot= List[4]= 9 For( n=5; n<=7; n++) If(List[n] < pivot) { SmallIndex++; Swap List[SamllIndext] with List[n] }

  21. Quick Sort 0 1 2 3 4 5 6 7 2 5 3 6 15 9 11 7 0 1 2 4 5 6 7 3 2 5 9 7 11 15 Mid= (0 + 2) /2 =1 Swap List[0] and List[mid] Pivot= List[0]= 5 For( n=1; n<=2; n++) If(List[n] < pivot) { SmallIndex++; Swap List[SamllIndext] with List[n] } Mid= (4 + 7) /2 =5 Swap List[4] and List[mid] Pivot= List[4]= 9 For( n=5; n<=7; n++) If(List[n] < pivot) { SmallIndex++; Swap List[SamllIndext] with List[n] }

  22. Quick Sort 0 1 2 3 4 5 6 7 2 5 3 6 15 9 11 7 0 1 2 4 5 6 7 3 2 5 7 9 11 15 Mid= (0 + 2) /2 =1 Swap List[0] and List[mid] Pivot= List[0]= 5 For( n=1; n<=2; n++) If(List[n] < pivot) { SmallIndex++; Swap List[SamllIndext] with List[n] } Mid= (4 + 7) /2 =5 Swap List[4] and List[mid] Pivot= List[4]= 9 For( n=5; n<=7; n++) If(List[n] < pivot) { SmallIndex++; Swap List[SamllIndext] with List[n] }

  23. Quick Sort 0 1 2 3 4 5 6 7 2 5 3 6 15 9 11 7 0 1 2 4 5 6 7 3 2 5 7 9 11 15 In this Level we are sure that Number 5 and Number 9 in the right places Now we have the following 4 array 3, 2 and Nothing and 7 and 11 ,15

  24. Quick Sort 0 1 2 3 4 5 6 7 2 5 3 6 15 9 11 7 0 1 2 4 5 6 7 3 2 5 7 9 11 15 0 1 4 6 7 3 2 7 11 15 Mid = (6+7)/2= 6 Swap List[6] and List[mid] Pivot =List[6] For (n=7; n<=7; n++) If(List[n]<pivot) { SmallIndex++; Do the Swap.... } Mid = (0+1)/2= 0 Swap List[0] and List[mid] Pivot =List[0] For (n=1; n<=1; n++) If(List[n]<pivot) { SmallIndex++; Do the Swap.... } Do Nothing

  25. Quick Sort 0 1 2 3 4 5 6 7 2 5 3 6 15 9 11 7 0 1 2 4 5 6 7 3 2 5 7 9 11 15 0 1 4 6 7 2 3 7 11 15 1 7 3 15 0 1 2 3 4 5 6 7 2 3 5 6 7 9 11 15

More Related