Exemplo n.º 1
0
int main()
{
        MergeSort sorter;

        sorter.readList();
        sorter.sort();
        cout << "Output:\n";
        sorter.printArray();
	return 0;
}
Exemplo n.º 2
0
void Interface::run() {
	int option;
	string filename, userInput;

	while (_active) {
		string filename;
		vector<int> data;

		cout << "\n Choose your data  \n 1. Manual \n 2. Auto Random \n 3. Load CSV file \n 4. Exit" << endl;
		getline(cin, userInput);
		option = atoi(userInput.c_str());

		switch (option) {
			case 1:
				_arrSize = requestArraySize();
				data = manualEntries(_arrSize);
				break;
			case 2:
				_arrSize = requestArraySize();
				data = automaticEntries(_arrSize);
				break;
			case 3:
				cout << "Please type file name " << endl;
				getline(cin, filename);
				data = CSVEntries(filename);
				break;
			case 4:
				_active = false;
				break;
			default:
				cout << "Invalid option. Please start again. \n" << endl;
				break;
		}

		if(data.size() > 0) {
			DataCollection store(_arrSize, data);
			cout << "\n print Unsorted List:" << endl;
			store.printElements();
		
			MergeSort mergeSort;
			mergeSort.sort(store.getDataArray());

			InsertionSort insertionSort;
			insertionSort.sort(store.getDataArray() );

			cout << "\n Merge sort finished in: "<< mergeSort.getExecutionTime() <<" nanosecond " << endl;
			mergeSort.printElements();

			cout << "\n Insertion Sort finished in: " << insertionSort.getExecutionTime() <<" nanoseconds"<< endl;
			insertionSort.printElements();
		}
	}
}
Exemplo n.º 3
0
void sortData()
{
		cout << "\n SORTING...\n" << endl;
		MergeSort sortVector;
		//cout << "Unsorted vector:" << endl;
		sortVector.displayElements(); // print unsorted vector
		//cout << endl << endl;
		sortVector.sort(); // sort vector
		//cout << "Sorted vector:" << endl;
		sortVector.displayElements(); // print sorted vector
		cout << endl;
	if(verbose)
	cout << "Done with sorting! " << endl;
}
Exemplo n.º 4
0
int FsmAp::partitionRound( StateAp **statePtrs, MinPartition *parts, int numParts )
{
	/* Need a mergesort object and a single partition compare. */
	MergeSort<StateAp*, PartitionCompare> mergeSort;
	PartitionCompare partCompare;

	/* For each partition. */
	for ( int p = 0; p < numParts; p++ ) {
		/* Fill the pointer array with the states in the partition. */
		StateList::Iter state = parts[p].list;
		for ( int s = 0; state.lte(); state++, s++ )
			statePtrs[s] = state;

		/* Sort the states using the partitioning compare. */
		int numStates = parts[p].list.length();
		mergeSort.sort( statePtrs, numStates );

		/* Assign the states into partitions based on the results of the sort. */
		int destPart = p, firstNewPart = numParts;
		for ( int s = 1; s < numStates; s++ ) {
			/* If this state differs from the last then move to the next partition. */
			if ( partCompare.compare( statePtrs[s-1], statePtrs[s] ) < 0 ) {
				/* The new partition is the next avail spot. */
				destPart = numParts;
				numParts += 1;
			}

			/* If the state is not staying in the first partition, then
			 * transfer it to its destination partition. */
			if ( destPart != p ) {
				StateAp *state = parts[p].list.detach( statePtrs[s] );
				parts[destPart].list.append( state );
			}
		}

		/* Fix the partition pointer for all the states that got moved to a new
		 * partition. This must be done after the states are transfered so the
		 * result of the sort is not altered. */
		for ( int newPart = firstNewPart; newPart < numParts; newPart++ ) {
			StateList::Iter state = parts[newPart].list;
			for ( ; state.lte(); state++ )
				state->alg.partition = &parts[newPart];
		}
	}

	return numParts;
}
Exemplo n.º 5
0
void RedFsmAp::sortByStateId()
{
	/* Make the array. */
	int pos = 0;
	RedStateAp **ptrList = new RedStateAp*[stateList.length()];
	for ( RedStateList::Iter st = stateList; st.lte(); st++, pos++ )
		ptrList[pos] = st;
	
	MergeSort<RedStateAp*, CmpStateById> mergeSort;
	mergeSort.sort( ptrList, stateList.length() );

	stateList.abandon();
	for ( int st = 0; st < pos; st++ )
		stateList.append( ptrList[st] );

	delete[] ptrList;
}
Exemplo n.º 6
0
bool FsmAp::minimizeRound()
{
	/* Nothing to do if there are no states. */
	if ( stateList.length() == 0 )
		return false;

	/* Need a mergesort on approx compare and an approx compare. */
	MergeSort<StateAp*, ApproxCompare> mergeSort;
	ApproxCompare approxCompare;

	/* Fill up an array of pointers to the states. */
	StateAp **statePtrs = new StateAp*[stateList.length()];
	StateList::Iter state = stateList;
	for ( int s = 0; state.lte(); state++, s++ )
		statePtrs[s] = state;

	bool modified = false;

	/* Sort The list. */
	mergeSort.sort( statePtrs, stateList.length() );

	/* Walk the list looking for duplicates next to each other, 
	 * merge in any duplicates. */
	StateAp **pLast = statePtrs;
	StateAp **pState = statePtrs + 1;
	for ( int i = 1; i < stateList.length(); i++, pState++ ) {
		if ( approxCompare.compare( *pLast, *pState ) == 0 ) {
			/* Last and pState are the same, so fuse together. Move forward
			 * with pState but not with pLast. If any more are identical, we
			 * must */
			fuseEquivStates( *pLast, *pState );
			modified = true;
		}
		else {
			/* Last and this are different, do not set to merge them. Move
			 * pLast to the current (it may be way behind from merging many
			 * states) and pState forward one to consider the next pair. */
			pLast = pState;
		}
	}
	delete[] statePtrs;
	return modified;
}
Exemplo n.º 7
0
void estadistica(int n_pruebas, int tam_vec, double factor){
    //Instanciacion del banco de pruebas
    Vector<Dni>* bank_pruebas = new Vector<Dni>[n_pruebas];
    srand(time(NULL));
    
    //Instanciar algoritmos
    InsertionSort<Dni> is;
    BubbleSort<Dni> bs;
    ShellSort<Dni> ss(factor);
    QuickSort<Dni> qs;
    MergeSort<Dni> ms;
    
     //Aplicar algoritmos al banco de pruebas
    for(int i = 0; i < n_pruebas; i++){
        bank_pruebas[i] = bank_gen(tam_vec);
        cout << "Vector " << i << " : "; bank_pruebas[i].mostrar();
         
        cout << " ## InsertionSort: " << endl;
        is.ordenar(bank_pruebas[i], bank_pruebas[i].getSize());
        
        cout << "Vector : "; bank_pruebas[i].mostrar();
        cout << " ## BubbleSort: " << endl;
        bs.ordenar(bank_pruebas[i], bank_pruebas[i].getSize());
        
        cout << "Vector: " ; bank_pruebas[i].mostrar();
        cout << " ## ShellSort: " << endl;
        ss.ordenar(bank_pruebas[i], bank_pruebas[i].getSize());

        cout << "Vector: " ; bank_pruebas[i].mostrar();
        cout << " ## QuickSort: " << endl;
        qs.ordenar(bank_pruebas[i], bank_pruebas[i].getSize());
        
        cout << "Vector: " ; bank_pruebas[i].mostrar();
        cout << " ## MergeSort: " << endl;
        ms.ordenar(bank_pruebas[i], bank_pruebas[i].getSize());
        
        cout << "~~~~~~" << endl;
    }
        
    bs.setAvg( (double)bs.getTot() / n_pruebas);
    is.setAvg( (double)is.getTot() / n_pruebas);
    ss.setAvg( (double)ss.getTot() / n_pruebas);
    qs.setAvg( (double)qs.getTot() / n_pruebas);
    ms.setAvg( (double)ms.getTot() / n_pruebas);

    cout << endl;
    cout << "Estadisticas" << endl;
    cout << "Algoritmo     " << "    min " << "    avg " << "    max " << " total" << endl;
    cout << setprecision(5);
    cout << "InsertionSort   " << is.getMin() << "    " << is.getAvg() << "    " << is.getMax() << "     " << is.getTot() << endl;
    cout << "BubbleSort      " << bs.getMin() << "    " << bs.getAvg() << "    " << bs.getMax() << "     " << bs.getTot() << endl;
    cout << "ShellSort       " << ss.getMin() << "    " << ss.getAvg() << "    " << ss.getMax() << "     " << ss.getTot() << endl;
    cout << "QuickSort       " << qs.getMin() << "    " << qs.getAvg() << "    " << qs.getMax() << "     " << qs.getTot() << endl;
    cout << "MergeSort       " << ms.getMin() << "    " << ms.getAvg() << "    " << ms.getMax() << "     " << ms.getTot() << endl;

}
Exemplo n.º 8
0
int main()
{
    vector<int> testint, testint2;
    testint.push_back(1);
    testint.push_back(5);
    testint.push_back(0);
    testint.push_back(2);
    testint.push_back(2);
    testint.push_back(8);
    testint.push_back(6);
    testint.push_back(9);

    MergeSort <int> sort;
    testint2 = sort.solve(testint);

    for (int i = 0; i < testint2.size(); i++)
    {
        cout << testint2[i] << " " << endl;
    }

    vector<char> testch, testch2;
    testch.push_back('f');
    testch.push_back('h');
    testch.push_back('a');
    testch.push_back('b');
    testch.push_back('u');
    testch.push_back('p');
    testch.push_back('e');
    testch.push_back('z');

    MergeSort <char> sort2;
    testch2 = sort2.solve(testch);

    for (int i = 0; i < testch2.size(); i++)
    {
        cout << testch2[i] << " " << endl;
    }

    return 0;
}
Exemplo n.º 9
0
/**
 * \brief Minimize by partitioning version 1.
 *
 * Repeatedly tries to split partitions until all partitions are unsplittable.
 * Produces the most minimal FSM possible.
 */
