C Solution.Do it in a hard way.


  • 0
    D
        quick_sort(nums, 0, numsSize - 1);
        int sum = 0;
        int k;
        for (k = 0; k < numsSize; k += 2) {
            sum += nums[k];
        }
        return sum;
    }
    int partition(int* arr, int low, int high){
        int key;
        key = arr[low];
        while(low<high){
            while(low <high && arr[high]>= key )
                high--;
            if(low<high)
                arr[low++] = arr[high];
            while( low<high && arr[low]<=key )
                low++;
            if(low<high)
                arr[high--] = arr[low];
        }
        arr[low] = key;
        return low;
    }
    void quick_sort(int* arr, int start, int end){
        int pos;
        if (start<end){
            pos = partition(arr, start, end);
            quick_sort(arr,start,pos-1);
            quick_sort(arr,pos+1,end);
        }
        return;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.