0% found this document useful (0 votes)
36 views

Sorting Algorithms: Selection Sort

Selection sort is an in-place sorting algorithm that finds the minimum element from the unsorted part of the array and swaps it with the element in the current position, repeating this process until the array is fully sorted. It has a worst-case time complexity of O(n^2) because the outer loop runs n-1 times and the inner loop runs from n-i-1 comparisons each iteration. While simple to implement, selection sort is not efficient for large data sets due to its quadratic time complexity.

Uploaded by

Shovon Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Sorting Algorithms: Selection Sort

Selection sort is an in-place sorting algorithm that finds the minimum element from the unsorted part of the array and swaps it with the element in the current position, repeating this process until the array is fully sorted. It has a worst-case time complexity of O(n^2) because the outer loop runs n-1 times and the inner loop runs from n-i-1 comparisons each iteration. While simple to implement, selection sort is not efficient for large data sets due to its quadratic time complexity.

Uploaded by

Shovon Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

SORTING ALGORITHMS

SELECTION SORT
Selection Sort
(Cards Example)
Initial Configuration

(search all cards and find the largest)


Swap the two cards
As before, the swap is
performed in three steps.
Among the remaining cards
the king is the largest.

It will remain in place.

Sorted Unsorted But the algorithm may perform


Some empty operations
(ie., swap it with itself in place)
Among the remaining cards
the queen is the largest.

It will remain in place.

Sorted Unsorted But the algorithm may perform


Some empty operations
(i.e., swap it with itself in place)
Among the remaining cards
the Jack is the largest.

It will remain in place.

Sorted Unsorted But the algorithm may perform


Some empty operations
(i.e., swap it with itself in place)
As before, the swap is
performed in three steps.
We are down to the last card.
Because there is only one and
Because we know that it is
Smaller than all the rest
We don’t need to do anything
Else with it. This is why the
Sorted Unsorted Algorithm goes up to < N-1
All cards are now sorted.

Sorted
Selection Sort

[http://web.ics.purdue.edu/~cs154/lectures/lecture010.htm]
Example:
Selection
Sort

[http://web.ics.purdue.edu/~cs154/lectures/lecture010.htm]
Example: Selection Sort.c
// Selection Sort
#include <stdlib.h>
#define N 6
int 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");
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.
Advantages
– Easy to write
– Can be done “in place”
– Can be done on linked lists too (keep a tail pointer)
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.

You might also like