forked from aayushi1499/Hacktoberfest2021-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelection Sort
59 lines (51 loc) · 844 Bytes
/
Selection Sort
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
59
#include <iostream>
using namespace std;
void swap(int *a,int *b)
{
int tmp=*a;
*a=*b;
*b=tmp;
}
int Selection_Sort(int arr[],int n)
{
int i,j,min=arr[0];
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(arr[j]<arr[min])
{
min=j;
}
}
swap(&arr[min],&arr[i]);
}
}
int dispaly(int arr[],int n)
{
for(int i=0;i<n;i++)
cout<<arr[i]<<" "<<endl<<endl;
}
int main()
{
int SIZE;
cout<<"Enter size of an aaray"<<endl;
cin>>SIZE;
int arr[SIZE];
int n=sizeof(arr)/sizeof(arr[0]);
cout<<"Enter elements for an array"<<endl;
for(int i=0;i<SIZE;i++)
{
cin>>arr[i];
}
cout<<"Unsorted Array elements are:"<<endl;
for(int i=0;i<SIZE;i++)
{
cout<<arr[i]<<endl;
}
Selection_Sort(arr,n);
cout<<"Sorted array elements are:"<<endl;
dispaly(arr,n);
return 0;
}