Exemplo n.º 1
0
int main(){
	int aSize = 0;
	cnt = 0;
	cout<<"Enter array size: ";cin>>aSize;
	int *strArray = new int[aSize];
	int *orgArray = new int[aSize];
	int search = aSize;
	//getArray(strArray,aSize);
	fillRand(strArray,aSize);
	printAr(strArray,aSize);
	copyArray(strArray,orgArray,aSize);
	cout<<"******** standard search **********"<<endl;
	cout<<"Found "<<search<<" at ";
	cout<<standardSearch(strArray,aSize, search);
	cout<<" comparisons: "<<cnt<<endl<<"***********************"<<endl<<endl;
	
	cout<<"******** binary search **********"<<endl;
	cout<<"Found "<<search<<" at ";
	selectionSort(strArray,aSize);
	cout<<binarySearch(strArray,aSize,search);
	cout<<" comparisons: "<<cnt<<endl<<"***********************"<<endl<<endl;
	copyArray(orgArray,strArray,aSize);
	
	
	cout<<"******** selection sort **********"<<endl;
	selectionSort(strArray,aSize);
	printAr(strArray,aSize);
	cout<<" comparisons: "<<cnt<<endl<<"***********************"<<endl<<endl;
	copyArray(orgArray,strArray,aSize);
	
	cout<<"******** insert sort **********"<<endl;
	insertSort(strArray,aSize);
	printAr(strArray,aSize);
	cout<<" comparisons: "<<cnt<<endl<<"***********************"<<endl<<endl;
	copyArray(orgArray,strArray,aSize);
	
	cout<<"******** Bubble sort **********"<<endl;
	BubbleSort(strArray,aSize);
	printAr(strArray,aSize);
	cout<<" comparisons: "<<cnt<<endl<<"***********************"<<endl<<endl;
	return 0;
}
Exemplo n.º 2
0
/*
This function performs an insertion sort on the input array,
ar. It is expectatd that the input array is unsorted.

    Usage:

        int _ar_size = 5;
        int _ar[_ar_size], _ar_i;
        // initialize the integer array
        for(_ar_i = 0; _ar_i < _ar_size; _ar_i++)
        {
            // reading values in at each pass.
            scanf("%d", &_ar[_ar_i]);
        }

*/
void insertionSort(int arSize, int *  ar)
{
    // key will be used to hold the current value
    // of the insertion sort's outer loop (the
    // current value to be sorted).
    int key;

#ifdef DEBUG
    printAr(arSize, ar);
    printf("*************************************************************\n");
    printf("\n");
#endif

    // Iterate, counting up from the first index in the array
    for(int j=0; j<arSize; j++)
    {
        // i is the current point in the outer loop.
        int i = j - 1;

        bool doPrintAr = false;

        // copy the current value to key for comparison
        key = ar[j];
#ifdef DEBUG
        printf("%d________________\n", j);
        printf("key to sort: %d\n", key);
#endif

        // Iterate backwards from i as long as next (counting backwards)
        // value is greater than the current value of key.

        while(i>=0 && compare(ar[i], key) > 0)
        {
#ifdef DEBUG
            printf("-%d-----\n", i);
            printf("innerKey: %d\n", i);
#endif
            // Shift values one item to the right with each pass.
            //int poo;
            //poo = ar[i + 1];
            //printf("%d\n", poo);
            ar[i + 1] = ar[i];
            ar[i] = key;
            // Print the new array
            //printAr(arSize, ar);
            i--;
        }

        ar[i + 1] = key;

#ifdef DEBUG
        printf("%d___\n", ar[i + 1]);
#endif

        if(j != 0)
        {
            printAr(arSize, ar);
        }

#ifdef DEBUG
        printf("\n");
#endif
    }
}