forked from krimanisha/Hacktoberfest21
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection_sort.cpp
More file actions
41 lines (33 loc) · 816 Bytes
/
Selection_sort.cpp
File metadata and controls
41 lines (33 loc) · 816 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
#include<iostream>
using namespace std;
void selection(int a[], int n)
{
int i, j, small;
for (i = 0; i < n-1; i++)
{
small = i;
for (j = i+1; j < n; j++)
if (a[j] < a[small])
small = j;
int temp = a[small];
a[small] = a[i];
a[i] = temp;
}
}
void printArr(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
cout<< a[i] <<" ";
}
int main()
{
int a[] = { 100, 10, 20, 30, 50, 40, 90, 80, 70, 60 };
int n = sizeof(a) / sizeof(a[0]);
cout<< "Before sorting array elements are - "<<endl;
printArr(a, n);
selection(a, n);
cout<< "\nAfter sorting array elements are - "<<endl;
printArr(a, n);
return 0;
}