Skip to content

Commit

Permalink
Merge pull request #9 from AthaSSiN/master
Browse files Browse the repository at this point in the history
Added Algo for selection sort
  • Loading branch information
manishbisht committed Oct 10, 2019
2 parents a831be1 + df304f7 commit d788fbf
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Algorithms/Sorting/Selection sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <bits/stdc++.h>
using namespace std;
void generateArray(int a[], int n){
int i;
for(i = 0; i < n; i++){
a[i] = rand() % 100;
}
}
void sSort(int a[], int n){
int i, j, temp, loc, min;
for(i=0;i<n-1;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
min=a[j];
loc=j;
}
}

temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
}
int main()
{
int i, n, a[100];
cin>>n;
generateArray(a, n);
cout<<"Original Array: ";
for(i = 0; i < n; i++){
cout<<a[i]<<" ";
}
sSort(a, n);
cout<<"\nFinal Array: ";
for(i = 0; i < n; i++){
cout<<a[i]<<" ";
}
return 0;
}

0 comments on commit d788fbf

Please sign in to comment.