//********************SELECTION SORT******************************
#include<iostream>
using namespace std;
//swap function
void swap(int &x,int &y){
int temp;
temp = x;
x=y;
y= temp;
}
//display function
void display(int A[],int n){
for(int i=0;i<n;i++)
cout<<A[i]<<"\t";
}
//Selection Sort Function
int SelectionSort(int A[],int n){
for(int i=0;i<n-1;i++){
int iMin =i;
for(int j=i+1;j<n;j++){
if(A[j]<A[iMin]){
iMin=j;
}
}
swap(A[i],A[iMin]); //calling "swap" function
}
display(A,n); //calling "display" function
}
//main function
int main(){
//asking user for "# of array elements" AND "the values of those elements"
int n;
cout<<"Enter the number of elements in your array: ";
cin>>n;
int A[n];
for(int i=0;i<n;i++){
cout<<"Enter the "<<i<<"th element of your array: "<<endl;
cin>>A[i];
}
SelectionSort(A,n); //calling "selection sort"
}
Comments
Post a Comment