Selection Sort code in C++


 Other Type Of Sorting : Click here :: most important ::



#include<iostream.h>
#include<conio.h>
void display(int *arr,int size)
{
 cout<<endl;
 for(int i=0;i<=size;i++){
 cout<<arr[i]<<"\t";     }
}

int find_min_index(int *arr,int start,int n)
{
 int posmin=start;
 int index;
 for(index=start;index<=n;index++){
 display(arr,n);
 if(arr[index]<arr[posmin]){
 posmin=index;
 cout<<"\n Min index = "<<posmin<<"\t Value = "<<arr[posmin]<<"\n";
 }
 }
return posmin;
}

void selection_sort(int *arr,int n)
{
 int posmin,count,temp;
 for(count=0;count<=n;count++)
 {
 posmin=find_min_index(arr,count,n);
 temp=arr[posmin];
 arr[posmin]=arr[count];
 arr[count]=temp;
 }
}

void main()
{
int x[]={12,5,7,1,9,4,8};
clrscr();
selection_sort(x,6);
display(x,6);
getch();
}

No comments

Powered by Blogger.