zoukankan      html  css  js  c++  java
  • 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("
    ");
    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 }
  • 相关阅读:
    Keep at Most 100 Characters
    Larry and Inversions
    计算指数
    简单题
    重要的话说三遍
    I Love GPLT
    猜数字
    打印沙漏
    多态性(polymorphism),封装性(encapsulation),内聚(cohesion)以及耦合(coupling)的基本概念
    Hibernate面试题
  • 原文地址:https://www.cnblogs.com/micoblog/p/13729925.html
Copyright © 2011-2022 走看看