int main() { const int ARRAY_SIZE = 10; int testArray[ARRAY_SIZE]; // int *testArray2; BubbleSort<int> bsobj; srand((unsigned int)time(0)); for (int i = 0; i < ARRAY_SIZE; i++) { testArray[i] = 1 + rand() % 50; cout << testArray[i] << endl; } vector<int> testVect(testArray, testArray + ARRAY_SIZE); vector<int> testVect2(testVect.size()); cout << "-------------END OF UNSORTED ARRAY-----------------" << endl << endl; testVect2 = bsobj.bubbleSort(testVect); for (size_t i = 0; i < testVect.size(); i++) { cout << testVect2[i] << endl; } cin.get(); return 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()); }
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; }
int main(int argc, char* argv[]) { //initialize random generator srand(time(NULL)); BubbleSort<double> BubbleSorter; std::cout << "# of elements\tbubble sort time\tmerge sort time" << std::endl; for(int N = 10; N < 100001; N*=10) { //Create random arrays MergeSort<double>* MergeSorter = new MergeSort<double>(); double randBubbleSort[N]; double randMergeSort[N]; for(int i = 0; i<N; ++i) { randBubbleSort[i] = drand48(); randMergeSort[i] = randBubbleSort[i]; } //time bubble sort unsigned long bubStart = getTime(); BubbleSorter.sort(randBubbleSort,N); unsigned long bubTotal = getTime() - bubStart; //time merge sort unsigned long merStart = getTime(); MergeSorter->sort(randMergeSort,N); unsigned long merTotal = getTime() - merStart; //deletes object off heap to clear private members delete MergeSorter; std::cout << N << "\t\t" << bubTotal << "\t\t\t" << merTotal << std::endl; } return 0; }
/** * Calcula o tempo de ordenação com a função Clock. */ double ordenaVetorClock(int algorithm, int size) { clock_t clock_init; // Algoritmos de busca QuickSort qs = QuickSort(); BubbleSort bs = BubbleSort(); MergeSort ms = MergeSort(); // Escolhe o algoritmo que deve ser testado switch (algorithm) { case QUICK_SORT: clock_init = clock(); qs.start(vetorQuickSort, size); return clock() - clock_init; case BUBBLE_SORT: clock_init = clock(); bs.start(vetorBubbleSort, size); return clock() - clock_init; case MERGE_SORT: clock_init = clock(); ms.start(vetorMergeSort, size); return clock() - clock_init; } return -1; }
/** * Calcula o tempo de ordenação com a função Clock. */ double ordenaVetorTime(int algorithm, int size){ time_t time_init; // Algoritmos de busca QuickSort qs = QuickSort(); BubbleSort bs = BubbleSort(); MergeSort ms = MergeSort(); // Escolhe o algoritmo que deve ser testado switch (algorithm) { case QUICK_SORT: time(&time_init); qs.start(vetorQuickSort, size); return time(NULL) - time_init; case BUBBLE_SORT: time_init = time(NULL); bs.start(vetorBubbleSort, size); return time(NULL) - time_init; case MERGE_SORT: time_init = time(NULL); ms.start(vetorMergeSort, size); return time(NULL) - time_init; default: cout << "ERROR" << endl; break; } return -1; }
int main() { int arr[]= {2,4,7,9,8,6}; BubbleSort a; a.bubbleSort(arr,6); for(int i=0; i<6; i++) cout<<arr[i]<<" "; cout<<endl; return 0; }
int main(int argc, const char * argv[]) { int A[] = {5,1,2,3,4}; BubbleSort bu; bu.sortAlg(A, 5); for(int i=0; i< 5; i++){ cout << A[i] << " "; } cout << endl; return 0; }
int main(int argc, const char * argv[]) { // insert code here... std::cout << "Hello, World!\n"; testInsertionSort(); testInsertionSortNonIncreasing(); testMergeSort(); insertionSortRecursion a; a.test(); twoSum ts; ts.test(); MergesortWithInsertionSort mwis; mwis.test(); BubbleSort bs; bs.test(); return 0; }
int * MergeSort::ExecuteMergeSort(int array[], int arraySize){ BubbleSort mBubbleSort; // Split array in half, int pivot = (arraySize/2); int leftArray[pivot]; int rightArray[arraySize-pivot]; for(int i=0; i<pivot; i++){ leftArray[i] = array[i]; } for(int i=0; i<arraySize-pivot; i++){ rightArray[i] = array[i+pivot]; } // Sort two halves mBubbleSort.ExecuteBubbleSort(leftArray, pivot); mBubbleSort.ExecuteBubbleSort(rightArray, arraySize - pivot); for(int i=0; i<pivot; i++){ cout << leftArray[i] << endl; } for(int i=0; i<arraySize - pivot; i++){ cout << rightArray[i] << endl; } int j = 0, k = 0; // merge halves together for(int i=0; i<arraySize; i++){ if(j < pivot && (k > arraySize-pivot || leftArray[j] <= rightArray[k])){ array[i] = leftArray[j]; j++; } else if(k < arraySize-pivot && (j > pivot || rightArray[k] < leftArray[j])){ array[i] = rightArray[k]; k++; } cout << array[i] << endl; } return array; }
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; }
int main(int argc, const char * argv[]) { if ( argc > 3){ for (int i = 1; i <= argc; i++){ if (strcmp( argv[i], "--help") == 0 || strcmp( argv[i], "-h") == 0){ PrintHelp(); return 1; } BubbleSort *bubble = new BubbleSort(argv[1], argv[2], argv[3], argv[4]); bubble->ParallelSorting(); bubble->Plot(); return 0; } } else { cout << "Arguments incorrect" << endl; PrintHelp(); return 1; } }
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"; } }