sorting algorithms 1. 2 selection sort: array-based lists list sorted by selecting elements in the...
TRANSCRIPT
![Page 1: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/1.jpg)
1
Sorting Algorithms
![Page 2: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/2.jpg)
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.
![Page 3: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/3.jpg)
3
List of 8 elements
Elements of list during the first iteration
Elements of list during the second iteration
![Page 4: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/4.jpg)
Quick Sort
5 7 11 6 15 9 3 2
0 1 2 3 4 5 6 7
Mid = (0 + 7) /2 = 3
Swap between List[0] and List[mid]
![Page 5: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/5.jpg)
Quick Sort
6 7 11 5 15 9 3 2
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
![Page 6: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/6.jpg)
Quick Sort
6 7 11 5 15 9 3 2
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
![Page 7: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/7.jpg)
Quick Sort
6 7 11 5 15 9 3 2
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
![Page 8: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/8.jpg)
Quick Sort
6 5 11 7 15 9 3 2
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
![Page 9: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/9.jpg)
Quick Sort
6 5 11 7 15 9 3 2
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
![Page 10: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/10.jpg)
Quick Sort
6 5 11 7 15 9 3 2
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
![Page 11: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/11.jpg)
Quick Sort
6 5 11 7 15 9 3 2
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
![Page 12: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/12.jpg)
Quick Sort
6 5 3 7 15 9 11 2
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
![Page 13: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/13.jpg)
Quick Sort
6 5 3 7 15 9 11 2
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
![Page 14: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/14.jpg)
Quick Sort
6 5 3 2 15 9 11 7
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]
![Page 15: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/15.jpg)
Quick Sort
2 5 3 6 15 9 11 7
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]
![Page 16: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/16.jpg)
Quick Sort
2 5 3 6 15 9 11 7
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
![Page 17: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/17.jpg)
Quick Sort
2 5 3 6 15 9 11 7
0 1 2 3 4 5 6 7
2 5 3 15 9 11 7
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]
}
![Page 18: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/18.jpg)
Quick Sort
2 5 3 6 15 9 11 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]
}
0 1 2 3 4 5 6 7
0 1 2 4 5 6 7
![Page 19: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/19.jpg)
Quick Sort
2 5 3 6 15 9 11 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]
}
0 1 2 3 4 5 6 7
0 1 2 4 5 6 7
![Page 20: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/20.jpg)
Quick Sort
2 5 3 6 15 9 11 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]
}
0 1 2 3 4 5 6 7
0 1 2 4 5 6 7
![Page 21: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/21.jpg)
Quick Sort
2 5 3 6 15 9 11 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]
}
0 1 2 3 4 5 6 7
0 1 2 4 5 6 7
![Page 22: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/22.jpg)
Quick Sort
2 5 3 6 15 9 11 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]
}
0 1 2 3 4 5 6 7
0 1 2 4 5 6 7
![Page 23: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/23.jpg)
Quick Sort
2 5 3 6 15 9 11 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
0 1 2 3 4 5 6 7
0 1 2 4 5 6 7
![Page 24: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/24.jpg)
Quick Sort
2 5 3 6 15 9 11 7
3 2 5 7 9 11 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....
}
3 2 7 11 15
0 1 4 6 7
Do NothingMid = (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
![Page 25: 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](https://reader035.vdocument.in/reader035/viewer/2022081721/56649d215503460f949f604d/html5/thumbnails/25.jpg)
Quick Sort
2 5 3 6 15 9 11 7
3 2 5 7 9 11 15
2 3 7 11 15
0 1 4 6 7
3 15
1 7
2 3 5 6 7 9 11 15
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
0 1 2 4 5 6 7