Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added .gitignore and implemented quicksort in C #122

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.out
.vscode
54 changes: 54 additions & 0 deletions 9_quick_sort/quick_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include<stdio.h>

void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}

int divide (int data[], int left, int right) {
int pivot = data[right];
int i = (left - 1);

for (int j = left; j <= right- 1; j++) {
if (data[j] < pivot) {
i++;
swap(&data[i], &data[j]);
}
}
swap(&data[i + 1], &data[right]);
return i + 1;
}

void quicksort(int data[], int left, int right) {
if (left < right) {
int index = divide(data, left, right);
quicksort(data, left, index - 1);
quicksort(data, index + 1, right);
}
}


int main(int argc, char* argv){
int test[10] = {
10,
9,
8,
7,
3213123,
5,
4,
3,
2,
1
};
int start = 0;
int* end = &test[(sizeof(test) / sizeof(test[0])) - 1];
quicksort(test, 0, (sizeof(test) / sizeof(test[0])) - 1);

for(int i = 0; i < (sizeof(test) / sizeof(test[0])); ++i){
printf("%i -> %i\n", i, test[i]);
}

return 0;
}