Professional Documents
Culture Documents
1.2.3.3 Selection Sort
1.2.3.3 Selection Sort
Topics to be covered
• HOW SELECTION SORT WORKS ?
• COMPLEXITY ANALYSIS OF SELECTION SORT.
• COMPARISON OF SELECTION SORT WITH BUBBLE SORT
Selection Sorting
The selection sort algorithm sorts an array by repeatedly
finding the minimum element (considering ascending
order) from the unsorted part and putting it at the
beginning.
k=1 1 2 3 4 5 6 7 8
77 33 44 11 88 22 66 55 min=77, loc=1
77 33 44 11 88 22 66 55 min=33, loc=2
77 33 44 11 88 22 66 55 min=33,loc=2
77 33 44 11 88 22 66 55 min=11,loc=4
77 33 44 11 88 22 66 55 min=11,loc=4
77 33 44 11 88 22 66 55 min=11,loc=4
77 33 44 11 88 22 66 55 min=11,loc=4
77 33 44 11 88 22 66 55 min=11,loc=4
swap(a[k],a[loc])
11 33 44 77 88 22 66 55 swap(a[1],a[4])
k=3 1 2 3 4 5 6 7 8
11 22 44 77 88 33 66 55 min=44,loc=3
11 22 44 77 88 33 66 55 min=44,loc=3
11 22 44 77 88 33 66 55 min=44,loc=3
11 22 44 77 88 33 66 55 min=33,loc=6
11 22 44 77 88 33 66 55 min=33,loc=6
11 22 44 77 88 33 66 55 min=33,loc=6
swap(a[k],a[loc])
11 22 33 77 88 44 66 55 swap(a[3],a[6])
k=4 1 2 3 4 5 6 7 8
11 22 33 77 88 44 66 55 min=77,loc=4
11 22 33 77 88 44 66 55 min=77,loc=4
11 22 33 77 88 44 66 55 min=44,loc=6
11 22 33 77 88 44 66 55 min=44,loc=6
11 22 33 77 88 44 66 55 min=44,loc=6
swap(a[k],a[loc])
11 22 33 44 88 77 66 55 swap(a[4],a[6])
k=5 1 2 3 4 5 6 7 8
11 22 33 44 88 77 66 55 min=88,loc=5
11 22 33 44 88 77 66 55 min=77,loc=6
11 22 33 44 88 77 66 55 min=66,loc=7
11 22 33 44 88 77 66 55 min=55,loc=8
swap(a[k],a[loc])
11 22 33 44 55 77 66 88 swap(a[5],a[8])
k=6 1 2 3 4 5 6 7 8
11 22 33 44 55 77 66 88 min=77,loc=6
11 22 33 44 55 77 66 88 min=66,loc=7
11 22 33 44 55 77 66 88 min=66,loc=7
swap(a[k],a[loc])
11 22 33 44 55 66 77 88 swap(a[6],a[7])
k=7 1 2 3 4 5 6 7 8
11 22 33 44 55 66 77 88 min=77,loc=7
11 22 33 44 55 66 77 88 min=77,loc=7
no
swap(a[k],a[loc])
swap(a[7],a[7])
Algorithm: MIN(A,K,N,LOC)
This algorithm finds the location LOC of the smallest element among A[K], A[K+1], …+A[N]
5. Set MIN=A[k] & LOC=K. [Initializes pointers]
6. Repeat for J=K+1,K+2,..N:
If MIN>A[J], then: Set MIN=A[J] & LOC=J
[End of loop]
3. Return
#include <iostream>
using namespace std;
// driver code
int main() {
int data[] = {20, 12, 10, 15, 2};
int size = sizeof(data) / sizeof(data[0]);
selectionSort(data, size);
cout << "Sorted array in Acsending Order:\n";
printArray(data, size);
}
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
Selection sort is better than Bubble sort due to less swapping required.
Selection sort becomes even more efficient than Bubble sort if the list is
more larger.
References
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline
Series, Tata McGraw Hill.
• Goodrich, Michael T., Tamassia, Roberto, and Mount,
David M., “Data Structures and Algorithms in C++”, Wiley
Student Edition.
• https://www.tutorialspoint.com/
data_structures_algorithms/sorting_algorithms.htm
• https://www.geeksforgeeks.org/sorting-algorithms/
Books Recommended
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline
Series, Tata McGraw Hill.
• Gilberg/Forouzan,” Data Structure with C ,Cengage
Learning.
• Augenstein,Moshe J , Tanenbaum, Aaron M, “Data
Structures using C and C++”, Prentice Hall of India.
THANKS