#include <iostream>
void selectionSort(int AR[],int size)
{
int i,j,temp;
for (i=0; i<size; i++)
{
for (j=i+1; j<=size; j++)
{
if ( AR[i] > AR[j] ) // sorts in ascending order
{
temp = AR[j];
AR[j] = AR[i];
AR[i] = temp;
}
}
}
}
int main()
{
int arr[20],size;
// prompts the user to enter the elements in the array
cout<<" Enter the no. of elements that you want to enter (max 20 ) : ";
cin>>size;
cout<<" Now enter the elements in the array ";
for (int i=0; i<size; i++)
{
cout<<" \n Element "<<i<<" : ";
cin>>arr[i];
}
selectionSort(arr,size); // calls the function to sort the array
cout<<" \n The sorted array is as follows ";
for (int i=0; i<size; i++)
{
std::cout<<" \n Element "<<i<<" : "<<arr[i];
}
return 0;
}
void selectionSort(int AR[],int size)
{
int i,j,temp;
for (i=0; i<size; i++)
{
for (j=i+1; j<=size; j++)
{
if ( AR[i] > AR[j] ) // sorts in ascending order
{
temp = AR[j];
AR[j] = AR[i];
AR[i] = temp;
}
}
}
}
int main()
{
int arr[20],size;
// prompts the user to enter the elements in the array
cout<<" Enter the no. of elements that you want to enter (max 20 ) : ";
cin>>size;
cout<<" Now enter the elements in the array ";
for (int i=0; i<size; i++)
{
cout<<" \n Element "<<i<<" : ";
cin>>arr[i];
}
selectionSort(arr,size); // calls the function to sort the array
cout<<" \n The sorted array is as follows ";
for (int i=0; i<size; i++)
{
std::cout<<" \n Element "<<i<<" : "<<arr[i];
}
return 0;
}
No comments:
Post a Comment