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 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
Quick Sort Mid = (0 + 7) /2 = 3 Swap between List[0] and List[mid]
Quick Sort 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
Quick Sort 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
Quick Sort 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
Quick Sort 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
Quick Sort 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
Quick Sort 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
Quick Sort 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
Quick Sort 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
Quick Sort 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
Quick Sort 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]
Quick Sort 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]
Quick Sort Now We are sure that number 6 is in the rigth place We need to complete the QuickSort and
Quick Sort 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] }
Quick Sort 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] }
Quick Sort 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] }
Quick Sort 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] }
Quick Sort 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] }
Quick Sort 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] }
Quick Sort 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,
Quick Sort 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 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.... }
Quick Sort