TEST_F(SortingTest, ShouldReturnAscendingSortedIfAlreadyDescending) { Sorting sorting; vector<int> sorted = sorting.selectionSort(desAges); ASSERT_EQ(10, sorted.front()); ASSERT_EQ(30, sorted.back()); }
TEST_F(SortingTest, ShouldReturnTheSameOrderIfAlreadySorted) { Sorting sorting; vector<int> sorted = sorting.selectionSort(ascAges); ASSERT_EQ(sorted.front(), ascAges.front()); ASSERT_EQ(sorted.back(), ascAges.back()); }
TEST_F(SortingTest, ShouldReturnSortedIfNotSorted) { Sorting sorting; vector<int> sorted = sorting.selectionSort(randAges); ASSERT_EQ(10, sorted.front()); ASSERT_EQ(30, sorted.back()); }
int main() { int length; int *array; cout << "Enter the length of the array" << endl; cin >> length; array = new int[length]; cout << "Enter the values of the array" << endl; for(int i = 0; i< length ; i++) { cin >> array[i]; } Sorting objSort; //Selection Sort // objSort.selectionSort(array, length); //Bubble Sort // objSort.bubbleSort(array, length); //Insertion Sort //objSort.insertionSort(array, length); //QuickSort //objSort.quickSort(array,0,length-1); //ShellSort // objSort.shellSort(array, length); //MergeSort objSort.mergeSort(array, 0, length-1); delete[] array; return 0; }
//closest to zero sum void minAbsSumPair(int A[], int n) { /* Array should have at least two elements*/ if(n < 2) { cout<<"Invalid Input"; return; } Sorting *obj = &Sorting(); obj->SetArray(A, n); obj->InsertionSort(); // left and right index variables int l = 0, r = n-1; int sum,minsum=A[r]; int min_l,min_r; while(l<r) { sum=A[l]+A[r]; if(abs (minsum) > abs(sum)) { minsum=sum; min_l = l; min_r = r; } if(sum >= 0) r--; else l++; } cout<<"min sum is "<<A[min_l]<< " + " <<A[min_r]<<" = "<< minsum; }
void testSorting(int argc, char **argv) { Sorting t; vector<int> A = buildVector(argc, argv); t.flipSort(A); // t.radixSort(A); copy(A.begin(), A.end(), ostream_iterator<int>(cout, " ")); cout << endl; }
TEST_F(SortingTest, ShouldSwapVariableContents) { Sorting sorting; int a = 10, b = 20; sorting.swap(a, b); ASSERT_EQ(10, b); ASSERT_EQ(20, a); }
TEST_F(SortingTest, ShouldReturnSortedForLargeGroupOfNumber) { Sorting sorting; vector<int> actual = sorting.selectionSort(lrgAges); for (int idx = 0; idx < lrgAges.size(); idx++) { ASSERT_EQ(sortedLrgAges.at(idx), actual.at(idx)); } }
bool CheckDuplicacy(int *Arr,int len) { Sorting *obj = &Sorting(); obj->SetArray(Arr, len); obj->InsertionSort(); for (int i=0;i<len;i++) { if (Arr[i]=Arr[i+1]) return true; } return false; }
int main() { Sorting sort; //sort.BubbleSort(); //sort.SelectionSort(); //sort.InsertionSort(); //sort.ShellSort(); sort.MergeSort(); //sort.QuickSortDriver(); system("PAUSE"); }
int main(){ int arr[] = {10,3,-5,8,2,5,-12,3,13,4,0,0,INT_MAX,INT_MIN}; Sorting srt; srt.sort(arr, sizeof(arr)/sizeof(int)); for (unsigned int i = 0; i < sizeof(arr)/sizeof(int); ++i){ cout << arr[i] << " "; } cout << endl; return 0; }
void Assign::assign(vector <Student*> Courses[NUM_COURSES], vector <Student*> TA, vector <int> TAships){ vector <Student*> assignment[NUM_COURSES]; Sorting sort; unsigned int i,j; // add heuristics, ex: karl's note about constrained TAs // final resort while (!constraint_course(assignment,TAships)/*||!constraint_guarantee(Courses)*/){ for (i=0;i<NUM_COURSES;i++){ //cout << assignment[i].size() << " " << TAships[i] << endl; if(assignment[i].size()<(unsigned)TAships[i]){ assignment[i].push_back(Courses[i][0]); //cout << "Added "<< assignment[i][assignment[i].size()-1]->firstName << " " << assignment[i][assignment[i].size()-1]->TAhoursOwed << " to course " << i << endl; TA[Courses[i][0]->id]->TAhoursOwed -= 54; TA[Courses[i][0]->id]->TAshipsWanted -= 54; TA[Courses[i][0]->id]->TAshipsWanted -= 54; TA[Courses[i][0]->id]->minWilling -= 54; TA[Courses[i][0]->id]->maxWilling -= 54; TA[Courses[i][0]->id]->minTA -= 54; TA[Courses[i][0]->id]->maxTA -= 54; sort.sort(TA,Courses); } /*else if (constraint_course(assignment,TAships)&&!constraint_guarantee(Courses)){ assignment[i].push_back(Courses[i][0]); cout << "Added "<< assignment[i][assignment[i].size()-1]->firstName << "to course " << i << endl; TA[Courses[i][0]->id]->TAhoursOwed -= 54; TA[Courses[i][0]->id]->TAshipsWanted -= 54; TA[Courses[i][0]->id]->TAshipsWanted -= 54; TA[Courses[i][0]->id]->minWilling -= 54; TA[Courses[i][0]->id]->maxWilling -= 54; TA[Courses[i][0]->id]->minTA -= 54; TA[Courses[i][0]->id]->maxTA -= 54; sort.sort(TA,Courses); } */ } } //print assignment for(i=0;i<NUM_COURSES;i++){ cout << "course " << i; for (j=0;j<assignment[i].size();j++){ cout << " " << assignment[i][j]->firstName; } cout << endl; } //return assignment; need to find out how to return vector <student*> assignment[NUM_COURSES] }
int main() { Sorting test = Sorting(); //get the users input int input; string title; while(input != 6) { displayMenu(); cin >> input; //clear out cin cin.clear(); cin.ignore(10000,'\n'); switch (input) { case 1: //initialize array test.initializeArray(); break; case 2: //apply algorithms test.runAlgorithms(); break; case 3: //display results break; case 4: //learn more test.moreInformation(); break; case 5: // change the array settings test.settings(); break; case 6: cout << "Goodbye!" << endl; break; default: cout << "Invalid Input" << endl; cin.clear(); cin.ignore(10000,'\n'); break; } } return 0; }
int hasArrayTwoCandidates(int A[], int size, int k) { Sorting *obj = &Sorting(); obj->SetArray(A, size); obj->InsertionSort(); int left=0; int right=size-1; while(left<right) { if (A[left]+A[right] == k ) { cout<<"number are "<<A[left] <<" ,"<<A[right]<<endl; return 1; } if ( A[left]+A[right] < k) left++; else right-- ; } return 0; }
int MaxOccurance(int *Arr,int len) { int currentcount=0,maxc=0; Sorting *obj = &Sorting(); obj->SetArray(Arr, len); obj->InsertionSort(); int currentcandidate=Arr[0],maxcandidate = Arr[0]; for(int i=0;i<len;i++) { if(Arr[i]==currentcandidate) currentcount++; else { currentcandidate=Arr[i]; currentcount=1; } if(currentcount>maxc) { maxc=currentcount; maxcandidate=currentcandidate; } } return maxcandidate; }
/* * This function prints the Averages from the tests after running through all the tests. * * int max_size = maximum size that we want to test each sorting algorithm to * int min_size = minimum size that we want to test each sorting algorithm to * string theSort = string that contains tha name of the sorting algorithm we are testing. * * Worst-Case Time Complexity: Θ(n^2 + n^2 + n^2 + n^2) = Θ(n^2) */ void testing(int min_size, int max_size, string algorithm){ // making sure I know what the size of each tests I'm doing const int& tests = 5; const int& test_size = 10; // making sure each size of the array is a set value const int& array_size = 10; // average cpu_time arrays for each data set float* increasing_array_averages = new float[array_size]; float* decreasing_array_averages = new float[array_size]; float* random_array_averages = new float[array_size]; float* constant_array_averages = new float[array_size]; std::cout << "\nIncreasing Array using " << algorithm << std::endl; // run 10 size instances (10000, 20000, 30000, etc.) for (int i = 0; i < test_size; i++){ std::cout << max_size << " instances." << std::endl; // instantiate average value and sum value to zero on every new test. float average = 0.0; float sum = 0.0; // create new cpu_times array on every new test. float* cpu_times = new float[5]; // run five tests for (int j = min_size; j < tests; j++){ // set time to 0 before running the cpu times float time = 0.0; // instantiate new array for increasing ordered array. int* increasingArray = ag.generateIncreasingArray(max_size); if (algorithm == "insertionSort"){ clock_t c_start = clock(); s.insertionSort(increasingArray, max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } else if (algorithm == "bubbleSort"){ clock_t c_start = clock(); s.bubbleSort(increasingArray, max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } else if (algorithm == "quickSort"){ clock_t c_start = clock(); s.quickSort(increasingArray, max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } else if (algorithm == "mergeSort"){ clock_t c_start = clock(); s.mergeSort(increasingArray, max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } else { clock_t c_start = clock(); s.selectionSort(increasingArray, max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } //std::cout << "CPU Time (IncreasingArray): " << fixed << setprecision(3) << time << " ms." << std::endl; std::cout << fixed << setprecision(3) << time << " ms" << std::endl; // add cpu time to cpu_times array cpu_times[j] = time; // delete array at the end of each run to avoid memory leaks and faulty data delete[] increasingArray; } // add up the cpu_times values for (int j = 0; j < 5; j++){ sum += cpu_times[j]; } // find average value average = sum / 5.0; //std::cout << "Average time (" << max_size << " instances): " << average << "ms.\n" << std::endl; std::cout << fixed << setprecision(3) << average << " ms\n" << std::endl; // add average value to increasing_array_averages array per number of runs increasing_array_averages[i] = average; // once we pass our last instance (100000) break. if (max_size == 100000){ break; } // go to the next instance (10000 to 20000) max_size += 10000; // delete cpu_times array to avoid memory leaks or faulty data delete[] cpu_times; } // start max_size back at 10000 max_size = 10000; std::cout << "\nDecreasing Array using " << algorithm << std::endl; // run 10 size instances (10000, 20000, 30000, etc.) for (int i = min_size; i < test_size; i++){ std::cout << max_size << " instances." << std::endl; // instantiate average value and sum value to zero on every new test. float average = 0.0; float sum = 0.0; // create new cpu_times array on every new test. float* cpu_times = new float[5]; // run five tests for (int j = min_size; j < tests; j++){ // set time to 0 before running the cpu times float time = 0.0; // instantiate new decreasing order array every run. int* decreasingArray = ag.generateDecreasingArray(max_size); if (algorithm == "insertionSort"){ clock_t c_start = clock(); s.insertionSort(decreasingArray, max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } else if (algorithm == "bubbleSort"){ clock_t c_start = clock(); s.bubbleSort(decreasingArray, max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } else if (algorithm == "quickSort"){ clock_t c_start = clock(); s.quickSort(decreasingArray, max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } else if (algorithm == "mergeSort"){ clock_t c_start = clock(); s.mergeSort(decreasingArray, max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } else { clock_t c_start = clock(); s.selectionSort(decreasingArray, max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } //std::cout << "CPU Time (DecreasingArray): " << fixed << setprecision(3) << time << " ms." << std::endl; std::cout << fixed << setprecision(3) << time << " ms" << std::endl; // add cpu time to cpu_times array cpu_times[j] = time; // delete array at the end of each run to avoid memory leaks and faulty data delete[] decreasingArray; } // add up the cpu_times values for (int j = 0; j < 5; j++){ sum += cpu_times[j]; } // find average value average = sum / 5.0; //std::cout << "Average time (" << max_size << " instances): " << fixed << setprecision(3) << average << "ms.\n" << std::endl; std::cout << fixed << setprecision(3) << average << " ms\n" << std::endl; // add average value to decreasing_array_averages array per number of runs decreasing_array_averages[i] = average; // once we pass our last instance (100000) break. if (max_size == 100000){ break; } // go to the next instance (10000 to 20000) max_size += 10000; // delete cpu_times array to avoid memory leaks or faulty data delete[] cpu_times; } // start max_size back at 10000 max_size = 10000; std::cout << "\nRandom Array using " << algorithm << std::endl; // run 10 size instances (10000, 20000, 30000, etc.) for (int i = min_size; i < test_size; i++){ std::cout << max_size << " instances." << std::endl; // instantiate average value and sum value to zero on every new test. float average = 0.0; float sum = 0.0; // create new cpu_times array on every new test. float* cpu_times = new float[5]; // run five tests for (int j = min_size; j < tests; j++){ // set time to 0 before running the cpu times float time = 0.0; // instantiate new random order array every run. int* randomArray = ag.generateRandomArray(max_size); if (algorithm == "insertionSort"){ clock_t c_start = clock(); s.insertionSort(randomArray, max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } else if (algorithm == "bubbleSort"){ clock_t c_start = clock(); s.bubbleSort(randomArray, max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } else if (algorithm == "quickSort"){ clock_t c_start = clock(); s.quickSort(randomArray, max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } else if (algorithm == "mergeSort"){ clock_t c_start = clock(); s.mergeSort(randomArray, max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } else { clock_t c_start = clock(); s.selectionSort(randomArray, max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } //std::cout << "CPU Time (RandomArray): " << fixed << setprecision(3) << time << " ms." << std::endl; std::cout << fixed << setprecision(3) << time << " ms" << std::endl; // add cpu time to cpu_times array cpu_times[j] = time; // delete array at the end of each run to avoid memory leaks and faulty data delete[] randomArray; } // add up the cpu_times values for (int j = 0; j < 5; j++){ sum += cpu_times[j]; } // find average value average = sum / 5.0; //std::cout << "Average time (" << max_size << " instances): " << fixed << setprecision(3) << average << "ms.\n" << std::endl; std::cout << fixed << setprecision(3) << average << " ms\n" << std::endl; // add average value to random_array_averages array per number of runs random_array_averages[i] = average; // once we pass our last instance (100000) break. if (max_size == 100000){ break; } // go to the next instance (10000 to 20000) max_size += 10000; // delete cpu_times array to avoid memory leaks or faulty data delete[] cpu_times; } // start max_size back at 10000 max_size = 10000; std::cout << "\nConstant Array using " << algorithm << std::endl; // run 10 size instances (10000, 20000, 30000, etc.) for (int i = 0; i < test_size; i++){ std::cout << max_size << " instances." << std::endl; // instantiate average value and sum value to zero on every new test. float average = 0.0; float sum = 0.0; // create new cpu_times array on every new test. float* cpu_times = new float[5]; // run five tests for (int j = 0; j < 5; j++){ // set time to 0 before running the cpu times float time = 0.0; if (algorithm == "insertionSort"){ clock_t c_start = clock(); s.insertionSort(ag.generateConstArray("Awesome", max_size), max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } else if (algorithm == "bubbleSort"){ clock_t c_start = clock(); s.bubbleSort(ag.generateConstArray("Awesome", max_size), max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } else if (algorithm == "quickSort"){ clock_t c_start = clock(); s.quickSort(ag.generateConstArray("Awesome", max_size), max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } else if (algorithm == "mergeSort"){ clock_t c_start = clock(); s.mergeSort(ag.generateConstArray("Awesome", max_size), max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } else { clock_t c_start = clock(); s.selectionSort(ag.generateConstArray("Awesome", max_size), max_size); clock_t c_end = clock(); time = 1000 * (float(c_end) - float(c_start)) / CLOCKS_PER_SEC; } //std::cout << "CPU Time (ConstantArray): " << fixed << setprecision(3) << time << " ms." << std::endl; std::cout << fixed << setprecision(3) << time << " ms" << std::endl; // add cpu time to cpu_times array cpu_times[j] = time; // delete array at the end of each run to avoid memory leaks and faulty data delete[] ag.generateConstArray("Awesome", max_size); } // add up the cpu_times values for (int j = 0; j < 5; j++){ sum += cpu_times[j]; } // find average value average = sum / 5.0; //std::cout << "Average time (" << max_size << " instances): " << fixed << setprecision(3) << average << " ms.\n" << std::endl; std::cout << fixed << setprecision(3) << average << " ms\n" << std::endl; // add average value to constant_array_averages array per number of runs constant_array_averages[i] = average; // once we pass our last instance (100000) break. if (max_size == 100000){ break; } // go to the next instance (10000 to 20000) max_size += 10000; // delete cpu_times array to avoid memory leaks or faulty data delete[] cpu_times; } // print averages of each array generated. printAverages(increasing_array_averages, decreasing_array_averages, random_array_averages, constant_array_averages, array_size, algorithm); // delete each array we used to hold specific (generated arrays) of averages from each test. delete[] constant_array_averages; delete[] random_array_averages; delete[] decreasing_array_averages; delete[] increasing_array_averages; }
int main() { Sorting sortClass; int sortMe[LIST_SIZE] = { 15, 14, 16, 7, 4, 8, 1 }; // INITIAL ARRAY sortClass.LoadArray(sortMe); cout << "THE INITIAL LIST CONTAINS" << endl; sortClass.DisplayArray(); cout << endl; // BUBBLE SORT sortClass.LoadArray(sortMe); cout << "BEGIN BUBBLE SORT" << endl; sortClass.BubbleSort(); sortClass.DisplayArray(); // INSERTION SORT sortClass.LoadArray(sortMe); cout << "BEGIN INSERTION SORT" << endl; sortClass.InsertionSort(); sortClass.DisplayArray(); // SELECTION SORT sortClass.LoadArray(sortMe); cout << "BEGIN SELECTION SORT" << endl; sortClass.SelectionSort(); sortClass.DisplayArray(); return 0; }