-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibsort.c
72 lines (54 loc) · 1.13 KB
/
libsort.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
57
58
59
60
61
62
#include <stdio.h>
#include "libsort.h"
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void printArray(int array[], int size){
for(int i=0; i<size; i++)
printf("%d ", array[i]);
printf("\n\n");
}
void bubbleSort(int array[], int size)
{
for( int i=0 ; i < size-1; i++){
for(int j=0; j<size-1-i; j++){
if(array[j]>array[j+1]){
swap(&array[j], &array[j+1]);
}
}
}
}
void selectionSort(int array[], int size)
{
for(int i=0; i<size;i++)
{
int minIndex=i;
for(int j=i+1;j<size;j++)
{
if(array[minIndex] > array[j])
{
minIndex = j;
}
}
if(i != minIndex)
swap(&array[i], &array[minIndex]);
}
}
// https://www.geeksforgeeks.org/insertion-sort/
void insertionSort(int array[], int size)
{
for(int i = 1 ; i < size ; i++)
{
int key = array[i];
int j = i-1;
while(j >= 0 && array[j] > key)
{
array[j+1] = array[j];
j--;
}
array[j+1]=key;
}
}