-
Notifications
You must be signed in to change notification settings - Fork 25
/
selection_sort.c
53 lines (40 loc) · 886 Bytes
/
selection_sort.c
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
#include<stdio.h>
void Selection_sort(int [],int );
int main(){
int n,x;
printf("Enter the size of array : ");
scanf("%d",&n);
int arr[n];
printf("Enter the value of array : ");
for (int i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
printf("\nBefore Sort : \n\n ");
for (int i = 0; i < n; i++)
{
printf("%d ",arr[i]);
}
Selection_sort(arr,n);
printf("\nAfter Sort : \n\n ");
for (int i = 0; i < n; i++)
{
printf("%d ",arr[i]);
}
return 0;
}
void Selection_sort(int arr[],int n ){
for (int i = 0; i < n-1; i++)
{
int min = i;
for (int j = i+1; j < n; j++)
{
if(arr[min] > arr[j]){
min = j;
}
}
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}