forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Selection_Sort.c
47 lines (39 loc) · 852 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
#include<stdio.h>
// Function for selection sort
void Selection_Sort(int array[], int size)
{
int min_index, temp, i, j;
for(i = 0; i < size - 1; i++)
{
min_index = i;
for(j = i + 1; j < size; j++)
if(array[j] < array[min_index])
min_index = j;
temp = array[i];
array[i] = array[min_index];
array[min_index] = temp;
}
}
// Function to print elements of array
void Print_Array(int array[], int size)
{
int i;
for(i = 0; i < size; i++)
printf("%d\t",array[i]);
printf("\n");
}
// Driver Function
int main()
{
int num;
scanf("%d", &num);
int array[num];
for(int i = 0; i < num; i++) {
scanf("%d", &array[i]);
}
Selection_Sort(array, num);
Print_Array(array, num);
return 0;
}
// Output
// 1 2 3 4 4 6 8