Exemplo n.º 1
0
static void TestBasicSort()
{
    // This just tests the insertion sort and heap sort functions; there's
    // no point running introsort on this small a dataset, it'll just use
    // insertion sort on it anyway.
    sInt x[nTestInts];

    // test plain direct insertion sort
    InitTest(x);
    sInsertionSort(sAll(x));
    VerifySorted(x,nTestInts);

    // test insertion sort with predicate (and reverse order)
    InitTest(x);
    sInsertionSort(sAll(x),sCmpGreater<sInt>());
    VerifyRevSorted(x,nTestInts);

    // test heapsort with direct compare
    InitTest(x);
    sHeapSort(sAll(x));
    VerifySorted(x,nTestInts);

    // test heapsort with predicate (and reverse order)
    InitTest(x);
    sHeapSort(sAll(x),sCmpGreater<sInt>());
    VerifyRevSorted(x,nTestInts);
}
Exemplo n.º 2
0
static void TestSequence1(const sChar *desc,sArrayRange<sInt> &seq)
{
    static const sInt nAttempts = 3;
    sStaticArray<sInt> arr(seq.GetCount());

    sPrintF(L"%-12s ",desc);
    for(sInt i=0; i<sCOUNTOF(sortFuncs1); i++)
    {
        // report minimum runtime over nAttempts runs
        sInt minTime = 0x7fffffff;

        for(sInt j=0; j<nAttempts; j++)
        {
            arr.Clear();
            sCopyMem(arr.AddMany(seq.GetCount()),&seq[0],seq.GetCount()*sizeof(sInt));

            ThrashCache();

            sU64 start = sGetTimeUS();
            sortFuncs1[i](arr);
            minTime = sMin(minTime,(sInt) (sGetTimeUS() - start));
        }

        VerifySorted(&arr[0],arr.GetCount());

        sPrintF(L"%10d ",minTime);
    }

    sPrint(L"\n");
}
Exemplo n.º 3
0
// just a correctness test on tons of random arrays
static void TestSort3()
{
    static const sInt count = 10000;

    sRandomKISS rand;
    sFixedArray<sInt> arr(count);

    sPrint(L"Sort3");

    rand.Seed(2187462);

    for(sInt run=0; run<10000; run++)
    {
        sInt cnt = (count/2) + rand.Int(count/2);
        for(sInt i=0; i<cnt; i++)
            arr[i] = rand.Int32();

        sIntroSort(sArrayRange<sInt>(&arr[0],&arr[0]+cnt));
        VerifySorted(&arr[0],cnt);

        if((run%200) == 0)
            sPrint(L".");
    }

    sPrint(L"\n");
}
Exemplo n.º 4
0
int main(int argc, char* argv[])
{
	int seed = 40;
	int n = 10;
	int max = 100;

	int *a = nullptr;

	// Insertion sort
	a = CreatA(a, n, seed, max);
	printArray(a, n, "Original");

	InsertionSort(a, n);
	//printArray(a, n, "InsertSort");
	VerifySorted(a, n);

	// Selection sort
	a = CreatA(a, n, seed, max);
	SelectionSort(a, n);
	//printArray(a, n, "selectSort");
	VerifySorted(a, n);

	a = CreatA(a, n, seed, max);
	MergeSort(a, n);
	//printArray(a, n, "MergeSort");
	VerifySorted(a, n);

	a = CreatA(a, n, seed, max);
	QuickSort(a, n);
	//printArray(a, n, "QuickSort");
	VerifySorted(a, n);

	a = CreatA(a, n, seed, max);
	BubbleSort(a, n);
	printArray(a, n, "BubbleSort");
	VerifySorted(a, n);

	int value = 22;
	//BinarySearch(a, n, value);
	//BinarySearchIter(a, n, value);
	//RunBinarySearchCircular(a, n, value);

	value = 27;
	/*BinarySearch(a, n, value);
	BinarySearchIter(a, n, value);
	RunBinarySearchCircular(a, n, value);
*/
	a = CreatA(a, n, seed, max);
	printArray(a, n, "Pre  min heap sort");
	RunSortMinHeap(a, n);
	printArray(a, n, "MinHeapSort");
	VerifySorted(a, n);

	a = CreatA(a, n, seed, max);
	printArray(a, n, "Pre  max heap sort");
	RunSortMaxHeap(a, n);
	printArray(a, n, "MaxHeapSort");
	VerifySorted(a, n);

	printf("\nMedian %d\n", Median(a, n));

	return 0;
}