void FsmAp::minimizePartition1()
{
	/* Need one mergesort object and partition compares. */
	MergeSort<StateAp*, InitPartitionCompare> mergeSort;
	InitPartitionCompare initPartCompare;

	/* Nothing to do if there are no states. */
	if ( stateList.length() == 0 )
		return;

	/* 
	 * First thing is to partition the states by final state status and
	 * transition functions. This gives us an initial partitioning to work
	 * with.
	 */

	/* Make a array of pointers to states. */
	int numStates = stateList.length();
	StateAp** statePtrs = new StateAp*[numStates];

	/* Fill up an array of pointers to the states for easy sorting. */
	StateList::Iter state = stateList;
	for ( int s = 0; state.lte(); state++, s++ )
		statePtrs[s] = state;
		
	/* Sort the states using the array of states. */
	mergeSort.sort( statePtrs, numStates );

	/* An array of lists of states is used to partition the states. */
	MinPartition *parts = new MinPartition[numStates];

	/* Assign the states into partitions. */
	int destPart = 0;
	for ( int s = 0; s < numStates; s++ ) {
		/* If this state differs from the last then move to the next partition. */
		if ( s > 0 && initPartCompare.compare( statePtrs[s-1], statePtrs[s] ) < 0 ) {
			/* Move to the next partition. */
			destPart += 1;
		}

		/* Put the state into its partition. */
		statePtrs[s]->alg.partition = &parts[destPart];
		parts[destPart].list.append( statePtrs[s] );
	}

	/* We just moved all the states from the main list into partitions without
	 * taking them off the main list. So clean up the main list now. */
	stateList.abandon();

	/* Split partitions. */
	int numParts = destPart + 1;
	while ( true ) {
		/* Test all partitions for splitting. */
		int newNum = partitionRound( statePtrs, parts, numParts );

		/* When no partitions can be split, stop. */
		if ( newNum == numParts )
			break;

		numParts = newNum;
	}

	/* Fuse states in the same partition. The states will end up back on the
	 * main list. */
	fusePartitions( parts, numParts );

	/* Cleanup. */
	delete[] statePtrs;
	delete[] parts;
}
Exemplo n.º 10
0
/* Split partitions that need splittting, decide which partitions might need
 * to be split as a result, continue until there are no more that might need
 * to be split. */
