-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathSelection_sort.cpp
More file actions
58 lines (41 loc) · 779 Bytes
/
Selection_sort.cpp
File metadata and controls
58 lines (41 loc) · 779 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Selection sort in C++
#include<iostream>
using namespace std;
void input(int a[], int x)
{
cout<<"Enter array elements: \n";
for(int i=0; i<x; i++)
cin>>a[i];
cout<<"Input array: ";
for(int i=0; i<x; i++)
cout<<a[i]<<" ";
}
void sort(int a[], int x)
{
int temp;
for(int i=0; i<x; i++)
{
for(int j=i; j<x; j++)
{
if( a[i] >= a[j+1])
{
temp=a[i];
a[i]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\nSorted array: ";
for(int i=0; i<x; i++ )
cout<<a[i]<<" ";
}
int main()
{
int x;
cout<<"Enter number of elements: ";
cin>>x;
int a[x];
input(a,x);
sort(a,x);
return 0;
}