Skip to content

Commit

Permalink
ADD sort-algos,sort-algos-int,sort-algos-int-with-func
Browse files Browse the repository at this point in the history
  • Loading branch information
ismdeep committed Jan 30, 2020
1 parent 39aa50a commit b25682e
Show file tree
Hide file tree
Showing 32 changed files with 618 additions and 56 deletions.
36 changes: 32 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
cmake_minimum_required(VERSION 3.15)
project(sort-algos-c C)
project(sort-algos C)
project(sort-algos-int C)
project(sort-algos-int-with-func C)

set(CMAKE_C_STANDARD 11)

include_directories("./include")

add_executable(sort-algos-c
add_executable(sort-algos

include/data-swap.c
include/sort-algos/selection-sort.c
Expand All @@ -14,6 +16,32 @@ add_executable(sort-algos-c
include/sort-algos/quick-sort.c
include/sort-algos/cocktail-sort.c
include/sorted-assert.c
include/sort-test.c
include/sort-algos/sort-test.c
include/time-utils.c
main.c)
sort-algos.c)

add_executable(sort-algos-int

include/sort-algos-int/selection-sort-int.c
include/sort-algos-int/bubble-sort-int.c
include/sort-algos-int/insertion-sort-int.c
include/sort-algos-int/quick-sort-int.c
include/sort-algos-int/cocktail-sort-int.c
include/sorted-assert.c
include/data-swap.c
include/sort-algos-int/sort-test.c
include/time-utils.c
sort-algos-int.c)

add_executable(sort-algos-int-with-func

include/sort-algos-int-with-func/selection-sort-int-with-func.c
include/sort-algos-int-with-func/bubble-sort-int-with-func.c
include/sort-algos-int-with-func/insertion-sort-int-with-func.c
include/sort-algos-int-with-func/quick-sort-int-with-func.c
include/sort-algos-int-with-func/cocktail-sort-int-with-func.c
include/sorted-assert.c
include/data-swap.c
include/sort-algos-int-with-func/sort-test.c
include/time-utils.c
sort-algos-int-with-func.c)
17 changes: 17 additions & 0 deletions include/sort-algos-int-with-func/bubble-sort-int-with-func.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Created by ismdeep on 2020/1/28.
//

#include <sort-algos-int-with-func/bubble-sort-int-with-func.h>

#include <data-swap.h>

void bubble_sort(int a[], int len, bool (*cmp_func)(const int *, const int *)) {
for (int stop = len - 1; stop > 0; stop--) {
for (int i = 0; i < stop; i++) {
if ( !cmp_func(&a[i], &a[i+1])) {
data_swap(a + i, a + i + 1, sizeof(int));
}
}
}
}
12 changes: 12 additions & 0 deletions include/sort-algos-int-with-func/bubble-sort-int-with-func.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Created by ismdeep on 2020/1/28.
//

#ifndef SORT_ALGOS_INT_WITH_FUNC_BUBBLE_SORT_INT_WITH_FUNC_H
#define SORT_ALGOS_INT_WITH_FUNC_BUBBLE_SORT_INT_WITH_FUNC_H

#include <stdbool.h>

void bubble_sort(int a[], int len, bool (*cmp_func)(const int *, const int *));

#endif //SORT_ALGOS_INT_WITH_FUNC_BUBBLE_SORT_INT_WITH_FUNC_H
26 changes: 26 additions & 0 deletions include/sort-algos-int-with-func/cocktail-sort-int-with-func.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Created by ismdeep on 2020/1/28.
//

#include <sort-algos-int-with-func/cocktail-sort-int-with-func.h>

void cocktail_sort(int a[], int len, bool (*cmp_func)(const int *, const int *)) {
int i, left = 0, right = len - 1;
int temp;
while (left < right) {
for (i = left; i < right; i++)
if (!cmp_func(&a[i], &a[i + 1])) {
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
right--;
for (i = right; i > left; i--)
if (!cmp_func(&a[i - 1], &a[i])) {
temp = a[i];
a[i] = a[i - 1];
a[i - 1] = temp;
}
left++;
}
}
12 changes: 12 additions & 0 deletions include/sort-algos-int-with-func/cocktail-sort-int-with-func.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Created by ismdeep on 2020/1/28.
//

#ifndef SORT_ALGOS_INT_WITH_FUNC_COCKTAIL_SORT_INT_WITH_FUNC_H
#define SORT_ALGOS_INT_WITH_FUNC_COCKTAIL_SORT_INT_WITH_FUNC_H

#include <stdbool.h>

void cocktail_sort(int a[], int len, bool (*cmp_func)(const int *, const int *));

#endif //SORT_ALGOS_INT_WITH_FUNC_COCKTAIL_SORT_INT_WITH_FUNC_H
23 changes: 23 additions & 0 deletions include/sort-algos-int-with-func/insertion-sort-int-with-func.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Created by ismdeep on 2020/1/28.
//

#include <sort-algos-int-with-func/insertion-sort-int-with-func.h>

#include <stdbool.h>

void insertion_sort(int a[], int len, bool (*cmp_func)(const int *, const int *)) {
for (int left = 0; left < len; left++) {
int min_p = left;
for (int cur = min_p + 1; cur < len; cur++) {
if ( cmp_func(&a[cur], &a[min_p]) ) {
min_p = cur;
}
}
int tmp = a[min_p];
for (int cursor = min_p - 1; cursor >= left; cursor--) {
a[cursor + 1] = a[cursor];
}
a[left] = tmp;
}
}
12 changes: 12 additions & 0 deletions include/sort-algos-int-with-func/insertion-sort-int-with-func.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Created by ismdeep on 2020/1/28.
//

#ifndef SORT_ALGOS_INT_WITH_FUNC_INSERTION_SORT_INT_WITH_FUNC_H
#define SORT_ALGOS_INT_WITH_FUNC_INSERTION_SORT_INT_WITH_FUNC_H

#include <stdbool.h>

void insertion_sort(int a[], int len, bool (*cmp_func)(const int *, const int *));

#endif //SORT_ALGOS_INT_WITH_FUNC_INSERTION_SORT_INT_WITH_FUNC_H
32 changes: 32 additions & 0 deletions include/sort-algos-int-with-func/quick-sort-int-with-func.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Created by ismdeep on 2020/1/28.
//

#include <sort-algos-int-with-func/quick-sort-int-with-func.h>

#include <stdbool.h>
#include <data-swap.h>

void quick_sort(int a[], int len, bool (*cmp_func)(const int *, const int *)) {
if (len <= 1)
return;
int mid = a[len - 1];
int left = 0;
int right = len - 2;
while (left < right) {
while (cmp_func(&a[left], &mid) && left < right)
++left;
while (!cmp_func(&a[right], &mid) && left < right)
--right;
data_swap(a + left, a + right, sizeof(int));
}
if ( cmp_func(&a[len-1], &a[left]) ) {
data_swap(a + left, a + len - 1, sizeof(int));
} else {
++left;
data_swap(a + left, a + len - 1, sizeof(int));
}
if (left)
quick_sort(a, left, cmp_func);
quick_sort(a + left + 1, len - left - 1, cmp_func);
}
12 changes: 12 additions & 0 deletions include/sort-algos-int-with-func/quick-sort-int-with-func.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Created by ismdeep on 2020/1/28.
//

#ifndef SORT_ALGOS_INT_WITH_FUNC_QUICK_SORT_INT_WITH_FUNC_H
#define SORT_ALGOS_INT_WITH_FUNC_QUICK_SORT_INT_WITH_FUNC_H

#include <stdbool.h>

void quick_sort(int a[], int len, bool (*cmp_func)(const int *, const int *));

#endif //SORT_ALGOS_INT_WITH_FUNC_QUICK_SORT_INT_WITH_FUNC_H
21 changes: 21 additions & 0 deletions include/sort-algos-int-with-func/selection-sort-int-with-func.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Created by ismdeep on 2020/1/28.
//

#include <sort-algos-int-with-func/selection-sort-int-with-func.h>

#include <stdbool.h>

void selection_sort(int a[], int len, bool (*cmp_func)(const int *, const int *)) {
for (int left = 0; left < len; left++) {
int min_p = left;
for (int cur = left + 1; cur < len; cur++) {
if ( cmp_func(&a[cur], &a[min_p]) ) {
min_p = cur;
}
}
int tmp = a[min_p];
a[min_p] = a[left];
a[left] = tmp;
}
}
12 changes: 12 additions & 0 deletions include/sort-algos-int-with-func/selection-sort-int-with-func.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Created by ismdeep on 2020/1/28.
//

#ifndef SORT_ALGOS_INT_WITH_FUNC_SELECTION_SORT_INT_WITH_FUNC_H
#define SORT_ALGOS_INT_WITH_FUNC_SELECTION_SORT_INT_WITH_FUNC_H

#include <stdbool.h>

void selection_sort(int a[], int len, bool (*cmp_func)(const int *, const int *));

#endif //SORT_ALGOS_INT_WITH_FUNC_SELECTION_SORT_INT_WITH_FUNC_H
38 changes: 38 additions & 0 deletions include/sort-algos-int-with-func/sort-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Created by ismdeep on 2020/1/30.
//

#include <sort-algos-int-with-func/sort-test.h>

#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <sorted-assert.h>
#include <time-utils.h>
#include <stdio.h>

void sort_test(
int data[],
int len,
void (*sort_func)(int [], int, bool(*)(const int*, const int*)),
char *sort_func_name,
bool (*cmp_func)(const int*, const int*)
) {
/* Copy data from raw to selection_sort_data */
int *sort_data = (int *) malloc(sizeof(int) * len );
memcpy(sort_data, data, sizeof(int) * len);

/* Sort array **sort_data** with **sort_func()** algorithm */
struct timeval *start_point = create_start_point();
sort_func(sort_data, len, cmp_func);
printf("Time elapse[%s]: %.2lf ms\n", sort_func_name, stop_watch_us(start_point) / 1000.0);

/* Assert sort result */
if (sorted_assert(data, data + len, sort_data, sort_data + len, sizeof(int),
(bool (*)(const void *, const void *)) cmp_func)) {
printf("Successfully.\n\n");
} else {
printf("Unsuccessfully.\n\n");
}
}
18 changes: 18 additions & 0 deletions include/sort-algos-int-with-func/sort-test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Created by ismdeep on 2020/1/30.
//

#ifndef SORT_ALGOS_INT_WITH_FUNC_SORT_TEST_H
#define SORT_ALGOS_INT_WITH_FUNC_SORT_TEST_H

#include <stdbool.h>

void sort_test(
int data[],
int len,
void (*sort_func)(int [], int, bool(*)(const int*, const int*)),
char *sort_func_name,
bool (*cmp_func)(const int*, const int*)
);

#endif //SORT_ALGOS_INT_WITH_FUNC_SORT_TEST_H
17 changes: 17 additions & 0 deletions include/sort-algos-int/bubble-sort-int.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Created by ismdeep on 2020/1/28.
//

#include <sort-algos-int/bubble-sort-int.h>
#include <data-swap.h>


void bubble_sort(int a[], int len) {
for (int stop = len - 1; stop > 0; stop--) {
for (int i = 0; i < stop; i++) {
if (a[i] > a[i + 1]) {
data_swap(a + i, a + i + 1, sizeof(int));
}
}
}
}
10 changes: 10 additions & 0 deletions include/sort-algos-int/bubble-sort-int.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Created by ismdeep on 2020/1/28.
//

#ifndef SORT_ALGOS_C_BUBBLE_SORT_INT_H
#define SORT_ALGOS_C_BUBBLE_SORT_INT_H

void bubble_sort(int a[], int len);

#endif //SORT_ALGOS_C_BUBBLE_SORT_INT_H
26 changes: 26 additions & 0 deletions include/sort-algos-int/cocktail-sort-int.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Created by ismdeep on 2020/1/28.
//

#include <sort-algos-int/cocktail-sort-int.h>

void cocktail_sort(int a[], int len) {
int i, left = 0, right = len - 1;
int temp;
while (left < right) {
for (i = left; i < right; i++)
if (a[i] > a[i + 1]) {
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
right--;
for (i = right; i > left; i--)
if (a[i - 1] > a[i]) {
temp = a[i];
a[i] = a[i - 1];
a[i - 1] = temp;
}
left++;
}
}
10 changes: 10 additions & 0 deletions include/sort-algos-int/cocktail-sort-int.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Created by ismdeep on 2020/1/28.
//

#ifndef SORT_ALGOS_INT_WITH_FUNC_COCKTAIL_SORT_INT_H
#define SORT_ALGOS_INT_WITH_FUNC_COCKTAIL_SORT_INT_H

void cocktail_sort(int a[], int len);

#endif //SORT_ALGOS_INT_WITH_FUNC_COCKTAIL_SORT_INT_H
21 changes: 21 additions & 0 deletions include/sort-algos-int/insertion-sort-int.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Created by ismdeep on 2020/1/28.
//

#include <sort-algos-int/insertion-sort-int.h>

void insertion_sort(int a[], int len) {
for (int left = 0; left < len; left++) {
int min_p = left;
for (int cur = min_p + 1; cur < len; cur++) {
if ( a[cur] < a[min_p] ) {
min_p = cur;
}
}
int tmp = a[min_p];
for (int cursor = min_p - 1; cursor >= left; cursor--) {
a[cursor + 1] = a[cursor];
}
a[left] = tmp;
}
}
10 changes: 10 additions & 0 deletions include/sort-algos-int/insertion-sort-int.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Created by ismdeep on 2020/1/28.
//

#ifndef SORT_ALGOS_INT_WITH_FUNC_INSERTION_SORT_INT_H
#define SORT_ALGOS_INT_WITH_FUNC_INSERTION_SORT_INT_H

void insertion_sort(int a[], int len);

#endif //SORT_ALGOS_INT_WITH_FUNC_INSERTION_SORT_INT_H
Loading

0 comments on commit b25682e

Please sign in to comment.