Skip to content

Commit 77a6e24

Browse files
committed
added c quicksort
1 parent e116358 commit 77a6e24

File tree

10 files changed

+63
-2
lines changed

10 files changed

+63
-2
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
*/__pycache__
1+
__pycache__/
2+
3+
*.exe

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# Algorithms and Datastructures
2-
Many different Algorithms and data structures written in Python and other languages.
2+
Many different Algorithms and data structures written in many different languages.

c/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Algorithms and Datastructures in C
2+
Many different Algorithms and data structures written in C.

c/sortingAlgorithms/quicksort.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
void swap(int *x, int *y) {
2+
int temp = *x;
3+
*x = *y;
4+
*y = temp;
5+
}
6+
7+
int partition(int array[], int low, int high) {
8+
int pivot = array[high];
9+
int i = (low - 1);
10+
11+
for (int pos = low; pos < high; pos++) {
12+
if (array[pos] <= pivot) {
13+
i++;
14+
swap(&array[i], &array[pos]);
15+
}
16+
}
17+
18+
swap(&array[i + 1], &array[high]);
19+
20+
return (i + 1);
21+
}
22+
23+
void quicksort(int arr[], int low, int high) {
24+
if (low < high) {
25+
int pivot = partition(arr, low, high);
26+
27+
quicksort(arr, low, pivot-1);
28+
quicksort(arr, pivot+1, high);
29+
}
30+
}
31+
32+
33+

c/sortingAlgorithms/testing.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
3+
#include "quicksort.c"
4+
5+
6+
void printArray(int arr[], int size) {
7+
for (int i = 0; i < size; i++) {
8+
printf("%d ", arr[i]);
9+
}
10+
printf("end");
11+
}
12+
13+
int main()
14+
{
15+
int arr[] = {21561, 9, -124714, 0, -1247, 24171, 21742174, 47421, -7285, 261, 2, 521, 10, 3, 216, 7, 6216, 2, 62161, 7, 68, 9, -83148, -1235, 1, -10, 166, 389, 13, 63, 19361, 32, 7317, 8539, 32732, 24151621, -2, 7327, 2157105, 1235, 852, 121, 21, -100, -124, 23, 742, 7654, 87, 123456, 6543, 21261, 48245, 16, 8765, 987654321, 247, 125, 8421, 942, 532, 913, 126, 274, 135, 100000, 2162, 37272};
16+
int length = sizeof(arr) / sizeof(int);
17+
18+
quicksort(arr, 0, length);
19+
printArray(arr, length);
20+
21+
return 0;
22+
}

python/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Algorithms and Datastructures in Python
2+
Many different Algorithms and data structures written in Python.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)