Ejemplo n.º 1
0
int main(void)
{
	int randomArray[MAX_ARRAY];
	int randomNum,i;
    int* compares;
    int* moves;
    int comNum = 0;
    int moveNum = 0;
    
    compares = &comNum;
    moves = &moveNum;
    
    //Randomize the array
	srand((unsigned int)time(NULL));
	for(i=0;i<MAX_ARRAY;i++){
		randomNum = (rand() % (7500+1 - 0))+0;
		randomArray[i] = randomNum;
	}//end for
    
    //Heap Sorting
    sortHeap(randomArray,MAX_ARRAY-1,compares,moves);
	if(MAX_ARRAY == 100)
        printArray(randomArray);
    printf("\nArray Size: %d\n",MAX_ARRAY);
    printf("random list heapSort:  compares = %d  moves = %d\n",*compares,*moves);
    makeNearSort(randomArray);
    sortHeap(randomArray,MAX_ARRAY-1,compares,moves);
    printf("nearly ordered heapSort:  compares = %d  moves = %d\n\n",*compares,*moves);
    
    //Shell Sorting
    sortShell(randomArray,MAX_ARRAY-1,compares,moves);
	if(MAX_ARRAY == 100)
        printArray(randomArray);
    printf("\nArray Size: %d\n",MAX_ARRAY);
    printf("random list shellSort:  compares = %d  moves = %d\n",*compares,*moves);
    makeNearSort(randomArray);
    sortShell(randomArray,MAX_ARRAY-1,compares,moves);
    printf("nearly ordered shellSort:  compares = %d  moves = %d\n\n",*compares,*moves);
    
    //Quick Sorting
    *compares = 0;
    *moves = 0;
    sortQuick(randomArray,0,MAX_ARRAY-1,compares,moves);
    if(MAX_ARRAY == 100)
        printArray(randomArray);
    printf("\nArray Size: %d\n",MAX_ARRAY);
    printf("random list quickSort:  compares = %d  moves = %d\n",*compares,*moves);
    makeNearSort(randomArray);
    *compares = 0;
    *moves = 0;
    sortQuick(randomArray,0,MAX_ARRAY-1,compares,moves);
    printf("nearly ordered quickSort:  compares = %d  moves = %d\n\n",*compares,*moves);
    
    printf("The most efficient sort for a large, random list is: \n");   //NOT DONE YET
    printf("The most efficient sort for a large, nearly ordered list is: \n");
    printf("The most efficient sort for a small, random list is: \n");
    printf("The most efficient sort for a small, nearly ordered list is: \n");
    
	return 0;
}
Ejemplo n.º 2
0
		void TestHeap::testCase1() {
			int ia[9] = {0,1,2,3,4,8,9,3,5};
			Vector<int> ivec(ia, ia+9);
			makeHeap(ivec.begin(), ivec.end());
			for(int i=0; i<ivec.size(); ++i)
			std::cout << ivec[i] << ' ';
			// 9 5 8 3 4 0 2 3 1
			std::cout << std::endl;
			ivec.push_back(7);
			pushHeap(ivec.begin(), ivec.end());
			for(int i=0; i<ivec.size(); ++i)
			std::cout << ivec[i] << ' ';
			// 9 7 8 3 5 0 2 3 1 4
			std::cout << std::endl;
			popHeap(ivec.begin(), ivec.end());
			std::cout << ivec.back() << std::endl;
			// 9. return but no remove.
			ivec.pop_back();
			// remove last elem and no return
			for(int i=0; i<ivec.size(); ++i)
			std::cout << ivec[i] << ' ';
			std::cout << std::endl;
			// 8 7 4 3 5 0 2 3 1
			sortHeap(ivec.begin(), ivec.end());
			for(int i=0; i<ivec.size(); ++i)
			std::cout << ivec[i] << ' ';
			// 0 1 2 3 3 4 5 7 8
			std::cout << std::endl;
			std::cout << "case1 passed" << std::endl;
		}
