Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting Algorithms 1. 2 Selection Sort: Array-Based Lists List sorted by selecting elements in the list – Select elements one at a time – Move elements.

Similar presentations


Presentation on theme: "Sorting Algorithms 1. 2 Selection Sort: Array-Based Lists List sorted by selecting elements in the list – Select elements one at a time – Move elements."— Presentation transcript:

1 Sorting Algorithms 1

2 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 3 List of 8 elements Elements of list during the first iteration Elements of list during the second iteration

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

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

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

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

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

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

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

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

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

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

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

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

16 Quick Sort 2536159117 0 1 2 3 4 5 6 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 2536159117 0 1 2 3 4 5 6 7 253159117 0 1 2 4 5 6 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 2536159117 523159117 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] } 0 1 2 3 4 5 6 7 0 1 2 4 5 6 7

19 Quick Sort 2536159117 325159117 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] } 0 1 2 3 4 5 6 7 0 1 2 4 5 6 7

20 Quick Sort 2536159117 325915117 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] } 0 1 2 3 4 5 6 7 0 1 2 4 5 6 7

21 Quick Sort 2536159117 32597 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] } 0 1 2 3 4 5 6 7 0 1 2 4 5 6 7

22 Quick Sort 2536159117 32579 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] } 0 1 2 3 4 5 6 7 0 1 2 4 5 6 7

23 Quick Sort 2536159117 32579 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 0 1 2 3 4 5 6 7 0 1 2 4 5 6 7

24 Quick Sort 2536159117 32579 15 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.... } 3271115 0 1 4 6 7 Do Nothing 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.... } 0 1 2 3 4 5 6 7 0 1 2 4 5 6 7

25 Quick Sort 2536159117 32579 15 2371115 0 1 4 6 7 315 1 7 2356791115 0 1 2 3 4 5 6 7 0 1 2 4 5 6 7


Download ppt "Sorting Algorithms 1. 2 Selection Sort: Array-Based Lists List sorted by selecting elements in the list – Select elements one at a time – Move elements."

Similar presentations


Ads by Google