void ibis::array_t<T>::qsort(array_t<uint32_t>& ind, uint32_t front,
			     uint32_t back, uint32_t lvl) const {
    while (back > front + QSORT_MIN) { // more than QSORT_MIN elements
	// find the pivot
	uint32_t p = partition(ind, front, back);
	// make sure the smaller half is sorted
	if (p >= back) {
	    front = back; // all values are the same
	}
	else if (p - front <= back - p) {
	    // sort [front, p-1]
	    if (p > front + QSORT_MIN) { // use quick sort
		if (lvl >= QSORT_MAX_DEPTH)
		    hsort(ind, front, p);
		else
		    qsort(ind, front, p, lvl+1);
	    }
	    else if (p > front + 2) { // between 2 and QSORT_MIN elements
		isort(ind, front, p);
	    }
	    else if (p == front + 2) { // two elements only
		if (m_begin[ind[front]] > m_begin[ind[front+1]]) {
		    uint32_t tmp = ind[front];
		    ind[front] = ind[front+1];
		    ind[front+1] = tmp;
		}
	    }
	    front = p;
	}
	else { // sort [p, back-1]
	    if (p + QSORT_MIN < back) { // more than QSORT_MIN elements
		if (lvl >= QSORT_MAX_DEPTH) {
		    hsort(ind, p, back);
		}
		else {
		    qsort(ind, p, back, lvl+1);
		}
	    }
	    else if (p + 2 < back) { // 2 to QSORT_MIN elements
		isort(ind, p, back);
	    }
	    else if (p + 2 == back) { // two elements only
		if (m_begin[ind[p]] > m_begin[ind[p+1]]) {
		    back = ind[p];
		    ind[p] = ind[p+1];
		    ind[p+1] = back;
		}
	    }
	    back = p;
	}
    }
    // use insertion sort to clean up the few left over elements
    isort(ind, front, back);
#if DEBUG+0 > 2 || _DEBUG+0 > 2
    ibis::util::logger lg(4);
    lg.buffer() << "DEBUG -- qsort(" << front << ", " << back << ")\n";
    for (size_t i = front; i < back; ++i)
	lg.buffer() << ind[i] << "\t" << m_begin[ind[i]] << "\n";
#endif
} // qsort
예제 #2
0
파일: 1116.c 프로젝트: 12ashk/etude
int main(void)
{
	int n;//number of data
	int i, j;
	printf("input number of data: ");
	scanf("%d", &n);
	makeheap(n);

	hsort(n-1);

	return 0;
}
예제 #3
0
int main(int argc, char **argv)
{
	int n, i;
	scanf("%d", &n);
	char base[n][100];
	for(i = 0; i < n; i++) 
		scanf("%s", base[i]);
	hsort(base, n, 100, compare);
	for(i = 0; i < n; i++)
		printf("%s\n", base[i]);
	
	
	return 0;
}
예제 #4
0
int main()
{
    int arr[] = {7, 4, 2, 1, 8, 3, 0, 9, 5, 6, 1, 2, 3, 9, 4, 0, 9};
	//int arr[] = {7, 4, 2, 1, 8, 3, 0, 9, 5, 6};
    int i;

    parray(arr, 0, (sizeof(arr) / sizeof(int)) - 1);

    hsort(arr, (sizeof(arr) / sizeof(int)));

    parray(arr, 0, (sizeof(arr) / sizeof(int)) - 1);

return 0;
}
예제 #5
0
파일: heapsort.c 프로젝트: PepSalehi/clrs
int
main(int argc, char *argv[])
{
        int *A, i;

        if ((A = malloc(--argc * sizeof(*A))) == NULL)
                err(EXIT_FAILURE, "Not enough memory.");
        for (i = 0; i<argc; i++)
                A[i] = atoi(argv[i+1]);
        hsort(A, argc);
        for (i = 0; i < argc; i++)
                printf("%d%c", A[i], (i == argc-1 ? '\n' : ' '));
        free(A);
        return 0;
}
예제 #6
0
void main()
{

int i=0;
int arr1[] = {4,1,3,2,16,9,10,14,8,7};
hp_s* hp = build_hp(arr1,sizeof(arr1)/sizeof(int));

printf("Printing new array positions:");
for(i=0;i<sizeof(arr1)/sizeof(int);i++)
	printf("%d ->",arr1[i]);

//Heap sorting
hsort(hp);
printf("\n After Heap sorting:");
for(i=0;i<sizeof(arr1)/sizeof(int);i++)
	printf("%d ->",arr1[i]);
}
예제 #7
0
파일: 1116.c 프로젝트: 12ashk/etude
int hsort(int n)
{
	if(n == 0){
		printf("%d\n", A[0]);
		return 0;
	}
	else{
		printf("%d ", A[0]);
		int i = 0, j = 1, temp;
		A[0] = A[n];
		A[n] = 0;
		while(1){
			if((A[i] <= A[j] && A[i] <= A[j+1])
				|| i >= n - 1 || A[j+1] == 0){
				break;
			}
			else if(A[j] < A[j+1] && A[j] != 0){
				temp = A[j];
				A[j] = A[i];
				A[i] = temp;
				i = j;
				j = j * 2 + 1;
			}
			else{
				temp = A[j+1];
				A[j+1] = A[i];
				A[i] = temp;
				i = j + 1;
				j = j  * 2 + 3;
			}
		}
		n--;
		hsort(n);
	}
	return 0;
}
예제 #8
0
파일: main.c 프로젝트: rashmi18/SwissKnife
int main()
{
	block blk[MAX];

	int arr[MAX];// = {16, 47, 5, 12, 67, 21, 7};
	int i;
	int output[MAX];
	int limit = MAX - 1, outcntr = 0;

	for(i = 0; i < MAX; i++) {
		blk[i].id = MAX - i;
		arr[i] = rand() % MAX;
	}

	printf("array: ");

	for(i = 0; i < MAX; i++)
		printf("%d, ", arr[i]);

	printf("\n");

/*	heapify(arr, MAX);

	printf("min heap generated is: ");

	for(i = 0; i < MAX; i++)
		printf("%d, ", arr[i]);

	printf("now sorting\n");

	while(limit >= 0) {
		output[outcntr++] = arr[0];

		arr[0] = arr[limit];

		if(limit > 0) {
			limit--;	

			heapify(arr, limit + 1);
		}
		else
			break;
	}

	printf("sorted output: ");

	for(i = 0; i < MAX; i++)
		printf("%d, ", output[i]);

	printf("\n");*/

	hsort((void *) &arr, MAX, sizeof(int), cmpasc);

	printf("sorted output: ");

	for(i = 0; i < MAX; i++)
		printf("%d, ", arr[i]);

	printf("\n");

	return 0;
}
예제 #9
0
파일: wgraph.cpp 프로젝트: bunnywj/GProject
void wgraph::esort() {
// Sort the edges by cost.
	hsort(); bldadj();
}