Tuesday 28 January 2014

selection sorting

 SELECTION SORT IN C++


Searching and sorting are popular in elementry data structures. Although now a days many advanced sorting techniques are used to sort a set of data, the basic sorting methods are still popular and are the used frequently.

The algorithm works is selection sort works by finding the smallest element in the list and by assuming that we are doing sorting in ascending order we replace that smallest found element with first element of list in this way we put the smallest element of list at first  position . Then we go on repeating this task for rest of elements. We then find the second smallest element in the list and then replace it with second element of list and thus we have our first two elements of list in sorted order of ascending order .


 I have given  C++ code for selection sort . The algorithm for selection sort is quite simple. In the first iteration, 1st element is compared against all the other elements (from array index 1 to array index n). In the second iteration 2nd element is compared with the elements from array index 2 to n. This process is repeated n-1 times.

 Disadvantages of Selection Sort
  • It is slow as the sorting process is too long and time consuming so it is mainly used for sorting when N (number of elements is less)


 Advantages of Selection Sort


  • Selection sort is easy to implement and does not require additional storage
  • It implements better on small lists




Code For Selection Sort
//It is for 10 elements and to change number of elements change 10 arguement in smallest function call


#include<iostream.h>
#include<conio.h>

int small , a1=0 , loc;
int a[ 10 ];
int main( )
{
int n, temp, temp1, count, compare;
int smallest( int , int ) ;
clrscr( );
cout<<"enter number of elements you want to sort";
cin>>n;
count=n-1;
for(int i=0; i<=n-1; i++)
{
cout<<"ARRAY ["<<i<<"] :- ";
cin>>a[ i ];
}
for(int j1=0;j1<=n-1;j1++)
{
compare = smallest(10,a1);            // call smallest() function to calculate smallest element
temp1 = a[ j1 ];
a[ j1 ] = a[ loc ];
a[ loc ] = temp1;
a1 = a1 + 1;
}
cout<<endl;
cout<<" sorted array is ";cout<<endl;

for(i=0;i<=n-1;i++)                                      // Print sorted Array 
{
cout<<"ARRAY ["<<i<<"] :- ";
cout<<a[i];cout<<endl;
}
 getch();
}
int smallest(int n,int a1)      // function to calculate smaller number
{
for(int k=a1;k<=a1;k++)
{
small=a[k];                       // smaller is assigned first element
for(int j=k+1;j<=n-1;j++)
{
if(small>a[j])                       // if value smaller than small variable found
                                             then small = a[j] 

{                                                      
small = a[j];
loc = j;                                 // and location of smallest loc = j
}
}
}
return(loc);                              //return location of smallest element
     }



Share this

1 Response to "selection sorting"

  1. When someone writes an post he/she retains the image of a user in his/her mind that how a user can be aware of it.
    So that's why this article is perfect. Thanks!


    Feel free to visit my webpage :: gamespot gta 5

    ReplyDelete