Skip to content

Commit bff9d43

Browse files
committed
Added Selection Sort for C language
1 parent 985b0fe commit bff9d43

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/c/SelectionSort.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdio.h>
2+
3+
void swap(int array[], int i, int j)
4+
{
5+
int temp = array[i];
6+
array[i] = array[j];
7+
array[j] = temp;
8+
}
9+
10+
void selection_sort(int array[], int n)
11+
{
12+
int i, j;
13+
for (i = 0; i < n; i++)
14+
{
15+
for (j = i + 1; j < n; j++)
16+
{
17+
if (array[i] > array[j])
18+
{
19+
swap(array, i, j);
20+
}
21+
}
22+
}
23+
}
24+
25+
int main()
26+
{
27+
int array_size = 10;
28+
int array[10] = {45, 7, 125, 18, 3, 5, 11, 107, 60, 4};
29+
30+
selection_sort(array, array_size);
31+
32+
printf("Sorted Array:\n");
33+
int i;
34+
for (i = 0; i < array_size; i++)
35+
printf("%d ", array[i]);
36+
37+
return 0;
38+
}

src/c/SelectionSort.exe

82.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)