/*
 * Sorts an array while keeping metadata in sync.
 * This sort is unstable and its average performance is
 *   O(data.size() * log2(data.size()).
 * Input assertions: for any valid index 'i' in data, index 'i' is valid in
 *   metadata. 'from' and 'to' are valid indices into data. 'to' can be
 *   the maximum value for the type 'unsigned int'.
 * Invariant: index 'i' in metadata describes index 'i' in data.
 * Input: 'data', data to sort.
 *        'metadata', data describing the values in 'data'.
 *        'sortFunction', function determining the sort order of two elements.
 *        'from', index of the first element in the range to sort.
 *        'to', index of the last element in the range to sort.
 */
template <class Data, class Metadata> void quickSort(std::vector<Data>& data, std::vector<Metadata>& metadata, int (*sortFunction) (Data&, Data&), const unsigned int from, const unsigned int to)
{
    if (isSorted(data, sortFunction, from, to))
        return;

    unsigned int pivotIndex = choosePivot(data, sortFunction, from, to);
    unsigned int newPivotIndex = partition(data, metadata, sortFunction, from, to, pivotIndex);
    if (newPivotIndex > 0)
        quickSort(data, metadata, sortFunction, from, newPivotIndex - 1);
    if (newPivotIndex < to)
        quickSort(data, metadata, sortFunction, newPivotIndex + 1, to);
}
Exemple #2
0
/*
** QuickSort algorithm (recursive function)
*/
static void auxsort (lua_State *L, IdxT lo, IdxT up,
                                   unsigned int rnd) {
  while (lo < up) {  /* loop for tail recursion */
    IdxT p;  /* Pivot index */
    IdxT n;  /* to be used later */
    /* sort elements 'lo', 'p', and 'up' */
    lua_geti(L, 1, lo);
    lua_geti(L, 1, up);
    if (sort_comp(L, -1, -2))  /* a[up] < a[lo]? */
      set2(L, lo, up);  /* swap a[lo] - a[up] */
    else
      lua_pop(L, 2);  /* remove both values */
    if (up - lo == 1)  /* only 2 elements? */
      return;  /* already sorted */
    if (up - lo < RANLIMIT || rnd == 0)  /* small interval or no randomize? */
      p = (lo + up)/2;  /* middle element is a good pivot */
    else  /* for larger intervals, it is worth a random pivot */
      p = choosePivot(lo, up, rnd);
    lua_geti(L, 1, p);
    lua_geti(L, 1, lo);
    if (sort_comp(L, -2, -1))  /* a[p] < a[lo]? */
      set2(L, p, lo);  /* swap a[p] - a[lo] */
    else {
      lua_pop(L, 1);  /* remove a[lo] */
      lua_geti(L, 1, up);
      if (sort_comp(L, -1, -2))  /* a[up] < a[p]? */
        set2(L, p, up);  /* swap a[up] - a[p] */
      else
        lua_pop(L, 2);
    }
    if (up - lo == 2)  /* only 3 elements? */
      return;  /* already sorted */
    lua_geti(L, 1, p);  /* get middle element (Pivot) */
    lua_pushvalue(L, -1);  /* push Pivot */
    lua_geti(L, 1, up - 1);  /* push a[up - 1] */
    set2(L, p, up - 1);  /* swap Pivot (a[p]) with a[up - 1] */
    p = partition(L, lo, up);
    /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */
    if (p - lo < up - p) {  /* lower interval is smaller? */
      auxsort(L, lo, p - 1, rnd);  /* call recursively for lower interval */
      n = p - lo;  /* size of smaller interval */
      lo = p + 1;  /* tail call for [p + 1 .. up] (upper interval) */
    }
    else {
      auxsort(L, p + 1, up, rnd);  /* call recursively for upper interval */
      n = up - p;  /* size of smaller interval */
      up = p - 1;  /* tail call for [lo .. p - 1]  (lower interval) */
    }
    if ((up - lo) / 128 > n) /* partition too imbalanced? */
      rnd = l_randomizePivot();  /* try a new randomization */
  }  /* tail call auxsort(L, lo, up, rnd) */
}
Exemple #3
0
void quickSort(int *A, int left, int right, int length)
{
	if (left >= right) {
		return;
	}
	int i = left + 1;
	int j = left + 1;
	int pivot = choosePivot(A, left, right);
	swap(A, pivot, left);
	while (j <= right) {
		if (A[j] < A[left]) {
			swap(A, i, j);
			i += 1;
		}
		j += 1;
	}
	swap(A, left, i - 1);
	printArray(A, length);
	quickSort(A, left, i - 2, length);
	quickSort(A, i, right, length);
}
Exemple #4
0
void quickSort(int *a, int left, int right) {
	int pivot, i, j;

	if(left < right) {
		swap(&a[left], &a[choosePivot(left, right)]);
		pivot = a[left];
		i = left + 1;
		j = right;

		while(i <= j) {
			while((a[i] <= pivot) && (i <= right))
				i++;
			while((a[j] > pivot) && (j >= left))
				j--;
			if(i < j)
				swap(&a[i], &a[j]);	
		}
		swap(&a[left], &a[j]);
		quickSort(a, left, j - 1);
		quickSort(a, j + 1, right);
	}
}
Exemple #5
0
void quickSort(PhoneCall *phoneCall, int left, int right) {
	int i, j;
	PhoneCall pivot;

	if(left < right) {
		swap(&phoneCall[left], &phoneCall[choosePivot(left, right)]);
		pivot = phoneCall[left];
		i = left + 1;
		j = right;

		while(i <= j) {
			while((phoneCall[i].during <= pivot.during) && (i <= right))
				i++;
			while((phoneCall[j].during > pivot.during) && (j >= left))
				j--;
			if(i < j)
				swap(&phoneCall[i], &phoneCall[j]);	
		}
		swap(&phoneCall[left], &phoneCall[j]);
		quickSort(phoneCall, left, j - 1);
		quickSort(phoneCall, j + 1, right);
	}
}
Exemple #6
0
void quicksort(int arr[], int left, int right) {
	test += (right - left - 1);

	int tmp;
	int l = left;
	int r = right;

	int pivot = choosePivot(l, r);
	
	do {
		while (arr[l] < arr[pivot]) {
			l++;
		}
		
		while (arr[r] > arr[pivot]) {
			r--;
		}
		
		if (l <= r) {
			tmp = arr[l];
			arr[l] = arr[r];
			arr[r] = tmp;
			
			l++;
			r--;
		}
	} while (l <= r);
	
	if (left < r) {
		quicksort(arr, left, r);
	}
	
	if (l < right) {
		quicksort(arr, l, right);
	}
}