/*******************************************************************
  QuickSort : Perform the quick sort operation. If size of array is
            : below the threshold, then use bubble sort, else
	    : divide the job and insert one part into the task stack
 *******************************************************************/
QuickSort(register int i, register int j)
{
    register int pivot, k;

QSORT:
    /* pivot is index of the pivot element */
    if(j-i+1 < BubbleThresh) {
        if(bubble) BubbleSort(i,j);
        else LocalQuickSort(i,j);
        return;
    }
    pivot = FindPivot(i,j);
    k = Partition(i,j,gMem->A[pivot]);
    if(k-i > j-k) {
        /* The lower half [i through (k-1)] contains more entries.      */
        /* Put the other half into the queue of unsorted subarrays      */
        /* and recurse on the other half.                               */
        PushWork(k,j);
        j=k-1;
        goto QSORT;
        /*    QuickSort(i, k-1); */ /* eliminating tail recursion */
    }
    else {
        PushWork(i,k-1);
        i=k;
        goto QSORT;
        /*     QuickSort(k,j); */ /* Eliminating tail recursion */
    }
}
Esempio n. 2
0
//
//
//	void LocalQuickSort(int,int);
//
//
void LocalQuickSort(int i, int j){
    int pivot, k;

    if (j <= i) {
    	return;
    }
    
    if (debug) {
    	fprintf(stderr,"\t%d: Local QuickSorting %d-%d.\n", Tmk_proc_id, i, j);
    }

    pivot = FindPivot(i,j);	
	//pivot = (i + j) >> 1;

    k = Partition(i, j, gMem->A[pivot]);
    
    if (debug) {
    	fprintf(stderr,"\t%d: Splitting at %d-%d:%d-%d\n", Tmk_proc_id,i,k-1,k,j);
    }

    if (k > i) {
		LocalQuickSort(i, k-1);
		LocalQuickSort(k,	j);
    }
    else {
		LocalQuickSort(i + 1, j);
    }
    
}
Esempio n. 3
0
//
//	void QuickSort(int,int);
//
void QuickSort(int i, int j){
    int pivot, k;

    if (debug) {
    	fprintf(stderr,"\t%d: QuickSorting %d-%d.\n", Tmk_proc_id, i, j);
    }

    // pivot is index of the pivot element
    if (j-i+1 < BubbleThresh) {
		if (bubble) {
			BubbleSort(i,j);
		}
		else {
			LocalQuickSort(i,j);
		}
		return;
    } 
    pivot = FindPivot(i,j);

    k = Partition(i, j, gMem->A[pivot]);

	if(debug){
		fprintf(stderr,"\t%d: Splitting at %d-%d:%d-%d\n", Tmk_proc_id,i,k-1,k,j);
	}
    
	#define FAST_SEQUENTIAL
	#ifdef FAST_SEQUENTIAL
	    if (Sequential) {
			QuickSort(i, k-1);
			QuickSort(k, j);
			return;
	    }
	#endif //	FAST_SEQUENTIAL
	
	if (k-i > j-k) {
		// The lower half [i through (k-1)] contains more entries.	
		// Put the other half into the queue of unsorted subarrays	
		// and recurse on the other half.			
		PushWork(k,  j);
		QuickSort(i, k-1);
    }
    else {
		// The lower half [i through (k-1)] contains more entries.
		// Put the other half into the queue of unsorted subarrays
		// and recurse on the other half.			
		PushWork(i,k-1);
		QuickSort(k,j);
    }

}
LocalQuickSort(int i, int j)
{
    int pivot, k;
LQSORT:
    if (j <= i) return;
    pivot = FindPivot(i,j);

    k = Partition(i,j,gMem->A[pivot]);
    if (k > i) {
        LocalQuickSort(i, k-1);
        i=k;
        goto LQSORT;
    }
    else {
        i=i+1;
        goto LQSORT;
    }
}