You are on page 1of 16

Department of Computer Science and Engineering (CSE)

Topics to be covered
• HOW SELECTION SORT WORKS ?
• COMPLEXITY ANALYSIS OF SELECTION SORT.
• COMPARISON OF SELECTION SORT WITH BUBBLE SORT

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

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.

• The algorithm maintains two subarrays in a given array.


• The subarray which already sorted.
• The remaining subarray was unsorted.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Follow the below steps to solve the problem:


• Initialize minimum value(min_idx) to location 0.
• Traverse the array to find the minimum element in the
array.
• While traversing if any element smaller than min_idx is
found then swap both the values.
• Then, increment min_idx to point to the next element.
• Repeat until the array is sorted.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example: Selection Sort


1 2 3 4 5 6 7 8
77 33 44 11 88 22 66 55

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])

University Institute of Engineering (UIE) 5


Department of Computer Science and Engineering (CSE)
k=2 1 2 3 4 5 6 7 8
11 33 44 77 88 22 66 55 min=33,loc=2
11 33 44 77 88 22 66 55 min=33,loc=2
11 33 44 77 88 22 66 55 min=33,loc=2
11 33 44 77 88 22 66 55 min=33,loc=2
11 33 44 77 88 22 66 55 min=22,loc=6
11 33 44 77 88 22 66 55 min=22,loc=6
11 33 44 77 88 22 66 55 min=22,loc=6
swap(a[k],a[loc])
11 22 44 77 88 33 66 55 swap(a[2],a[6])

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])

University Institute of Engineering (UIE) 6


Department of Computer Science and Engineering (CSE)

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])

University Institute of Engineering (UIE) 7


Department of Computer Science and Engineering (CSE)

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])

University Institute of Engineering (UIE) 8


Department of Computer Science and Engineering (CSE)

Algorithm for Selection Sort


Algorithm : (selection_sort) SELECTION(A,N)
This algorithm sorts the array A with N elements.
1. Repeat steps 2 & 3 for K = 1,2…N-1:
2. Call MIN(A,K,N,LOC)
3. Interchange A[K] & A[LOC].]
Set TEMP=A[K], A[K]=A[LOC] & A[LOC]=TEMP
[End of step1 loop]
4. Exit.

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

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

// Selection sort in C++

#include <iostream>
using namespace std;

// function to swap the the position of two


elements
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

// function to print an array


void printArray(int array[], int size) {
for (int i = 0; i < size; i++) {
cout << array[i] << " ";
}
cout << endl;
}

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)
void selectionSort(int array[], int size) {
for (int step = 0; step < size - 1; step++) {
int min_idx = step;
for (int i = step + 1; i < size; i++) {

// To sort in descending order, change > to < in this line.


// Select the minimum element in each loop.
if (array[i] < array[min_idx])
min_idx = i;
}

// put min at the correct position


swap(&array[min_idx], &array[step]);
}
}

// 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)

Complexity of the Selection sort algorithm


Number of comparisons in the selection sort algorithm is independent of the original order
of the elements. N-1 comparisons required during pass1 to find the smallest element, n-2
in 2nd pass and so on.
f(n) =(n-1)+(n-2)+…..2+1= n(n-1)/2 = O(n2 )
Best case = O(n2 )

NOTE : SELECTION SORT IS NOT STABLE ALGORITHM


Space Complexity is O(1)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Comparison of Selection sort with Bubble Sort

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Selection sort is faster than Bubble Sort

In Selection sort, a maximum of n moves are made, whereas in Bubble Sort,


up to n moves are made for each element.

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.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

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/

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

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.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

THANKS

University Institute of Engineering (UIE)

You might also like