Пример #1
0
int quicksort(void *array, int dim, int pivot) {
    int *indexes, i, diff;

    indexes = (int*)malloc(dim*sizeof(int));
    if (indexes == NULL) return -ENOMEM;

    diff = dim - pivot;
    for (i = 0 ; i < dim ; i++) {
        if (i < pivot)
            indexes[i] = diff + i;
        else indexes[i] = i - pivot;
    }
#ifdef __DEBUG_QS__
    printf("Sort an array of %d items from pivot %d containing:\n", dim, pivot);
    dump_char_array((char**)array, dim);
    printf("Unsorted indexes: ");
    dump_int_array(indexes, dim);
#endif
    __quicksort(array, indexes, 0, dim-1);

#ifdef __DEBUG_QS__
    printf("Sorted into:\n");
    dump_char_array((char**)array, dim);
    printf("Sorted indexes: ");
    dump_int_array(indexes, dim);
#endif

    free(indexes);
    return 0;
}
Пример #2
0
static void
test_endswap_int (void)
{	int orig [4], first [4], second [4] ;
	int k ;

	printf ("    %-24s : ", "test_endswap_int") ;
	fflush (stdout) ;

	for (k = 0 ; k < ARRAY_LEN (orig) ; k++)
		orig [k] = 0x76543210 + k ;

	endswap_int_copy (first, orig, ARRAY_LEN (first)) ;
	endswap_int_copy (second, first, ARRAY_LEN (second)) ;

	if (memcmp (orig, first, sizeof (orig)) == 0)
	{	printf ("\n\nLine %d : test 1 : these two array should not be the same:\n\n", __LINE__) ;
		dump_int_array ("orig", orig, ARRAY_LEN (orig));
		dump_int_array ("first", first, ARRAY_LEN (first));
		exit (1) ;
		} ;

	if (memcmp (orig, second, sizeof (orig)) != 0)
	{	printf ("\n\nLine %d : test 2 : these two array should be the same:\n\n", __LINE__) ;
		dump_int_array ("orig", orig, ARRAY_LEN (orig));
		dump_int_array ("second", second, ARRAY_LEN (second));
		exit (1) ;
		} ;

	endswap_int_array (first, ARRAY_LEN (first)) ;

	if (memcmp (orig, first, sizeof (orig)) != 0)
	{	printf ("\n\nLine %d : test 3 : these two array should be the same:\n\n", __LINE__) ;
		dump_int_array ("orig", orig, ARRAY_LEN (orig));
		dump_int_array ("first", first, ARRAY_LEN (first));
		exit (1) ;
		} ;

	endswap_int_copy (first, orig, ARRAY_LEN (first)) ;
	endswap_int_copy (first, first, ARRAY_LEN (first)) ;

	if (memcmp (orig, first, sizeof (orig)) != 0)
	{	printf ("\n\nLine %d : test 4 : these two array should be the same:\n\n", __LINE__) ;
		dump_int_array ("orig", orig, ARRAY_LEN (orig));
		dump_int_array ("first", first, ARRAY_LEN (first));
		exit (1) ;
		} ;

	puts ("ok") ;
} /* test_endswap_int */