forked from manishbisht/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection sort.cpp
More file actions
44 lines (43 loc) · 780 Bytes
/
Selection sort.cpp
File metadata and controls
44 lines (43 loc) · 780 Bytes
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
38
39
40
41
42
43
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;
}