Beispiel #1
0
// Quicksort with a build-in loop unrolled insertionsort 
// for when the input array is small
void quicksort1(int N, int M) {
  quicksort0(N, M); 
  /*
  // printf("Enter quicksort0 N: %d M: %d\n", N, M);
  while ( N < M ) {
    int L = M - N;
    if ( L <= 10 ) {
      insertionsort1(N, M);
      return;
    }
    // 10 < L
    int pn = N;
    int pm = M;
    int p0 = (pn+pm)/2;
    if (L > 40) { // do median of 9
      int d = L/8;
      pn = med(A, pn, pn + d, pn + 2 * d, compareXY);
      p0 = med(A, p0 - d, p0, p0 + d, compareXY);
      pm = med(A, pm - 2 * d, pm - d, pm, compareXY);
    }
    p0 = med(A, pn, p0, pm, compareXY); // p0 is index to 'best' pivot ...
    iswap(N, p0, A); // ... and is put in first position as required by quicksort0

    register void *p = A[N]; // pivot
    register int i, j;
    i = N;
    j = M;
    register void *ai; void *aj;

    // Split array in two
    while ( i < j ) {
      aj = A[j];
      while ( compareXY(p, aj) < 0 ) { 
	j--; 
	aj = A[j]; 
      }
      if ( j <= i ) break;
      // i < j 
      A[i] = aj; // fill hole !
      i++; ai = A[i];
      while ( i < j && compareXY(ai, p) <= 0 ) {
	i++; ai = A[i]; 
      }
      if ( j <= i ) break;
      A[j] = ai; // fill hole !
      j--;
    }
    A[i] = p;
    int ia = i-1; int ib = i+1; 
    if ( i-N < M-i ) { 
      if ( N < ia ) quicksort1(N, ia);  
      N = ib; 
    } else { 
      if ( ib < M ) quicksort1(ib, M);  
      M = ia; 
    }
  }
  */
} // end of quicksort1
Beispiel #2
0
void cut2(int N, int M) { 
  // printf("cut2 %d %d \n", N, M);
  int L = M - N;
  if ( L < cut2Limit ) { 
    quicksort0(N, M);
    return;
  }
  int depthLimit = 2 * floor(log(L));
  cut2c(N, M, depthLimit);
} // end cut2
Beispiel #3
0
// cut2 is used as a best in class quicksort implementation 
// with a defense against quadratic behavior due to duplicates
// cut2 is a support function to call up the workhorse cut2c
void cut2(void **A, int N, int M, int (*compare)()) { 
  // printf("cut2 %d %d %d\n", N, M, M-N);
  int L = M - N;
  if ( L < cut2Limit ) { 
    quicksort0(A, N, M, compare);
    return;
  }
  int depthLimit = 1 + 2.5 * floor(log(L));
  cut2c(A, N, M, depthLimit, compare);
} // end cut2