We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
int* quickSort(int arr_count, int* arr, int* result_count) {
result_count = arr_count;
int j = 0, pivot = arr[0];
int ar = (int*)malloc(arr_count * sizeof(int));
// Elements less than pivot
for (int i = 0; i < arr_count; i++) {
if (arr[i] < pivot) {
ar[j++] = arr[i];
}
}
// Pivot element
ar[j++] = pivot;
// Elements greater than pivot
for (int i = 0; i < arr_count; i++) {
if (arr[i] > pivot) {
ar[j++] = arr[i];
}
}
return ar;
}
int main() {
int arr_count;
scanf("%d", &arr_count);
int arr[arr_count];
for (int i = 0; i < arr_count; i++) {
scanf("%d", &arr[i]);
}
int result_count;
int* sorted = quickSort(arr_count, arr, &result_count);
for (int i = 0; i < result_count; i++) {
printf("%d ", sorted[i]);
}
printf("\n");
free(sorted);
return 0;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Quicksort 1 - Partition
You are viewing a single comment's thread. Return to all comments →
include
include
int* quickSort(int arr_count, int* arr, int* result_count) { result_count = arr_count; int j = 0, pivot = arr[0]; int ar = (int*)malloc(arr_count * sizeof(int));
}
int main() { int arr_count; scanf("%d", &arr_count);
}