void InputHandler::calculateSortTime() { Sorter<double> s; time_t b1; s.quickSort(qsa, numDoubles); time_t a1; time_t b2; s.insertionSort(isa, numDoubles); time_t a2; time_t b3; s.oddEvenSort(msa, numDoubles); time_t a3; cout<<endl; cout<<"QuickSort"<<endl; cout<<"Time started: "<<b1<<endl; cout<<"Time ended: "<<a1<<endl; cout<<"Time difference: "<<-(double)difftime(a1, b1)<<endl; cout<<endl; cout<<"Insertion Sort"<<endl; cout<<"Time started: "<<b2<<endl; cout<<"Time ended: "<<a2<<endl; cout<<"Time difference: "<<-(double)difftime(a2, b2)<<endl; cout<<endl; cout<<"Odd Even Sort"<<endl; cout<<"Time started: "<<b3<<endl; cout<<"Time ended: "<<a3<<endl; cout<<"Time difference: "<<-(double)difftime(a3, b3)<<endl; cout<<endl; }
void test0() { std::cout << "test0 " ; Sorter *sorter = new Sorter(); vector<uint32_t> a; vector<uint32_t> b; a.push_back(3); a.push_back(10); a.push_back(20); a.push_back(1); b.push_back(2); b.push_back(1); b.push_back(0); b.push_back(3); sorter->sort(a, b); assert(a[0] == 20); assert(a[1] == 10); assert(a[2] == 3); assert(a[3] == 1); assert(b[0] == 0); assert(b[1] == 1); assert(b[2] == 2); assert(b[3] == 3); delete sorter; }
void MinUnknownUniverseCover::_initializeVariableCover( unordered_map<uint32_t, uint32_t>& inputVariableFrequencyMap, vector<uint32_t>& outputCoverVariables) { //printf("\n initialize\n"); outputCoverVariables.clear(); vector<uint32_t> counts; for (unordered_map<uint32_t, uint32_t>::const_iterator iter = inputVariableFrequencyMap.begin(); iter != inputVariableFrequencyMap.end(); ++iter) { int var = iter->first; int count = iter->second; outputCoverVariables.push_back(var); counts.push_back(count); } //printf("\n sort by frequency %lu variables\n", counts.size()); // sort by frequency Sorter *sorter = new Sorter(); sorter->sort(counts, outputCoverVariables); delete sorter; //printf("\n initialize end\n"); }
int main(int argc, char** argv) { if (argc < 2) { std::cerr << "No arguments provided\n"; return 1; } Sorter sorter; sorter.sort(argv[1], "sorted_index.bin"); }
int main() { Sorter* bubble = new BubbleSort(); bubble->sorting(); delete bubble; Sorter* bead = new PlugSort(); bead->sorting(); delete bead; return 0; }
void SortByDelta() { struct Sorter : public maxon::BaseSort<Sorter> { static inline Bool LessThan(const Connection& a, const Connection& b) { return a.delta < b.delta; } }; Sorter sorter; sorter.Sort(Begin(), End()); }
static Float getMedian(const GeDynamicArray<Vector> &points, const Int32 *pnts, Int32 numPoints, Random &rng, INT axis, Sorter &sorter) { Int32 maxSamples = 20; Int32 numSamples = numPoints < maxSamples ? numPoints : maxSamples; Float* samples = NewMemClear(Float,numSamples); for(Int32 i=0;i<numSamples;i++){ Float val = rng.Get01(); Int32 a = val * (numPoints-1); switch(axis){ case 0: samples[i] = points[pnts[a]].x; break; case 1: samples[i] = points[pnts[a]].y; break; case 2: samples[i] = points[pnts[a]].z; break; } } sorter.Sort(samples,numSamples,sizeof(Float)); Float ret = samples[numSamples/2]; DeleteMem(samples); return ret; }
void doSorter(TaskConfigure& configure, char* rootDirectory) { vector<string> inputFiles; inputFiles.push_back(string(rootDirectory) + "0.par"); inputFiles.push_back(string(rootDirectory) + "1.par"); inputFiles.push_back(string(rootDirectory) + "2.par"); inputFiles.push_back(string(rootDirectory) + "3.par"); inputFiles.push_back(string(rootDirectory) + "4.par"); string outputFile = "sorted.dat"; Sorter *sorter = new Sorter(configure, outputFile, inputFiles); TS_ASSERT(sorter != NULL); TS_ASSERT(sorter->sort() == true); delete sorter; }
void checkSorter(Sorter& sorter, uint32_t count, ...) { va_list args; va_start(args, count); list<string> ordering = sorter.sort(); EXPECT_EQ(ordering.size(), count); foreach (const string& actual, ordering) { const char* expected = va_arg(args, char*); EXPECT_EQ(actual, expected); } va_end(args); }
void test1() { std::cout << "test1 " ; Sorter *sorter = new Sorter(); vector<uint32_t> a; vector<uint32_t> aIndexes; vector<uint32_t> aOriginal; // random tests time_t seed = time(NULL); srandom(seed); unsigned long nIter = 200000; for (unsigned long i = 0; i < nIter; i++) { long r = random(); uint32_t number = (uint32_t)r; // we're only using positive numbers the using code, but testing for +- a.push_back(number); aOriginal.push_back(number); aIndexes.push_back((uint32_t)i); } sorter->sort(a, aIndexes); assert(a.size() == nIter); assert(aIndexes.size() == nIter); for (unsigned long i = 1; i < nIter; i++) { assert(a[i] <= a[i-1]); uint32_t index = aIndexes[i]; assert(aOriginal[index] == a[i]); } delete sorter; }
int main(int argc, char ** argv) { if (argc != 2) { cerr << argv[0] << " <sorting algorithm>" << endl; exit(1); } Sorter<iterator_t> *mysort = sortingFactory<iterator_t>(string(argv[1])); cout << "Please insert your sequence of numbers (stop the sequence with Ctrl+D):" << endl; vector<T> foo; copy(istream_iterator<T>(cin), istream_iterator<T>(), back_inserter(foo)); double time = mysort->sort(foo.begin(), foo.end()); cout << "time used to sort: " << time << "sec" << endl; cout << endl << "The sorted sequence is:" << endl; copy(foo.begin(), foo.end(), ostream_iterator<T>(cout, "\n")); cout << endl; delete mysort; }
int main() { int myints[] = {16,277,3,-2,24,-54,-1,0,56,87,7,-7}; vector<int> items (myints, myints + sizeof(myints) / sizeof(int)); const string sortedStr = "-54, -7, -2, -1, 0, 3, 7, 16, 24, 56, 87, 277"; int myints2[] = {0,4,6,1,9,3,6,2,8,4,3,1}; vector<int> items2 (myints2, myints2 + sizeof(myints2) / sizeof(int)); const string sortedStr2 = "0, 1, 1, 2, 3, 3, 4, 4, 6, 6, 8, 9"; int myints3[] = {0,4,2391,72,111,3,6,72,2,85,44,3,991,1}; vector<int> items3 (myints3, myints3 + sizeof(myints3) / sizeof(int)); const string sortedStr3 = "0, 1, 2, 3, 3, 4, 6, 44, 72, 72, 85, 111, 991, 2391"; cout << "Input: " << vector2string(items) << endl; cout << "Input: " << vector2string(items2) << endl; Sorter sorter; vector<int> sortedItems(items); sorter.insertionSort(sortedItems); assertAndPrint(sortedItems, sortedStr, "insertionSort"); sortedItems = items; sorter.selectionSort(sortedItems); assertAndPrint(sortedItems, sortedStr, "selectionSort"); sortedItems = items; sorter.bubbleSort(sortedItems); assertAndPrint(sortedItems, sortedStr, "bubbleSort"); sortedItems = items; sorter.mergeSort(sortedItems); assertAndPrint(sortedItems, sortedStr, "mergeSort"); sortedItems = items; sorter.quickSort(sortedItems); assertAndPrint(sortedItems, sortedStr, "quickSort"); sortedItems = items2; sorter.countingSort(sortedItems); assertAndPrint(sortedItems, sortedStr2, "countingSort"); sortedItems = items3; sorter.radixSort(sortedItems); assertAndPrint(sortedItems, sortedStr3, "radixSort"); }
int DiceRoll::GetAtkArmies(int A) { do { cout << "How Many Units is the Attacker using?: "; do { cout << "How Many Units is the Attacker using?: "; atk::roll = get_int(); if(atk::roll == INT_MIN) { \cout << "You did not input a number, try again." << endl; } else if (atk::roll >= A) { if ((atk::roll - A) <= 0) { cout << "Sorry, there needs to be at least one left. Try again." << endl; continue; } else { cout << "Sorry, you do not have enough. Try Again " << endl; continue; } } else break; } while (true); return atk::roll; } int DiceRoll::Roll(int atk, int def) { vector<int> aroll; vector<int> droll; srand (time(NULL)); for (size_t i = 0; i < atk; ++i) aroll.push_back ((rand() % 6)+1); cout << "Attacker Rolls: " << aroll << endl; for (size_t i = 0; i < def; ++i) droll.push_back ((rand() % 6)+1); cout << "Defenders Rolls: " << droll << endl; return mySorter.asorter(aroll, droll); }
int main() { SorterTest test; QTest::qExec(&test); cout << "Welcome to the program of sorting of array\n"; cout << "Enter size of array "; int size = 0; cin >> size; cout << "Enter array "; const int maxSize = 100; int array1[maxSize]; int array2[maxSize]; int array3[maxSize]; for (int i = 0; i < size; ++i) { cin >> array1[i]; array2[i] = array1[i]; array3[i] = array1[i]; } Sorter *bubbleSort = new BubbleSort(); Sorter *quickSort = new QuickSort(); Sorter *heapSort = new HeapSort(); bubbleSort->sort(array1, size); quickSort->sort(array2, size); heapSort->sort(array3, size); cout << "Sorted by bubble\n"; for (int i = 0; i < size; ++i) { cout << array1[i] << ' '; } cout << '\n'; cout << "Sorted by quicksort\n"; for (int i = 0; i < size; ++i) { cout << array2[i] << ' '; } cout << '\n'; cout << "Sorted by heapsort\n"; for (int i = 0; i < size; ++i) { cout << array3[i] << ' '; } delete bubbleSort; delete quickSort; delete heapSort; return 0; }
int main(int argc, char *argv[]) { char *filename, *ndisp_str, *fdata; unsigned int fd, ndisp; struct stat finfo; if (argv[1] == NULL) { printf("USAGE: %s <filename> <# results>\n", argv[0]); exit(1); } // Initialize some variables filename = argv[1]; ndisp_str = argv[2]; CHECK_ERROR((ndisp = ((ndisp_str == NULL) ? DEFAULT_DISP_NUM : atoi(ndisp_str))) <= 0); printf("Sequential Sort: Running...\n"); // Open the file CHECK_ERROR((fd = open(filename, O_RDONLY)) < 0); CHECK_ERROR(fstat(fd, &finfo) < 0); #ifndef NO_MMAP // Memory map the file printf("Memory mapping the file (with MMAP_POPULATE)\n"); CHECK_ERROR((fdata = (char*) mmap(0, finfo.st_size + 1, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_POPULATE, fd, 0)) == NULL); #else uint64_t r = 0; printf("Mallocing the file\n"); fdata = (char *)malloc (finfo.st_size); CHECK_ERROR(fdata == NULL); while(r < (uint64_t)finfo.st_size) r += pread (fd, fdata + r, finfo.st_size, r); CHECK_ERROR(r != (uint64_t) finfo.st_size); #endif // Prepare splitter arguments Sorter *sort = new Sorter(fdata, finfo.st_size); // Divide file on a word border (i.e. a space) printf("Sequential Sort: Calling word count\n"); sort->splitter(); // Sort the results sort->sort(); printf("Sequential Sort: Completed\n"); // Print out results sort->print_results(ndisp); // Cleanup delete sort; #ifndef NO_MMAP CHECK_ERROR(munmap(fdata, finfo.st_size + 1) < 0); #else free (fdata); #endif CHECK_ERROR(close(fd)); return 0; }
void test2() { std::cout << "test2 " ; Sorter *sorter = new Sorter(); unsigned long len = 6; float* a = (float *)malloc(len * sizeof(float)); float* b = (float *)malloc(len * sizeof(float)); float* c = (float *)malloc(len * sizeof(float)); /* 4.000000e+00 9.499999e-01 3.500000e-01 2.000000e+00 5.500000e-01 6.500000e-01 1.500000e-01 4.000000e-01 3.500000e-01 3.000000e-01 4.000000e-01 3.500000e-01 4.000000e+00 9.499999e-01 5.500000e-01 3.000000e-01 3.000000e-01 3.500000e-01 */ a[0] = 4.000000e+00; b[0] = 9.499999e-01; c[0] = 3.500000e-01; a[1] = 2.000000e+00; b[1] = 5.500000e-01; c[1] = 6.500000e-01; a[2] = 1.500000e-01; b[2] = 4.000000e-01; c[2] = 3.500000e-01; a[3] = 3.000000e-01; b[3] = 4.000000e-01; c[3] = 3.500000e-01; a[4] = 4.000000e+00; b[4] = 9.499999e-01; c[4] = 5.500000e-01; a[5] = 3.000000e-01; b[5] = 3.000000e-01; c[5] = 3.000000e-01; sorter->sort(a, b, c, len); /* 1.500000e-01 4.000000e-01 3.500000e-01 3.000000e-01 3.000000e-01 3.500000e-01 3.000000e-01 4.000000e-01 3.500000e-01 2.000000e+00 5.500000e-01 6.500000e-01 4.000000e+00 9.499999e-01 3.500000e-01 4.000000e+00 9.499999e-01 5.500000e-01 */ float* ea = (float *)malloc(len * sizeof(float)); float* eb = (float *)malloc(len * sizeof(float)); float* ec = (float *)malloc(len * sizeof(float)); ea[0] = 1.500000e-01; eb[0] = 4.000000e-01; ec[0] = 3.500000e-01; ea[1] = 3.000000e-01; eb[1] = 3.000000e-01; ec[1] = 3.500000e-01; ea[2] = 3.000000e-01; eb[2] = 4.000000e-01; ec[2] = 3.500000e-01; ea[3] = 2.000000e+00; eb[3] = 5.500000e-01; ec[3] = 6.500000e-01; ea[4] = 4.000000e+00; eb[4] = 9.499999e-01; ec[4] = 3.500000e-01; ea[5] = 4.000000e+00; eb[5] = 9.499999e-01; ec[5] = 5.500000e-01; //printf("\n"); for (unsigned long i = 0; i < len; i++) { float diff = a[i] - ea[i]; if (diff < 0) { diff *= -1; } float eps = 0.01*ea[i]; if (eps < 0) { eps *= -1; } //printf("%e %e %e\n", a[i], b[i], c[i]); assert(diff < eps); } free(a); free(b); free(c); free(ea); free(eb); free(ec); delete sorter; }
bool operator()(const void *a, const void *b) { return srt->isLess(a,b); }