Selection Sort

 Selection Sort

In selection sort, the smallest value among the unsorted elements of the array is selected in every pass and inserted to its appropriate position into the array.

First, find the smallest element of the array and place it on the first position. Then, find the second smallest element of the array and place it on the second position. The process continues until we get the sorted array.

The array with n elements is sorted by using n-1 pass of selection sort algorithm.

  • In 1st pass, smallest element of the array is to be found along with its index pos. then, swap A[0] and A[pos]. Thus A[0] is sorted, we now have n -1 elements which are to be sorted.

  • In 2nd pas, position pos of the smallest element present in the sub-array A[n-1] is found. Then, swap, A[1] and A[pos]. Thus A[0] and A[1] are sorted, we now left with n-2 unsorted elements.

  • In n-1th pass, position pos of the smaller element between A[n-1] and A[n-2] is to be found. Then, swap, A[pos] and A[n-1].

Therefore, by following the above explained process, the elements A[0], A[1], A[2],...., A[n-1] are sorted.

Example

Consider the following array with 6 elements. Sort the elements of the array by using selection sort.

A = {10, 2, 3, 90, 43, 56}.

Pass

  Pos

  A[0]

A[1]

A[2]

A[3]

A[4]

A[5]


0

10

2

3

90

43

56

1

1

2

10

3

90

43

56

2

2

2

3

10

90

43

56

3

2

2

3

10

90

43

56

4

4

2

3

10

43

90

56

5

5

2

3

10

43

56

90

Sorted A = {2, 3, 10, 43, 56, 90}

SELECTION SORT(ARR, N)

  • Step 1: Repeat Steps 2 and 3 for K = 1 to N-1

  • Step 2: CALL SMALLEST(ARR, K, N, POS)

  • Step 3: SWAP A[K] with ARR[POS]
    [END OF LOOP]

  • Step 4: EXIT

SMALLEST (ARR, K, N, POS)

  • Step 1: [INITIALIZE] SET SMALL = ARR[K]

  • Step 2: [INITIALIZE] SET POS = K

  • Step 3: Repeat for J = K+1 to N -1
    IF SMALL > ARR[J]
    SET SMALL = ARR[J]
    SET POS = J
    [END OF IF]
    [END OF LOOP]

  • Step 4: RETURN POS

C Program

#include<stdio.h>  

int smallest(int[],int,int);  

void main ()  

{  

    int a[10] = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};  

    int i,j,k,pos,temp;  

    for(i=0;i<10;i++)  

    {  

        pos = smallest(a,10,i);  

        temp = a[i];  

        a[i]=a[pos];  

        a[pos] = temp;  

    }  

    printf("\nprinting sorted elements...\n");  

    for(i=0;i<10;i++)  

    {  

        printf("%d\n",a[i]);  

    }  

}  

int smallest(int a[], int n, int i)  

{  

    int small,pos,j;  

    small = a[i];  

    pos = i;  

    for(j=i+1;j<10;j++)  

    {  

        if(a[j]<small)  

        {  

            small = a[j];  

            pos=j;  

        }  

    }  

    return pos;  

}  


C++ Program

#include<iostream>  

using namespace std;  

int smallest(int[],int,int);  

int main ()  

{  

    int a[10] = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};  

    int i,j,k,pos,temp;  

    for(i=0;i<10;i++)  

    {  

        pos = smallest(a,10,i);  

        temp = a[i];  

        a[i]=a[pos];  

        a[pos] = temp;  

    }  

    cout<<"\n printing sorted elements...\n";  

    for(i=0;i<10;i++)  

    {  

        cout<<a[i]<<"\n";  

    }  

    return 0;  

}  

int smallest(int a[], int n, int i)  

{  

    int small,pos,j;  

    small = a[i];  

    pos = i;  

    for(j=i+1;j<10;j++)  

    {  

        if(a[j]<small)  

        {  

            small = a[j];  

            pos=j;  

        }  

    }  

    return pos;  

}  


Output:

printing sorted elements...

7

9

10

12

23

23

34

44

78

101


Comments

Popular posts from this blog

Huffman coding || Huffman coding with example || Huffman coding method || Huffman coding in c/c++ ||Huffman coding programe in c/c++/data structure /java || what is Huffman coding || Huffman complete,

Ada important question bank

Unix assignment question

Graphs in data structure, it's algorithm

Java question bank

M-way Trees

Data structure question bank

B Tree

Radix Sort

csa unit 01 part 01 basic of computers notes pdf