Exemple #1
0
void QuickSort(double a[2][N],int p,int r){
    if (p<r){
        int q=partion(a,p,r);
        QuickSort(a,p,q-1);
        QuickSort(a,q+1,r);
    }
}
Exemple #2
0
static void quicksort(int * array, int lower, int upper){
	if(lower<upper){
		int pivot = partion(array,lower,upper);
		quicksort(array,lower,pivot-1);
		quicksort(array,pivot+1,upper);
	}

}
void quickSort(std::vector<int> &vec, int lo, int hi)
{
    if (2 > hi - lo)
        return;
    int pivot = partion(vec, lo, hi - 1);
    quickSort(vec, lo, pivot);
    quickSort(vec, pivot + 1, hi);
}
void quicksort(int *a, int left, int right) {

    if (left<right)//加上这个,不然有死循环,造成堆栈溢出
    {
        int i = partion(a,left,right);
        quicksort(a,left,i-1);
        quicksort(a,i+1,right);
    }
}
Exemple #5
0
void quickSort(int* array, int n)
{
    int part = partion(array, n);
    if (part > 0) {
        quickSort(array, part);
    }
    if (part + 1 < n) {
        quickSort(array + part + 1, n - (part + 1));
    }
}
Exemple #6
0
//  quickSort
void quickSort(int arr[], int low, int high)
{
    if (low < high)
    {
        int k = partion(arr, low, high);

        quickSort(arr, low, k - 1);
        quickSort(arr, low + 1, high);
    }
}
Exemple #7
0
void Quick_Sort::process_sort(int* array,int beg,int end)
{
	if(beg < end && array != NULL)
	{
		int k = partion(array,beg,end);
		process_sort(array,beg,k-1);
		process_sort(array,k+1,end);
	}

}
Exemple #8
0
void quick_sort(int array[], int start, int end)
{
    int pivot;
    if (start < end)
    {
        pivot = partion(array, start, end);
        quick_sort(array, start, pivot-1);
        quick_sort(array, pivot+1, end);
    }
}
/**
 * find the k'th smallest elemnt from array who's contains 'length' elements.
 * @param array
 * @param length
 * @param k
 */
int liner_time_select(int array[], int low, int high, int k) {

    if (low == high)return array[low];
    int i = partion(array, low, high);
    int j = i - low + 1;
    if (k <= j) { // the k'th smallest element is here.
        return liner_time_select(array, low, i, k);
    } else {
        return liner_time_select(array, i + 1, high, k - j);
    }
}
Exemple #10
0
void quick_sort(int arr[], int p, int q)
    {
        int j;
        if (p < q)

        {
            j = partion(arr, p, q);
            quick_sort(arr, p, j-1);
            quick_sort(arr, j+1, q);
        }
    }
Exemple #11
0
void quick_sort(int *a, int len)
{
	int pivot;

	if(len>0)
	{
		pivot = partion(a, len);
		quick_sort(a, pivot);
		quick_sort(&a[pivot+1], len-(pivot+1));
	}
}
 int partion(vector<int> &nums, int b, int e, int k)   // [b, e]
 {
     if(b >= e || k <= 0)return nums[b];
     int i = b;
     for(int j = b + 1;j <= e;++j)
     {
         if (nums[j] >= nums[b])
         {
             i++;
             swap(nums[i], nums[j]);
         }
     }
     swap(nums[b], nums[i]);
     if(i - b + 1 == k)
         return nums[i];
     else if(i - b + 1 > k)
         return partion(nums, b, i  , k);
     else
         return partion(nums, i + 1, e, k - (i - b + 1));
 }
Exemple #13
0
int quicksort(int *p,int low,int high)
{
	if(low < high)
	{
		int mid=partion(p,low,high);
		quicksort(p,mid+1,high);
		quicksort(p,low,mid-1);
		return 1;
	}
	else
		return 0;
}
int qsort(int *data,int begin,int end)
{
	if(begin >= end)
		return 0;
	int i = partion(data,begin,end);
	printf("partion : %d \n",i);
	show(data,10);
	getchar();
	qsort(data,begin,i-1);
	qsort(data,i+1,end);
	return 0;

}
int find_min_k_nrs_by_partition(int *array, int size, int k, int *result)
{
	assert(array && result);

	if (k <= 0) {
		return 0;
	}

	if (k >= size) {
		memcpy(result, array, size*sizeof(*array));
		return size;
	}

	int *a = array;
	int s = size;
	int c = k;
	int ret;

	while (1) {
		if (s > 2) {
			ret = partion(a, s, c);
			if (ret == c) {
				// from a[0] to a[ret] are the smallest k numbers
				break;
			} else if (ret > c) {
				// continue partion
				s = ret;
			} else {
				// a[0] .. a[ret] are the results. we need to find others
				a = a + ret;
				s = s-ret;
				c = c - ret;
			}
		} else if (s == 2) {
			if (a[0] >= a[1]) {
				std::swap(a[0], a[1]);
			}
			break;
		} else {
			break;
		}
	}
 
	//Now array[0]~array[k] are smallest 
	memcpy(result, array, k*sizeof(*array));
	return k;
}
 /*
  * param k : description of k
  * param nums : description of array and index 0 ~ n-1
  * return: description of return
  */
 int kthLargestElement(int k, vector<int> nums) {
     // write your code here
     if(k <= 0 || k > nums.size())return -1;
     return partion(nums, 0, nums.size() - 1, k);
 }