Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Algo for selection sort #9

Merged
merged 1 commit into from
Oct 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added Algo for selection sort
  • Loading branch information
AthaSSiN committed Oct 9, 2019
commit df304f7a428325acf22982fe1f28cfcb58449e0e
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;
}