Download - Selection sort
![Page 1: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/1.jpg)
SORTING ALGORITHMS
SELECTION SORT
![Page 2: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/2.jpg)
![Page 3: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/3.jpg)
![Page 4: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/4.jpg)
![Page 5: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/5.jpg)
![Page 6: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/6.jpg)
Selection Sort(Cards Example)
![Page 7: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/7.jpg)
Initial Configuration
(search all cards and find the largest)
![Page 8: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/8.jpg)
Swap the two cards
![Page 9: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/9.jpg)
As before, the swap is performed in three steps.
![Page 10: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/10.jpg)
Sorted Unsorted
Among the remaining cardsthe king is the largest.
It will remain in place.
But the algorithm may performSome empty operations(ie., swap it with itself in place)
![Page 11: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/11.jpg)
Sorted Unsorted
Among the remaining cardsthe queen is the largest.
It will remain in place.
But the algorithm may performSome empty operations(i.e., swap it with itself in place)
![Page 12: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/12.jpg)
Sorted Unsorted
Among the remaining cardsthe Jack is the largest.
It will remain in place.
But the algorithm may performSome empty operations(i.e., swap it with itself in place)
![Page 13: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/13.jpg)
As before, the swap is performed in three steps.
![Page 14: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/14.jpg)
Sorted Unsorted
We are down to the last card.Because there is only one and Because we know that it is Smaller than all the restWe don’t need to do anything Else with it. This is why the Algorithm goes up to < N-1
![Page 15: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/15.jpg)
Sorted
All cards are now sorted.
![Page 16: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/16.jpg)
Selection Sort
[http://web.ics.purdue.edu/~cs154/lectures/lecture010.htm]
![Page 17: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/17.jpg)
Example:SelectionSort
[http://web.ics.purdue.edu/~cs154/lectures/lecture010.htm]
![Page 18: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/18.jpg)
Example: Selection Sort.c
![Page 19: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/19.jpg)
// Selection Sort#include <stdlib.h>#define N 6int main() {
int a[N]= { 23, 78, 45, 8, 32, 56}; int i,j; // Sort the array using Selection Sort int minIndex; for(i=0; i < N-1; i++) { // find the minimum element in the unsorted part of the array minIndex=i; for(j=i+1; j < N; j++) if(a[j] < a[minIndex]) minIndex = j;
// Swap a[i] with the smallest element int temp = a[i]; a[i] = a[minIndex]; a[minIndex] = temp; } system("pause");
}
![Page 20: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/20.jpg)
Time Complexity• Analysis of the worst case:
– Outer loop executes N-1 times (no exch rqd. for final element).– Inner loop executes N-i-1 comparisons. (N-1) + (N-2) + … + 2 + 1 = N*(N-1)/2 = O(N2)The worst case occurs if the array is already sorted in descending
order. Best Case = O (n^2) Average Case = O(n^2)
Selection sort is an inplace sorting algorithm. So its takes constant O(1) space.
![Page 21: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/21.jpg)
Advantages– Easy to write– Can be done “in place”– Can be done on linked lists too (keep a tail
pointer)
![Page 22: Selection sort](https://reader035.vdocuments.us/reader035/viewer/2022070510/58ae49c91a28abad338b6373/html5/thumbnails/22.jpg)
Disadvantages– It is about N2 even in the best case for
comparisons.– So the running time (over all inputs) is
approximately O(N2).– The primary disadvantage of selection sort is
its poor efficiency when dealing with a huge list of items.