-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSelectionSort.c
52 lines (45 loc) · 905 Bytes
/
SelectionSort.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
/*
Selection Sort Algorithm:
Avg case : o(n2)
Not Adaptive : Since no benifit in already sorted array
Not Stable : since Order not Maintained !
*/
#include <stdio.h>
void SelectionSort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
int min = i;
for (int j = i + 1; j < n; j++)
{
if (arr[min] > arr[j])
{
min = j;
}
}
if (i != min)
{
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
}
void Display(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
void main()
{
int arr[10] = {7, -2, 0, 1, 4, 9, 11, 3, 5, 10};
int n = 10;
printf("Before Sort : ");
Display(arr, n);
SelectionSort(arr, n);
printf("After Sort : ");
Display(arr, n);
}