Skip to content

Commit 8f13273

Browse files
committed
optimized c
1 parent 62ace97 commit 8f13273

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

c/sortingAlgorithms/quicksort.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ void swap(int *x, int *y) {
44
*y = temp;
55
}
66

7-
int partition(int array[], int low, int high) {
8-
int pivot = array[high];
9-
int i = (low - 1);
7+
int partition(int arr[], int low, int high) {
8+
int pivot = arr[high];
9+
int l = (low - 1);
1010

1111
for (int pos = low; pos < high; pos++) {
12-
if (array[pos] <= pivot) {
13-
i++;
14-
swap(&array[i], &array[pos]);
12+
if (arr[pos] <= pivot) {
13+
l++;
14+
swap(&arr[l], &arr[pos]);
1515
}
1616
}
1717

18-
swap(&array[i + 1], &array[high]);
18+
swap(&arr[l+1], &arr[high]);
1919

20-
return (i + 1);
20+
return (l + 1);
2121
}
2222

2323
void quicksort(int arr[], int low, int high) {

c/sortingAlgorithms/testing.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <stdio.h>
2+
#include <stdbool.h>
23

34
#include "quicksort.c"
45

@@ -7,16 +8,27 @@ void printArray(int arr[], int size) {
78
for (int i = 0; i < size; i++) {
89
printf("%d ", arr[i]);
910
}
10-
printf("end");
11+
printf("end\n");
12+
}
13+
14+
bool equalArrays(int arr[], int arr2[], int size) {
15+
for (int i = 0; i < size; i++) {
16+
if (arr[i] != arr2[i]) return false;
17+
}
18+
19+
return true;
1120
}
1221

1322
int main()
1423
{
1524
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};
25+
int sol[] = {-124714, -83148, -7285, -1247, -1235, -124, -100, -10, -2, 0, 1, 2, 2, 3, 7, 7, 9, 9, 10, 13, 16, 21, 23, 32, 63, 68, 87, 121, 125, 126, 135, 166, 216, 247, 261, 274, 389, 521, 532, 742, 852, 913, 942, 1235, 2162, 6216, 6543, 7317, 7327, 7654, 8421, 8539, 8765, 19361, 21261, 21561, 24171, 32732, 37272, 47421, 48245, 62161, 100000, 123456, 2157105, 21742174, 24151621, 987654321};
1626
int length = sizeof(arr) / sizeof(int);
1727

18-
quicksort(arr, 0, length);
28+
quicksort(arr, 0, length-1);
1929
printArray(arr, length);
2030

31+
printf("%s", equalArrays(arr, sol, length) ? "true" : "false");
32+
2133
return 0;
2234
}

python/sorting_algorithms/testing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
difficult_sort_test2 = [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]
1212

1313
difficult_non_negative_sort_test = [5312, 621, 7, 47, 1, 34, 7, 3146134, 416, 21, 641, 3512, 6325, 64174, 326, 3416, 417, 3164, 3421653, 731, 1431, 43181346, 631280]
14-
14+
print(sorted(difficult_sort_test2))
1515
print(bubble_sort(difficult_sort_test) == merge_sort(difficult_sort_test) == quicksort(difficult_sort_test2) == sorted(difficult_sort_test2))
1616

1717
print(bubble_sort(difficult_sort_test) == sorted(difficult_sort_test2))

0 commit comments

Comments
 (0)