forked from AdarshAddee/Hacktoberfest2022_for_Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection_sort.c
56 lines (46 loc) 路 1.41 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
/*
Author : Prathamesh Patil
In selection sort,
we get the least from array and place it at first position, then recursively leaving the first element do the same
*/
#include<stdio.h>
//declaration of display function
void display(int array[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", array[i]);
}
}
//declaration of funtion :- selection sort
void selection_sort(int array[], int size)
{
int least, i, j, temp, l;
for ( i = 0; i < size; i++) //loop for accessing the ith element from array
{
least = array[i]; //taking the i element as the least starting from 0
printf("Least : %d")
for ( j = i + 1; j < size; j++) //loop for comparing the array elements with ith element
{
if (least > array[j]) //getting the least element from the array
{
least = array[j]; //storing it in the least variable
l=j; //storing the index of least variable in l
}
}
//swap logic in least and ith element
temp = array[i];
array[i] = array[l];
array[l] = temp;
}
}
int main()
{
int array[5] = {23,11,1,35,21};
int size = 5;
//calling the selection_sort funtion
selection_sort(array, size);
//calling the display function
display(array, size);
return 0;
}