示例#1
0
void quickSort(PointList *array,int start,int end)
{
    if(start < end)
    {
        int p = randomPartition(array,start,end);
        quickSort(array,start,p);
        quickSort(array,p+1,end);
    }
}
示例#2
0
/**
 * CensusData::quickSortHelp.
 *
 * Private helper function that is called recursively in order
 * to run the quicksort algorithim.
 *
 * @param int type, int p, int r.
 * type == 0 to sort by population, type == 1 to sort by name.
 * int p = starting index of vector.
 * int r = ending index of vector.
 */
void CensusData::quickSortHelp(int type, int p, int r)
{
  int q;
  if(p < r)
  {
    q = randomPartition(type, p, r);  //sort a randomized partition
    quickSortHelp(type, p, q - 1);    //quicksort the lower half of vector
    quickSortHelp(type, q + 1, r);    //quicksort the upper half of vector
  }
}
    int kthSmallest(vector<int>& arr, int l, int r, int k)
    {

        if (k > 0 && k <= r - l + 1)
        {
            int pos = randomPartition(arr, l, r);
        
            if (pos-l == k-1)
                return arr[pos];
            else if(pos-l > k-1)
                return kthSmallest(arr, l, pos-1, k);
            else
                return kthSmallest(arr, pos+1, r, k-pos+l-1);
        }
        return INT_MAX;
    }
示例#4
0
__declspec(dllexport) void randomizedQuickSort(int qarr[], int low, int high)
{
	try 
	{
		if (low < high)
		{
			int pi = randomPartition(qarr, low, high);
			randomizedQuickSort(qarr, low, pi-1);
			randomizedQuickSort(qarr, pi + 1, high);
		}
	}
		catch(...)
		{
			throw "Some exception thrown";
		}
}