diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /sort_array.c | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'sort_array.c')
-rw-r--r-- | sort_array.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/sort_array.c b/sort_array.c new file mode 100644 index 0000000..5bc491a --- /dev/null +++ b/sort_array.c @@ -0,0 +1,35 @@ +// 912. Sort an Array +#include <stdio.h> +#include <stdlib.h> + +void quickSort(int *nums, int start, int end) { + if (start >= end) + return; + int tmp = nums[start]; + int i = start, k = end; + while (i < k) { + for (k; nums[k] >= tmp && k > i; k--) + ; + nums[i] = nums[k]; + nums[k] = tmp; + for (i; nums[i] <= tmp && i < k; i++) + ; + nums[k] = nums[i]; + nums[i] = tmp; + } + quickSort(nums, start, i - 1); + quickSort(nums, k + 1, end); +} + +int *sortArray(int *nums, int numsSize, int *returnSize) { + quickSort(nums, 0, numsSize - 1); + *returnSize = numsSize; + return nums; +} + +int main() { + int n1[] = {5, 2, 3, 1}, ns1 = 4, rs1[] = {4}; + int n2[] = {5, 1, 1, 2, 0, 0}, ns2 = 6, rs2[] = {6}; + printf("%p\n", sortArray(n1, ns1, rs1)); + printf("%p\n", sortArray(n2, ns2, rs2)); +} |