Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nathggns
Last active December 16, 2016 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathggns/60f8e289ed42c7be46531eaad0b51398 to your computer and use it in GitHub Desktop.
Save nathggns/60f8e289ed42c7be46531eaad0b51398 to your computer and use it in GitHub Desktop.
Two implementations of quick sort in C

Included in this gist are two C implementations of Quicksort. One designed to work with pointers, and one with arrays.

These were created for educational purposes and should not be used in production. Any suggestions on improvements would be vastly appreciated.

typedef int (*comparator)(const void*, const void*);
void mySwap(void **base, int indexA, int indexB) {
void *temp;
temp = base[indexA];
base[indexA] = base[indexB];
base[indexB] = temp;
}
void mySortWorker(void **base, int left, int right, comparator compare) {
if (left < right) {
int partitionIndex = left;
for (int i = left; i < right; i++) {
if (compare(base[i], base[right]) < 0) {
mySwap(base, i, partitionIndex);
partitionIndex++;
}
}
mySwap(base, right, partitionIndex);
mySortWorker(base, left, partitionIndex - 1, compare);
mySortWorker(base, partitionIndex + 1, right, compare);
}
}
void mySort(void **base, int num, comparator compare) {
mySortWorker(base, 0, num - 1, compare);
}
#include <stdio.h>
#include <stdlib.h>
int myCompar(const void *a, const void *b) {
const int *ia = (const int *) a;
const int *ib = (const int *) b;
return *ia - *ib;
}
int** convertIntArrayToIntPointers(int mainArr[], int len) {
int **arr = (int **) malloc(len * sizeof(int*));
for (int i = 0; i < 6; i++) {
int *j = malloc(sizeof(int));
*j = mainArr[i];
arr[i] = j;
}
return arr;
}
int main() {
#define LEN 6
int mainArr[LEN] = { 5, 2, 1, 6, 3, 4 };
int **arr = convertIntArrayToIntPointers(mainArr, LEN);
printf("Before sort: %d %d %d %d %d %d\n", *arr[0], *arr[1], *arr[2], *arr[3], *arr[4], *arr[5]);
mySort((void**) arr, LEN, *myCompar);
printf("After sort: %d %d %d %d %d %d\n", *arr[0], *arr[1], *arr[2], *arr[3], *arr[4], *arr[5]);
free(arr);
}
#include <stdlib.h>
#include <string.h>
typedef int (*comparator)(const void*, const void*);
void mySwap(void *a, void *b, size_t size) {
void *temp = malloc(size);
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
free(temp);
}
void mySortWorker(void *base, size_t size, int left, int right, comparator compare) {
if (left < right) {
int partitionIndex = left;
for (int i = left; i < right; i++) {
if (compare(&base[i * size], &base[right * size]) < 0) {
mySwap(&base[i * size], &base[partitionIndex * size], size);
partitionIndex++;
}
}
mySwap(&base[right * size], &base[partitionIndex * size], size);
mySortWorker(base, size, left, partitionIndex - 1, compare);
mySortWorker(base, size, partitionIndex + 1, right, compare);
}
}
void mySort(void *base, int num, size_t size, comparator compare) {
mySortWorker(base, size, 0, num - 1, compare);
}
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
typedef struct child
{
int age;
} Child;
void addChildrenToList(Child* list, int count) {
for (int i = 0; i < count; i++) {
Child *child = &list[i];
child->age = rand() % 19;
}
}
int myCompare(const void* A, const void* B) {
Child *childA = (Child*) A;
Child *childB = (Child*) B;
return childA->age - childB->age;
}
void printListOfChild(Child* list, int amount) {
for (int i = 0; i < amount; i++) {
printf("Age: %d\n", list[i].age);
}
}
int main() {
#define COUNT 6
srand(time(NULL));
Child list[COUNT];
addChildrenToList(list, COUNT);
printf("Unsorted\n");
printListOfChild(list, COUNT);
mySort(list, COUNT, sizeof(Child), *myCompare);
printf("\nSorted:\n");
printListOfChild(list, COUNT);
free(list);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment