-
Notifications
You must be signed in to change notification settings - Fork 0
/
Selection_sort.c
57 lines (50 loc) · 1.05 KB
/
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
54
55
56
57
#include <stdio.h>
// Predefining Used funtion
void swap(int *a, int *b);
void Selection_Sort(int array[], int n);
// main funtion
int main()
{
int size;
printf("\nEnter size of array: ");
scanf("%d", &size);
printf("Enter elements of array: \n");
int array[size];
for (int i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
// calling sort funtion
Selection_Sort(array, size);
// printing final sorted array
printf("\nArray after sorting using Selection_sort : \n");
for (int i = 0; i < size; i++)
{
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
// swap funtion
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// sorting funtion using selection sort
void Selection_Sort(int array[], int size)
{
for (int i = 0; i < size - 1; i++)
{
int k = i;
for (int j = i + 1; j < size; j++)
{
if (array[j] < array[k])
{
k = j;
}
}
swap(&array[k], &array[i]);
}
}