-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.c
More file actions
49 lines (40 loc) · 952 Bytes
/
sort.c
File metadata and controls
49 lines (40 loc) · 952 Bytes
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
#include<stdio.h>
static void do_swap(int *a,int *b) {
int temp = *a + *b;
*b = temp - *b;
*a = temp - *b;
}
static void selection_sort(int u_arr[],int len) {
int i = 1;
int j;
for(i = 0; i< len ; i++) {
int min_value = u_arr[i]; //select the minimum value
for(j = i+1;j< len;j++) {
if(u_arr[j] < min_value)
{ //compare all the min values
min_value = u_arr[j];
do_swap(&u_arr[i],&u_arr[j]); //put or swap the min value on first index and ++ index
}
}
}
}
//Algorithm
//InsertionSort(A)
//A[0] = −∞
//for i = 2 to n do
//j=i
//while (A[j] < A[j − 1]) do
//swap(A[j], A[j − 1])
//j =j−1
static void insertion_sort(int u_arr[],int len)
{
int i ,j;
int temp;
for(i = 1;i<len;i++) { //right now I have one card in my hand
j = i;
while(j>=0 && u_arr[j] < u_arr[j-1]) { //I am going to compare untill my last card in hand
do_swap(&u_arr[j],&u_arr[j-1]);
j = j-1;
}
}
}