Exemplo n.º 1
0
void demo(int tam_vec, double factor){
    
    Vector<Dni> v = bank_gen(tam_vec);
    
    InsertionSort<Dni> is;
    BubbleSort<Dni> bs;
    ShellSort<Dni> ss(factor);
    QuickSort<Dni> qs;
    MergeSort<Dni> ms;
    
    cout << "Vector: "; v.mostrar();
    cout << " ## InsertionSort: " << endl;
    is.ordenar(v, v.getSize());
    
    cout << "Vector: "; v.mostrar();    
    cout << " ## BubbleSort: " << endl;
    bs.ordenar(v, v.getSize());

    cout << "Vector: "; v.mostrar();
    cout << " ## ShellSort: " << endl;
    ss.ordenar(v, v.getSize());

    cout << "Vector: "; v.mostrar();
    cout << " ## QuickSort: " << endl;
    qs.ordenar(v, v.getSize());
        
    cout << "Vector: "; v.mostrar();
    cout << " ## MergeSort: " << endl;
    ms.ordenar(v, v.getSize());        
}
Exemplo n.º 2
0
int main()
{
	//待排数据输入方式:
		/*int N = 0;
		cout << "排序数据个数:\n";
		cin >> N;
		int* A = new int[N];
		cout << "请输入待排序的数据:\n";
		for (int i = 0; i < N; i++)
		{
		cin >> A[i];
		}*/
	//数据直接给定	
		int B[N] = { 1, 6, 3, 5, 2, 4 };
		int C[13] = { 54, 35, 48, 36, 27, 12, 44, 44, 8, 14, 26, 17, 2 };
		int* A = C;

	//从文件中读取,大量数据,计算时间复杂度
		
	printResult("待排原始数据:", C, N);

	BubbleSort bubble;
	bubble.bubbleSort(A,N);
	printResult("bubbleSort", A, N);

	SelectionSort select;
	select.selectionSort(A, N);
	printResult("selectSort", A, N);

	InsertionSort insert;
	insert.insertionSort(A, N);
	printResult("InsetSort", A, N);

	MergeSort merge;
	merge.mergeSort(A, N);
	printResult("MergeSort", A, N);

	QuickSort qucik;
	qucik.quickSort(A, N);
	printResult("QucikSort",A,N);

	QuickSort2 qucik2;
	qucik2.quickSort(A, N);
	printResult("QucikSort2", A, N);

	HeapSort heap;
	heap.heapSort(A, N);
	printResult("heapSort", A, N);

	HeapSort2 heap2;
	heap2.heapSort(A, N);
	printResult("heapSort2", A, N);


	ShellSort shell;
	shell.shellSort(A,N);
	printResult("shellSort", A, N);

	return 0;
}
Exemplo n.º 3
0
/// Sorts by assigned values.
void SortStrings(List<String> & listToSort, bool debugPrint /*= false*/)
{
	//// Sort the LIST!
	List<String> sorted;
	List<Sortable*> unsortedList, sortedList;

	/// copy pointers of all strings.
	for (int i = 0; i < listToSort.Size(); ++i)
	{
		unsortedList.Add((Sortable*)(&listToSort[i]));
	}

	// Assign them sorting values.
	InsertionSort insertionSort;
	insertionSort.Sort(unsortedList, sortedList, true);
	for (int i = 0; i < sortedList.Size(); ++i)
	{
		String & string = *(String*)sortedList[i];	
		if (debugPrint)
			std::wcout<<"\nSorted list "<<i<<": "<<string;
		sorted.Add(string);
	}
	// Copy list.
	listToSort = sorted;
}
Exemplo n.º 4
0
int TestInsertionSort_IntArray()
{
	int                 int_array [] = {0, 4, 3, 6, 2, 1, 5, 7, 9, 8 };
	InsertionSort<int> *is = new InsertionSort<int>();
	is->Sort(int_array, 10);
	for (int i = 0; i < 10; i++) {
		TEST_ASSERT(int_array[i] == i);
	}

	delete is;
	return 0;
}
Exemplo n.º 5
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.º 6
0
void Tester<T>::linkedInsertionSortTest(double TimeTable[], int& timeCount)
{//연결 삽입 정렬 TEST
	InsertionSort<T> insertion;
	ioHandler io;
	clock_t begin, end;

	begin = clock();
	insertion.linkedInsertionSort(arr,size);
	end =  clock();

	io.setTime(TimeTable,timeCount, ((double)(end - begin))/CLOCKS_PER_SEC);
}
Exemplo n.º 7
0
int main() {

  
  InsertionSort<int, Comp> s;
  NodeSequence<int> n;
  n.insertFirst(0);
  n.insertFirst(1);
  n.insertFirst(-1);
  s.sort(n);

  cout << n;

  return 0;
}
Exemplo n.º 8
0
int main()
{
	int len=7;
	//scanf_s("%d", &len);
	int p[8] = { 6, 12, 4, 63, 2, 35, 11 };
	InsertionSort insert;
	insert.insertionSort(p, len);
	//insert_sort(p, len);
	for (int i = 0; i < len; i++)
	{
		printf("%d,", *(p + i));
	}
	scanf_s("%d", &len);
    return 0;
}
Exemplo n.º 9
0
int main(){

	srand(time(0));

	CsvData csv("results.csv");

	/* STLSort */
	STLSort<int> stlSort;
	cout << "STLSort \n";
	stlSort.meet(10, 100000, csv);

	/* InsertionSort */
	cout << "InsertionSort \n";
	InsertionSort<int> insertionSort;
	insertionSort.meet(10, 100000, csv);

	return 0;
}
Exemplo n.º 10
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.º 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";
	}

}