/* Bismillahir Rahmanir Rahim
Implementation of Selection-Sort using C++
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
int ar[1000], i, j, n, temp, loc, mn;
cin>>n; // Enter the number of element
for(i=0; i<n; i++) cin>>ar[i]; // all elements
for(i=0; i<n-1; i++){
mn=ar[i]; // let, its the minimum
loc=i; // location of minimum
for(j=i+1; j<n; j++){
if(ar[j]<mn){
mn=ar[j]; // minimum updated
loc=j; // location of minimum
}
}
temp=ar[i];
ar[i]=ar[loc];
ar[loc]=temp;
}
for(i=0; i<n; i++) cout<<ar[i]<<" ";
cout<<endl;
return 0;
}
|
Monday, April 24, 2017
Selection Sort - Implementation with C++
Subscribe to:
Post Comments (Atom)
-
#include<bits/stdc++.h> #define ll long long using namespace std ; ll n , k , t_case ; ll bigmod ( ll b , ll p , ll m...
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ...
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3...
This comment has been removed by a blog administrator.
ReplyDelete