912. 排序数组

 

 1 void quickSort(int *nums, int n, int i, int j) {
 2     int base, tmp, this_begin,  this_end;
 3     if (i<0 || j >= n) 
 4         return;
 5     if (i >= j) 
 6         return ;
 7     this_begin = i;
 8 
 9     this_end = j;
10     base = nums[i];
11     while (i < j) {
12         while (i<j && nums[j] >= base)--j;
13         nums[i] = nums[j];
14         while (i<j && nums[i] < base) ++i;
15         nums[j] = nums[i];
16     }
17     nums[i] = base;
18 #if 0
19     int k;
20     for(k=0; k<n; ++k)
21         printf("%d ", nums[k]);
22     printf("\n");
23 #endif
24     if (i == j) ++j;
25     quickSort(nums, n, this_begin, i);
26     quickSort(nums, n, j, this_end);
27 }
28 /**
29  * Note: The returned array must be malloced, assume caller calls free().
30  */
31 int* sortArray(int* nums, int numsSize, int* returnSize){
32     *returnSize = numsSize;
33     int *returnNums;
34     
35     if (nums == NULL) 
36         return NULL;
37     returnNums = malloc(sizeof(int) * numsSize);
38     memcpy(returnNums, nums, sizeof(int) * numsSize);
39     
40     if (numsSize <= 1) 
41         return nums;
42     
43     quickSort(returnNums, numsSize, 0, numsSize - 1);
44     return returnNums;
45 }

 

posted @ 2020-09-25 14:20  yushimeng  阅读(137)  评论(0编辑  收藏  举报