Ejemplo n.º 3
0
int main(int argc, const char * argv[])
{
  	TYPE task1;
	TYPE task2;
	TYPE task3;
	TYPE task4;
	TYPE task5;
	TYPE task6;
	TYPE task7;
	TYPE task8;
	TYPE task9;
	TYPE task10;

	DynArr *mainList;
	mainList = createDynArr(10);

	/* create tasks */
	task1 = createTask(9, "task 1");
	task2 = createTask(3, "task 2");
	task3 = createTask(2, "task 3");
	task4 = createTask(4, "task 4");
	task5 = createTask(5, "task 5");
	task6 = createTask(7, "task 6");
	task7 = createTask(8, "task 7");
	task8 = createTask(6, "task 8");
	task9 = createTask(1, "task 9");
	task10 = createTask(0, "task 10");

	/* add tasks to the dynamic array */
	addHeap(mainList, task1, compare);
	addHeap(mainList, task2, compare);
	addHeap(mainList, task3, compare);
	addHeap(mainList, task4, compare);
	addHeap(mainList, task5, compare);
	addHeap(mainList, task6, compare);
	addHeap(mainList, task7, compare);
	addHeap(mainList, task8, compare);
	addHeap(mainList, task9, compare);
	addHeap(mainList, task10, compare);


	printf("Before Sort Called \n");
        printDynArr(mainList, print_type);

	/* sort tasks */
	sortHeap(mainList, compare);

	printf("After Sort Called \n");

	/* print sorted tasks from the dynamic array */
        printDynArr(mainList, print_type);

	return 0;
}
Ejemplo n.º 4
0
/*  Print the list

    param:  heap    pointer to the list
    pre:    the list is not empty
    post:   The tasks from the list are printed out in priority order.
			The tasks are not removed from the list.
*/
void printList(DynArr *heap)
{
    /* FIXME: Write this */
    struct DynArr *toPrint = createDynArr(sizeDynArr(heap));
    copyDynArr(heap, toPrint);
    int i, max = sizeDynArr(toPrint);
    sortHeap(toPrint);
    // call print_type() on each node
    for(i = 0; i < max; i++)
        print_type(getDynArr(toPrint, i));
        deleteList(toPrint);
}
Ejemplo n.º 5
0
Archivo: main2.c Proyecto: vb3/homework
int main (int argc, const char * argv[])
{
  	TYPE task1, task2, task3, task4, task5, task6, task7, task8, task9, task10;
	DynArr mainList;
	int i;
	initDynArr(&mainList, 10);

	/* create tasks */
	task1 = createTask(9, "task 1");
	task2 = createTask(3, "task 2");
	task3 = createTask(2, "task 3");
	task4 = createTask(4, "task 4");
	task5 = createTask(5, "task 5");
	task6 = createTask(7, "task 6");
	task7 = createTask(8, "task 7");
	task8 = createTask(6, "task 8");
	task9 = createTask(1, "task 9");
	task10 = createTask(0, "task 10");
	
	/* add tasks to the dynamic array */
	addDynArr(&mainList, task1);
	addDynArr(&mainList, task2);
	addDynArr(&mainList, task3);
	addDynArr(&mainList, task4);
	addDynArr(&mainList, task5);
	addDynArr(&mainList, task6);
	addDynArr(&mainList, task7);
	addDynArr(&mainList, task8);
	addDynArr(&mainList, task9);
	addDynArr(&mainList, task10);

	/* sort tasks */
	sortHeap(&mainList);

	/* print sorted tasks from the dynamic array */
	for (i = 0; i < mainList.size; i++)
	{
	  	printf("%d\n", mainList.data[i].priority);
	}
    
    printList(&mainList);

	return 0;
}
Ejemplo n.º 6
0
heapElement binaryHeap<heapElement>::sortHeap(int index) {
    int l = left(index);
    int r = right(index);
    int length = size();

    // if there are no more childnodes, swap the value at the current index with the value at
    // end of the array, sort the value at the current index then remove and return the
    // last array element (the minimum element)
    if (length <= l) {
        heapElement value = heap[swap(index, length) - 1];
        bubble(index);
        heap.pop_back();
        return value;
    }

    // if the right node is in range and less then the left node then swap
    // the child with the right node, otherwise swap with the left
    sortHeap(swap(index, length > r && lt(r,l) ? r : l ));
}
Ejemplo n.º 7
0
/*  Print the list

    param:  heap    pointer to the list
    pre:    the list is not empty
    post:   The tasks from the list are printed out in priority order.
			The tasks are not removed from the list.
*/
void printList(DynArr *heap)
{
    assert(sizeDynArr(heap) > 0);
    DynArr *tempList; // temp list for sorting
    Task *tempTask; // temp task pointer
    int i;

    tempList = malloc(sizeof(sizeDynArr(heap)));
    copyDynArr(heap, tempList);
    sortHeap(tempList);

    // iterate through sorted array and print tasks
    printf("\nYour list sorted by priority:\n");
    for(i = sizeDynArr(tempList) - 1; i >= 0; i--) {
        tempTask = getDynArr(tempList, i);
        printf("%d.\t%s\n", tempTask->priority, tempTask->description);
    }
    printf("\n");
}
Ejemplo n.º 8
0
/*  Print the list

    param:  heap    pointer to the list
    pre:    the list is not empty
    post:   The tasks from the list are printed out in priority order.
			The tasks are not removed from the list.
*/
void printList(DynArr *heap)
{
    /* FIXME: Write this */
    /* unable to effectively print in sorted order w/ tempArray, the following code is w/ help from tutor Dustin Castor */

    assert(sizeDynArr(heap) > 0);

    /* sort and print in reverse in order that priority order is maintained */
    sortHeap(heap);

    int i;
    int fin = sizeDynArr(heap)-1;  /* last element */

    for (i = fin; i >= 0; i--)
    {
        print_type(getDynArr(heap, i));
    }

    /* rebuild to a heap from sorted array */
    _buildHeap(heap);


}
Ejemplo n.º 9
0
void heapsort(Vector& data)
{
  buildMaxHeap(data); // std::make_heap(data.begin(), data.end());
  sortHeap(data); // std::sort_heap(data.begin(), data.end());
}
Ejemplo n.º 10
0
int main(int argc, const char * argv[])
{
  Task *task1, *task2, *task3, *task4, *task5, *task6, *task7, *task8, *task9, *task10;
  Task *task11, *task12, *task13, *task14, *task15, *task16, *task17, *task18, *task19, *task20;
	DynArr *mainList;
	int i;
	mainList = createDynArr(10);

	/* create tasks */
	task1 = createTask(9, "task 1");
	task2 = createTask(3, "task 2");
	task3 = createTask(2, "task 3");
	task4 = createTask(4, "task 4");
	task5 = createTask(5, "task 5");
	task6 = createTask(7, "task 6");
	task7 = createTask(8, "task 7");
	task8 = createTask(6, "task 8");
	task9 = createTask(1, "task 9");
	task10 = createTask(10, "task 10");
    task11 = createTask(19, "task 11");
	task12 = createTask(13, "task 12");
	task13 = createTask(12, "task 13");
	task14 = createTask(14, "task 14");
	task15 = createTask(15, "task 15");
	task16 = createTask(17, "task 16");
	task17 = createTask(18, "task 17");
	task18 = createTask(16, "task 18");
	task19 = createTask(3, "task 19");
	task20 = createTask(110, "task 20");

	/* add tasks to the dynamic array */
	addHeap(mainList, task1);
	addHeap(mainList, task2);
	addHeap(mainList, task3);
	addHeap(mainList, task4);
	addHeap(mainList, task5);
	addHeap(mainList, task6);
	addHeap(mainList, task7);
	addHeap(mainList, task8);
	addHeap(mainList, task9);
	addHeap(mainList, task10);
    addHeap(mainList, task11);
	addHeap(mainList, task12);
	addHeap(mainList, task13);
	addHeap(mainList, task14);
	addHeap(mainList, task15);
	addHeap(mainList, task16);
	addHeap(mainList, task17);
	addHeap(mainList, task18);
	addHeap(mainList, task19);
	addHeap(mainList, task20);

#ifdef TESTHEAP
    printf("Print heap\n");
	for(i = 0; i < sizeDynArr(mainList);i++)
		print_type(getDynArr(mainList,i));


	while(!isEmptyDynArr(mainList))
	{
		TYPE v;
		v = getMinHeap(mainList);
		 printf("Removing: ");
		print_type(v);
		removeMinHeap(mainList);

        for(i = 0; i < sizeDynArr(mainList);i++)
            print_type(getDynArr(mainList,i));
	}
#endif

#ifdef TESTSORT

	printf("Before Sort Called \n");
	for(i = 0; i < sizeDynArr(mainList);i++)
			printf("DynArr[%d] = %d\n", i, ((Task*)getDynArr(mainList,i))->priority);


	/* sort tasks */
	sortHeap(mainList);
	printf("After Sort Called \n");

	/* print sorted tasks from the dynamic array */
	for(i = 0; i < sizeDynArr(mainList);i++)
				printf("DynArr[%d] = %d\n", i, ((Task*)getDynArr(mainList,i))->priority);

	return 0;

#endif
}
Ejemplo n.º 11
0
heapElement binaryHeap<heapElement>::pop (int index) { sortHeap(index + 1); };
Ejemplo n.º 12
0
int main(int argc, const char * argv[])
{
  Task *task1, *task2, *task3, *task4, *task5, *task6, *task7, *task8, *task9, *task10;
	DynArr *mainList;
	int i;
	mainList = createDynArr(10);

	/* create tasks */
	task1 = createTask(9, "task 1");
	task2 = createTask(3, "task 2");
	task3 = createTask(2, "task 3");
	task4 = createTask(4, "task 4");
	task5 = createTask(5, "task 5");
	task6 = createTask(7, "task 6");
	task7 = createTask(8, "task 7");
	task8 = createTask(6, "task 8");
	task9 = createTask(1, "task 9");
	task10 = createTask(0, "task 10");

	/* add tasks to the dynamic array */
	addHeap(mainList, task1);
	addHeap(mainList, task2);
	addHeap(mainList, task3);
	addHeap(mainList, task4);
	addHeap(mainList, task5);
	addHeap(mainList, task6);
	addHeap(mainList, task7);
	addHeap(mainList, task8);
	addHeap(mainList, task9);
	addHeap(mainList, task10);

#ifdef TESTHEAP
	for(i = 0; i < sizeDynArr(mainList);i++)
		printf("DynArr[%d] = %d\n", i, getDynArr(mainList,i).priority);


	while(!isEmptyDynArr(mainList))
	{
		TYPE v;
		v = getMinHeap(mainList);
		printf("Val = %s ___%d\n", v->description, v->priority);
		removeMinHeap(mainList);
	}
#endif

#ifdef TESTSORT

	printf("Before Sort Called \n");
	for(i = 0; i < sizeDynArr(mainList);i++)
			printf("DynArr[%d] = %d\n", i, ((Task*)getDynArr(mainList,i))->priority);


	/* sort tasks */
	sortHeap(mainList);
	printf("After Sort Called \n");

	/* print sorted tasks from the dynamic array */
	for(i = 0; i < sizeDynArr(mainList);i++)
				printf("DynArr[%d] = %d\n", i, ((Task*)getDynArr(mainList,i))->priority);

	return 0;

#endif
}
Ejemplo n.º 13
0
int main() {
    struct task task1, task2, task3, task4, task5, task6, task7, task8, task9, task10;
	struct DynArr taskList;
	initDynArr(&taskList, 10);

    /* create tasks */
	task1 = createTask(10, "Clean house");
	task2 = createTask(6, "Eat dinner");
	task3 = createTask(2, "Do laundry");
	task4 = createTask(4, "Do dishes");
	task5 = createTask(5, "Grocery shopping");
	task6 = createTask(7, "Change oil");
	task7 = createTask(13, "Do taxes");
	task8 = createTask(8, "Sleep");
	task9 = createTask(1, "Do other homework");
	task10 = createTask(0, "Finish heap homework");

	/* add tasks to the dynamic array */
	addHeap(&taskList, task1);
	addHeap(&taskList, task2);
	addHeap(&taskList, task3);
	addHeap(&taskList, task4);
	addHeap(&taskList, task5);
	addHeap(&taskList, task6);
	addHeap(&taskList, task7);
	addHeap(&taskList, task8);
	addHeap(&taskList, task9);
	addHeap(&taskList, task10);

	printf("Beginning testing... \n");

  /* Should print: heap size: 10 */
  printf("heap size: %d \n", sizeDynArr(&taskList));

  /* Should print - Priority: 0 - Description: Finish heap homework */
  printTask(getMinHeap(&taskList));

  /* Remove top node from head */
	removeMinHeap(&taskList);

  /* Should print - Priority: 1 - Do other homework */
	printTask(getMinHeap(&taskList));

	int i;
	printf("printing heap... \n");
	/* Should print - 1 2 6 4 5 7 13 10 8 9 */
	for(i = 0; i <sizeDynArr(&taskList);i++) {
        printf("%d ", getDynArr(&taskList, i).priority);
	}
	printf("\n");

    /* Sort heap */
	sortHeap(&taskList);

    printf("printing sorted heap... \n");
    /* Should print (without descriptions) : 1, 2, 4, 5, 6, 7, 8, 10, 13 */
		/*Not sure how to both print "Priority: 1 - Do Other homework" like you asked
			above and in this order ascending, using the same function.
			Doesn't really make sense.*/
	for (i = 0; i < taskList.size; i++)	{
	  	printTask(getDynArr(&taskList, i));
	}
    printf("\n");

	return 0;
}