-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection_sort.c
More file actions
61 lines (53 loc) · 1.05 KB
/
Copy pathSelection_sort.c
File metadata and controls
61 lines (53 loc) · 1.05 KB
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
60
61
#include<stdio.h>
int i,j, k, pos, size;
int smallest();
void selection_sort();
void main()
{
int ch;
printf("In which order do you wanna sort?\n 1.Ascending 2.Descending\n Enter your choice(number): ");
scanf("%d", &ch);
printf("Enter the size of array: ");
scanf("%d", &size);
int a[size];
printf("Enter the elements of array: ");
for(i=0;i<size;i++)
scanf("%d", &a[i]);
selection_sort(a);
switch(ch)
{
case 1:
printf(" Sorted array: ");
for(i=0; i<size; i++)
printf("%d ", a[i]);
break;
case 2:
printf(" Sorted array: ");
for(i=size-1; i>=0; i--)
printf("%d ", a[i]);
break;
}
}
int smallest(int arr[], int k, int pos)
{
int small = arr[k];
pos = k;
for(j=k+1; j<size; j++)
if(small > arr[j])
{
small = arr[j];
pos = j;
}
return pos;
}
void selection_sort(int arr[])
{
int temp;
for(k=0; k<size-1; k++)
{
pos = smallest(arr, k, pos);
temp = arr[k];
arr[k] = arr[pos];
arr[pos] = temp;
}
}