-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselection_sort.c
50 lines (38 loc) · 930 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
48
49
50
#include<stdio.h>
void bubble_sort(int *arr, int n)
{
int total_n =0, min, m_indx;
for (int i = 0 ; i < n ; i ++)
{
min = *(arr+i);
m_indx = i;
for (int j = i+1 ; j <n ; j ++)
{
if (*(arr+j) < min) {
min = *(arr+j);
m_indx = j;
}
total_n++;
}
*(arr+m_indx) = *(arr+i);
*(arr+i) = min;
}
printf(" Big O = %d \n", total_n);
}
void show(int arr[], int n)
{
printf("\n------------------------\n");
for(int i=0 ; i < n ; i++)
{
printf(" %d ", arr[i]);
}
printf("\n------------------------\n");
}
int main(){
int arr[] = {81,9,22,4,8,58,14,53,12};
printf("sizeof arr[] = %d\n", sizeof(arr));
show(arr, sizeof(arr)/sizeof(int) );
bubble_sort(arr, sizeof(arr)/sizeof(int));
show(arr, sizeof(arr)/sizeof(int) );
return 0;
}