//Selection Sort Program
#include<iostream>
using namespace std;
#include<conio.h>
void SelectionSort(int arr[], int size)
{
int min,pos;
for (int i = 0; i < size - 1; i++)
{
min = arr[i];
pos = i;
//Finding the smallest element
for (int j = i + 1; j < size; j++)
{
if (arr[j] < min)
{
min = arr[j];
pos = j;
}
}
//Swaping the smallest element to its accurate position
arr[pos] = arr[i];
arr[i] = min;
}
}
int main()
{
int size;
cout<<"\n Enter the size of the array : ";
cin>>size;
int arr[size];
cout<<"\n Enter the Elements in the array : ";
for (int i = 0; i < size; i++)
{
cin>>arr[i];
}
SelectionSort(arr, size);
cout<<"\n Sorted Array : ";
for (int i = 0; i < size; i++)
{
cout<<arr[i]<<" ";
}
getch();
return 0;
}
#include<iostream>
using namespace std;
#include<conio.h>
void SelectionSort(int arr[], int size)
{
int min,pos;
for (int i = 0; i < size - 1; i++)
{
min = arr[i];
pos = i;
//Finding the smallest element
for (int j = i + 1; j < size; j++)
{
if (arr[j] < min)
{
min = arr[j];
pos = j;
}
}
//Swaping the smallest element to its accurate position
arr[pos] = arr[i];
arr[i] = min;
}
}
int main()
{
int size;
cout<<"\n Enter the size of the array : ";
cin>>size;
int arr[size];
cout<<"\n Enter the Elements in the array : ";
for (int i = 0; i < size; i++)
{
cin>>arr[i];
}
SelectionSort(arr, size);
cout<<"\n Sorted Array : ";
for (int i = 0; i < size; i++)
{
cout<<arr[i]<<" ";
}
getch();
return 0;
}
No comments:
Post a Comment