//Wap to
sort an array using selection sort
#include <iostream.h>
#include <conio.h>
#define MAX 10
class selsort
{
int arr[MAX],n;
public:
void getdata();
void showdata();
void sortLogic();
};
void selsort :: getdata()
{
cout<<"Enter the no of elements in array : ";
cin>>n;
cout<<"Enter the elements : "<<endl;
for(int
i=0;i<n;i++)
cin>>arr[i];
}
void selsort :: showdata()
{
cout<<"\nAfter sorting elements are : ";
for(int
i=0;i<n;i++)
cout<<arr[i]<<"
";
}
void selsort :: sortLogic()
{
int temp,min;
for(int i=0;i<n;i++)
{
min=i;
for(int
j=i+1;j<n;j++)
{
if(arr[min]
> arr[j])
{
min=j;
}
}
temp =
arr[min];
arr[min] =
arr[i];
arr[i] = temp;
cout<<"\n
arr[min] = "<<arr[min]<<"
arr[i] = "<<arr[i];
}
}
void main()
{
clrscr();
selsort obj;
obj.getdata();
obj.sortLogic();
obj.showdata();
getch();
}
No comments :
Post a Comment