Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
50 views

Selection Sort

The document describes how the selection sort algorithm works. It has multiple steps to find the minimum value in each pass and swap it into its correct position, resulting in an increasingly sorted list. The algorithm involves repeatedly finding the minimum value among remaining elements and swapping it into the proper place, with an overall complexity of O(n^2) operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Selection Sort

The document describes how the selection sort algorithm works. It has multiple steps to find the minimum value in each pass and swap it into its correct position, resulting in an increasingly sorted list. The algorithm involves repeatedly finding the minimum value among remaining elements and swapping it into the proper place, with an overall complexity of O(n^2) operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Sorting Data by Using Selection Sort

Selection sort algorithm:


Has a quadratic order of growth and is therefore suitable for
sorting small lists only
Scans through the list iteratively, selects one item in each
scan, and moves the item to its correct position in the list
Implementing Selection Sort Algorithm

To understand the implementation of selection sort


algorithm, consider an unsorted list of numbers
stored in an array.

0 1 2 3 4
arr 105 120 10 200 20
Implementing Selection Sort Algorithm (Contd.)

Pass 1
n=5
Search the minimum value in the array, arr[0] to arr[n – 1].

0 1 2 3 4
arr 105 120 10 200 20

min
Implementing Selection Sort Algorithm (Contd.)

Pass 1
n=5
Search the minimum value in the array, arr[0] to arr[n – 1].
Swap the minimum value with the value at index 0.
Swap
0 1 2 3 4
arr 10
10 120 10
10 200 20

min
Implementing Selection Sort Algorithm (Contd.)

Pass 1
n=5
Search the minimum value in the array, arr[0] to arr[n – 1].
Swap the minimum value with the value at index 0.

0 1 2 3 4
arr 10 120 105 200 20

The smallest value is placed at its correct location after Pass 1


Implementing Selection Sort Algorithm (Contd.)

Pass 2
n=5
Search the minimum value in the array, arr[1] to arr[n – 1].
Swap the minimum value with the value at index 1.
Swap
0 1 2 3 4
arr 20 105 200 120
10 120 20

min
Implementing Selection Sort Algorithm (Contd.)

Pass 2
n=5
Search the minimum value in the array, arr[1] to arr[n – 1].
Swap the minimum value with the value at index 1.

0 1 2 3 4
arr 10 120
20 10
105200
200 120
2

The second smallest value is placed at its correct location after


Pass 2
Implementing Selection Sort Algorithm (Contd.)

Pass 3
n=5
Search the minimum value in the array, arr[2] to arr[n – 1].
Swap the minimum value with the value at index 2.
0 1 2 3 4
arr 10 120
20 10
105 200
20 120
2

min
Implementing Selection Sort Algorithm (Contd.)

Pass 3
n=5
Search the minimum value in the array, arr[2] to arr[n – 1].
Swap the minimum value with the value at index 2.

0 1 2 3 4
arr 10 120
20 10
105 200
20 120
2

min

The third smallest value is placed at its correct location after Pass 3
Implementing Selection Sort Algorithm (Contd.)

Pass 4
n=5
Search the minimum value in the array, arr[3] to arr[n – 1].
Swap the minimum value with the value at index 3.
Swap
0 1 2 3 4
arr 10 120
20 10
105 12
20 120
20
2

min
Implementing Selection Sort Algorithm (Contd.)

Pass 4
n=5
Search the minimum value in the array, arr[3] to arr[n – 1].
Swap the minimum value with the value at index 3.
0 1 2 3 4
arr 10 120
20 10 20 200
105 120 2

The fourth smallest value is placed at its correct location after Pass 4
Implementing Selection Sort Algorithm (Contd.)

Pass 4
n=5
The list is now sorted.

0 1 2 3 4
arr 10 120
20 10 20 200
105 120 2
Implementing Selection Sort Algorithm (Contd.)

Write an algorithm to implement selection sort.


Algorithm for selection sort:
1. Repeat steps 2 and 3 varying j from 0 to n – 2
2. Find the minimum value in arr[j] to arr[n – 1]:
a. Set min_index = j
b. Repeat step c varying i from j + 1 to n – 1
c. If arr[i] < arr[min_index]:
i. min_index = i

3. Swap arr[j] with arr[min_index]


Implementing Selection Sort Algorithm (Contd.)

inline void Swap(int &a, int &b)


{
int k = a;
a = b;
b = k;
}
void SelectionSort(int arr[], int arr_size)
{
for(int i = 0; i < arr_size - 1; ++i){
int min = i;
for(int j = i+1; j < arr_size; ++j)
if(arr[j] < arr[min])
min = j;
Swap(arr[min], arr[i]);
Implementing Selection Sort Algorithm (Contd.)
void PrintArray(int *array, int n)
{
for (int i = 0; i < n; ++i)
cout << array[i] << " "<<flush;
cout << endl;
}
int main()
{
int array[] = {94, 42, 50, 95, 333, 65, 54, 456, 1, 1234};
int n = sizeof(array)/sizeof(array[0]);

cout << "\nBefore Selection Sort :\n" <<endl;


PrintArray(array, n);
SelectionSort(array, n);

cout << "\nAfter Selection Sort :\n" <<endl;


PrintArray(array, n);
return (0);
}
Determining the Efficiency of Selection Sort Algorithm

In selection sort, there are n – 1 comparisons during Pass 1 to find the


smallest element, n – 2 comparisons during Pass 2 to find the second
smallest element, and so on.
Total number of comparisons = (n – 1) + (n – 2) + (n – 3) + … + 3 + 2
+ 1 = n(n – 1)/2
n(n – 1)/2 is of O(n2) order. Therefore, the selection sort algorithm is of
the order O(n2).
QUESTIONS
Thank you !

You might also like