Skip to content

Create 0912-sort-an-array.c #3050

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
66 changes: 66 additions & 0 deletions c/0912-sort-an-array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// This is optimized for arrays with repeated values. Simple quicksort in arrays
// with lots of repeated values will fall to O(n^2). This will hold it under
// O(nlogn) but might end up running slower and consuming more space in average.
// The array is sorted inplace.

/**
* Note: The returned array must be malloced, assume caller calls free().
*/

void swap(int *first_value, int *second_value);
int *partition3(int *array, int left, int right);
void _quicksort(int *array, int array_length, int left, int right);

enum Position { LEFT_MOST, RIGHT_MOST };

int *sortArray(int *nums, int numsSize, int *returnSize) {
_quicksort(nums, numsSize, 0, numsSize - 1);
*returnSize = numsSize;
return nums;
}

void _quicksort(int *array, int array_length, int left, int right) {
while (left < right) {
int *middle = partition3(array, left, right);
if (middle[LEFT_MOST] - left < right - middle[RIGHT_MOST]) {
_quicksort(array, array_length, left, middle[LEFT_MOST] - 1);
left = middle[RIGHT_MOST] + 1;
} else {
_quicksort(array, array_length, middle[RIGHT_MOST] + 1, right);
right = middle[LEFT_MOST] - 1;
}
free(middle);
}
}

int *partition3(int *array, int left, int right) {
int pivot_position = left + (right - left) / 2;
int pivot = array[pivot_position];
swap(&array[left], &array[pivot_position]);

int *middle = (int *)malloc(2 * sizeof(int));

middle[LEFT_MOST] = left;
middle[RIGHT_MOST] = left;

for (int i = left + 1; i <= right; ++i) {
if (array[i] == pivot) {
middle[RIGHT_MOST]++;
swap(&array[middle[RIGHT_MOST]], &array[i]);
}
if (array[i] < pivot) {
middle[LEFT_MOST]++;
middle[RIGHT_MOST]++;
swap(&array[middle[RIGHT_MOST]], &array[i]);
swap(&array[middle[LEFT_MOST]], &array[middle[RIGHT_MOST]]);
}
}
swap(&array[left], &array[middle[LEFT_MOST]]);
return middle;
}

void swap(int *first_value, int *second_value) {
int tmp_value = *first_value;
*first_value = *second_value;
*second_value = tmp_value;
}