int FsmAp::splitCandidates( StateAp **statePtrs, MinPartition *parts, int numParts )
{
	/* Need a mergesort and a partition compare. */
	MergeSort<StateAp*, PartitionCompare> mergeSort;
	PartitionCompare partCompare;

	/* The lists of unsplitable (partList) and splitable partitions. 
	 * Only partitions in the splitable list are check for needing splitting. */
	PartitionList partList, splittable;

	/* Initially, all partitions are born from a split (the initial
	 * partitioning) and can cause other partitions to be split. So any
	 * partition with a state with a transition out to another partition is a
	 * candidate for splitting. This will make every partition except possibly
	 * partitions of final states split candidates. */
	for ( int p = 0; p < numParts; p++ ) {
		/* Assume not active. */
		parts[p].active = false;

		/* Look for a trans out of any state in the partition. */
		for ( StateList::Iter state = parts[p].list; state.lte(); state++ ) {
			/* If there is at least one transition out to another state then 
			 * the partition becomes splittable. */
			if ( state->outList.length() > 0 ) {
				parts[p].active = true;
				break;
			}
		}

		/* If it was found active then it goes on the splittable list. */
		if ( parts[p].active )
			splittable.append( &parts[p] );
		else
			partList.append( &parts[p] );
	}

	/* While there are partitions that are splittable, pull one off and try
	 * to split it. If it splits, determine which partitions may now be split
	 * as a result of the newly split partition. */
	while ( splittable.length() > 0 ) {
		MinPartition *partition = splittable.detachFirst();

		/* Fill the pointer array with the states in the partition. */
		StateList::Iter state = partition->list;
		for ( int s = 0; state.lte(); state++, s++ )
			statePtrs[s] = state;

		/* Sort the states using the partitioning compare. */
		int numStates = partition->list.length();
		mergeSort.sort( statePtrs, numStates );

		/* Assign the states into partitions based on the results of the sort. */
		MinPartition *destPart = partition;
		int firstNewPart = numParts;
		for ( int s = 1; s < numStates; s++ ) {
			/* If this state differs from the last then move to the next partition. */
			if ( partCompare.compare( statePtrs[s-1], statePtrs[s] ) < 0 ) {
				/* The new partition is the next avail spot. */
				destPart = &parts[numParts];
				numParts += 1;
			}

			/* If the state is not staying in the first partition, then
			 * transfer it to its destination partition. */
			if ( destPart != partition ) {
				StateAp *state = partition->list.detach( statePtrs[s] );
				destPart->list.append( state );
			}
		}

		/* Fix the partition pointer for all the states that got moved to a new
		 * partition. This must be done after the states are transfered so the
		 * result of the sort is not altered. */
		int newPart;
		for ( newPart = firstNewPart; newPart < numParts; newPart++ ) {
			StateList::Iter state = parts[newPart].list;
			for ( ; state.lte(); state++ )
				state->alg.partition = &parts[newPart];
		}

		/* Put the partition we just split and any new partitions that came out
		 * of the split onto the inactive list. */
		partition->active = false;
		partList.append( partition );
		for ( newPart = firstNewPart; newPart < numParts; newPart++ ) {
			parts[newPart].active = false;
			partList.append( &parts[newPart] );
		}

		if ( destPart == partition )
			continue;

		/* Now determine which partitions are splittable as a result of
		 * splitting partition by walking the in lists of the states in
		 * partitions that got split. Partition is the faked first item in the
		 * loop. */
		MinPartition *causalPart = partition;
		newPart = firstNewPart - 1;
		while ( newPart < numParts ) {
			/* Loop all states in the causal partition. */
			StateList::Iter state = causalPart->list;
			for ( ; state.lte(); state++ ) {
				/* Walk all transition into the state and put the partition
				 * that the from state is in onto the splittable list. */
				for ( TransInList::Iter trans = state->inList; trans.lte(); trans++ ) {
					MinPartition *fromPart = trans->fromState->alg.partition;
					if ( ! fromPart->active ) {
						fromPart->active = true;
						partList.detach( fromPart );
						splittable.append( fromPart );
					}
				}
			}

			newPart += 1;
			causalPart = &parts[newPart];
		}
	}
	return numParts;
}
Exemplo n.º 11
0
void TestSortingAlgos() {

	try
	{
		BubbleSort testBObj;
		int arrB[ARRAY_SIZE] = { 15,3,12,10,1,9,6,11,5,4 };

		testBObj.LoadData(arrB, ARRAY_SIZE);
		testBObj.Print();
		testBObj.Sort();

		cout << "Bubble Sort Output:";
		testBObj.Print();

		SelectionSort testSObj;
		int arrS[ARRAY_SIZE] = { 15,3,12,10,1,9,6,11,5,4 };

		testSObj.LoadData(arrS, ARRAY_SIZE);
		testSObj.Print();
		testSObj.Sort();

		cout << "Selection Sort Output:";
		testSObj.Print();


		InsertionSort testIObj;
		int arrI[ARRAY_SIZE] = { 15,3,12,10,1,9,6,11,5,4 };

		testIObj.LoadData(arrI, ARRAY_SIZE);
		testIObj.Print();
		testIObj.Sort();

		cout << "Insertion Sort Output:";
		testIObj.Print();


		MergeSort testMObj;

		int arrM[ARRAY_SIZE] = { 15,3,12,10,1,9,6,11,5,4 };

		testMObj.LoadData(arrM, ARRAY_SIZE);
		testMObj.Print();
		testMObj.Sort();

		cout << "Insertion Sort Output:";
		testMObj.Print();


		QuickSort testQObj;

		int arrQ[] = { 15,3,12,10,1,9,6,11,5,4, 12, 8,1, -23, 87,45, 12, 423 };

		testQObj.LoadData(arrQ, sizeof(arrQ)/sizeof(int));
		testQObj.Print();
		testQObj.Sort();

		cout << "Quick Sort Output:";
		testQObj.Print();



		HeapSort testHObj;

		int arrH[] = { 15,3,12,10,1,9,6,11,5,4, 12, 8,1, -23, 87,45, 12, 423 };

		testHObj.LoadData(arrH, sizeof(arrH) / sizeof(int));
		testHObj.Print();
		testHObj.Sort();

		cout << "Heap Sort Output:";
		testHObj.Print();


		QuickSortRandomized  testQRObj;

		int arrQR[] = { 15,3,12,10,1,9,6,11,5,4, 12, 8,1, -23, 87,45, 12, 423 };

		testQRObj.LoadData(arrQR, sizeof(arrQR) / sizeof(int));
		testQRObj.Print();
		testQRObj.Sort();

		cout << "Heap Sort Output:";
		testQRObj.Print();

		HeapSortRevised  testHSRObj;

		int arrHSR[] = { 15,3,12,10,1,9,6,11,5,4, 12, 8,1, -23, 87,45, 12, 423 };

		testHSRObj.LoadData(arrHSR, sizeof(arrHSR) / sizeof(int));
		testHSRObj.Print();
		testHSRObj.Sort();

		cout << "Heap Sort Output:";
		testHSRObj.Print();

	}
	catch (const std::exception& E)
	{
		cerr << "Caught exception \"" << E.what() << "\"\n";
	}
	catch (...)
	{
		cerr << "Caught unknown exception";
	}

}
Exemplo n.º 12
0
    void testCase4() {
        int numbers_[] = {-2000000000, 2000000000, 0, 0, 0, -2000000000, 2000000000, 0, 0, 0};
        vector<int> numbers(numbers_, numbers_ + (sizeof(numbers_) / sizeof(numbers_[0])));
		int expected_ = 19;
        assertEquals(4, expected_, solution.howManyComparisons(numbers));
    }
Exemplo n.º 13
0
    void testCase3() {
        vector<int> numbers;
		int expected_ = 0;
        assertEquals(3, expected_, solution.howManyComparisons(numbers));
    }
Exemplo n.º 14
0
    void testCase2() {
        int numbers_[] = {-17};
        vector<int> numbers(numbers_, numbers_ + (sizeof(numbers_) / sizeof(numbers_[0])));
		int expected_ = 0;
        assertEquals(2, expected_, solution.howManyComparisons(numbers));
    }
Exemplo n.º 15
0
    void testCase0() {
        int numbers_[] = {1, 2, 3, 4};
        vector<int> numbers(numbers_, numbers_ + (sizeof(numbers_) / sizeof(numbers_[0])));
		int expected_ = 4;
        assertEquals(0, expected_, solution.howManyComparisons(numbers));
    }