-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselection.cpp
More file actions
52 lines (49 loc) · 904 Bytes
/
selection.cpp
File metadata and controls
52 lines (49 loc) · 904 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
#include<iostream>
using namespace std;
void selection_sort(int *array,int size);
void print_array(int *array,int size);
int main()
{
// time complexity of this alogo is big-oh(n^2)
int size;
cout<<"Enter size of array : ";
cin>>size;
int *array=new int[size];
cout<<"Now enter all element of array : ";
for(int i=0;i<size;i++)
{
cin>>array[i];
}
selection_sort(array,size);
print_array(array,size);
}
void selection_sort(int *array,int size)
{
int min=0;
for(int i=0;i<size-1;i++)
{
min=i;
// set supposed min here.
for(int j=i+1;j<size;j++)
{
if(array[j]<array[min])
{
min=j;
}
}
// now swap actual min with supossed min value in array.
int temp;
temp=array[min];
array[min]=array[i];
array[i]=temp;
}
}
void print_array(int *array,int size)
{
cout<<"Now element in array is : ";
for(int i=0;i<size;i++)
{
cout<<array[i]<<" ";
}
cout<<endl;
}