/* Main function loops through all of the data sets, loads each one into a vector, then runs that data set on both Insertion Sort and Bubble Sort, then outputs algorithm step counts and runtimes for both Insertion Sort and Bubble Sort, then repeats with the next data set until the "content" vector is finished */ int main(){ std::string filename; filename = "dus-"; std::string fileExtension; fileExtension = "_SORTED.txt"; std::vector<std::string> content = {"2","4","6","8","10","12","16","20","24"}; int fileNum = content.size(); std::cout<<"File Name__________________Algorithm_______________Number of Steps_____________Duration\n\n"; for(int i=0; i<fileNum; i++){ insertionCount=0; bubbleCount=0; std::string file = filename+content[i]+fileExtension; std::vector<int> data; read_file(file, data); std::vector<int> data_copy = data; std::vector<int> data_copy_copy = data; beforeInsertion = clock(); Insertion_Sort(data_copy); afterInsertion = clock(); insertionTime = double(afterInsertion-beforeInsertion)/CLOCKS_PER_SEC; std::cout<<file<<" Insertion Sort "<<insertionCount<<" "<<insertionTime<<" Seconds\n"; beforeBubble = clock(); Bubble_Sort(data_copy_copy); afterBubble = clock(); bubbleTime = double(afterBubble-beforeBubble)/CLOCKS_PER_SEC; std::cout<<file<<" Bubble Sort "<<bubbleCount<<" "<<bubbleTime<<" Seconds\n"; } std::cout<<"\n"; }
int main() { char** pArr = new char*[100]; for(int i = 0; i < 100; ++i) { pArr[i] = new char[1000]; } int index = 0; int* Count = new int[100]; for(int i = 0; i < 100; ++i) { Count[i] = 0; } //------------------------------------------------ while(!feof(stdin) && index < 100) { int j = 0; int c; while((c = getchar()) != '0' && c != EOF && j < 1000) { pArr[index][j++] = c; } fflush(stdin); // Remove '\n' from i-sequence Count[index] = j; ++index; } for(int i = 0; i < index; ++i) { Insertion_Sort(pArr[i], Count[i]); for(int j = 0; j < Count[i]; ++j) { printf("%c", pArr[i][j]); } printf("\n"); } //------------------------------------------------ //------------------------------------------------ delete [] Count; for(int i = 0; i < 100; ++i) { delete pArr[i]; } delete [] pArr; //------------------------------------------------ _getch(); return 0; }
Node * Shell_Sort(Node *list) { long Size; //store the size of the integer list Size = findSize(list); //find the size int q = largest_index(3, Size); //find the largest value for q less than size long j, l; int count1; Node * head = list; int pow1, pow2, k; for (pow1 = q; pow1 >= 0; pow1--) { for (pow2 = pow1; pow2 >= 0 ; pow2--) { Node * cur = head; k = power(3, pow2) * power(2, pow1 - pow2); //use this formula to calculate the sequence elements for (j = 1; j <= (Size - k); j++) { List * dhead = NULL; dhead = List_Insert(dhead, cur); //insert starting from the first j'th value Node * tmp1 = cur; for (l = j + k; l <= Size; l = l + k) { //printf("\nj+k=%ld\n", l); count1 = 0; while (count1 != k) { tmp1 = tmp1 -> next; //keep on iterating till you reach the correct node count1++; } dhead = List_Insert(dhead, tmp1); //insert node into the sublist } Insertion_Sort(dhead); //implement insertion sort cur = cur -> next; //make current as the next node while (dhead != NULL) //free memory due to each sublist before the next iteration { List * tmp = dhead; dhead = dhead -> next; free(tmp); } } } } return head; //return the list of integers that has been sorted }
int main(){ std::vector<int> data = read_file("dus-16.txt"); std::cout<<"\nOriginal Vector...\n"; int size = data.size(); for(int i=0; i<size; i++){ std::cout<<data[i]<<" "; } std::cout<<"\nSorted Vector...\n"; before = clock(); Insertion_Sort(data); after = clock(); runTime = double(after-before)/CLOCKS_PER_SEC; for(int i=0; i<size; i++){ std::cout<<data[i]<<" "; } std::cout<<"\n"; std::cout<<"Total Steps Taken: "<<counter<<"\n"; std::cout<<"Total Runtime of Algorithm: "<<runTime<<" Seconds\n\n"; }
/************************************************************************* // Function: main(void) // Description : Gets input from user, passes it into functions, // and puts certain outputs to the screen. // Inputs: Reads inputs from the keyboard. // Outputs: Writes output to the screen. // Preconditions: <none> // Postconditions: Prints to screen and takes input from keyboard //***********************************************************************/ void main(void) { int array[ARRAY_SIZE], length = ARRAY_SIZE, choice, t, number; bool Ascending; srand(time(NULL)); // invokes random generation // // Run menu until exit condition is entered. // while(choice != 5) { cout << "\n\n---Insertion Sort---\n\n"; cout << "1. Generate Array\n2. Sort Ascending\n3. Sort Descending\n" << "4. Search\n5. Quit\n\n"; cout << "Choice-> "; cin >> choice; switch(choice) { // // Generate random numbers to fill array. // case 1 : for(t = 0; t < ARRAY_SIZE; t++) { if(t <= 6) { array[t] = (((rand() % 40) + 10) * -1); } else { array[t] = ((rand() % 50) + 50); } } cout << "\nUnorganized Array: "; for(t = 0; t < ARRAY_SIZE; t++) { cout << array[t] << " "; } break; // // Sort array into an ascending order. // case 2 : cout << "\nSort Ascending...\n"; Insertion_Sort(array, length, TRUE); for(t = 0; t < ARRAY_SIZE; t++) { cout << array[t] << " "; } break; // // Sort array into a descending order. // case 3 : cout << "\nSort Descending...\n"; Insertion_Sort(array, length, FALSE); for(t = 0; t < ARRAY_SIZE; t++) { cout << array[t] << " "; } break; // // Prompt user to enter a number, function searches for it in array. // case 4 : cout << "\nEnter number to search for: "; cin >> number; Search_Array(array, number); break; // // Say goodbye. // case 5 : cout << "\nGoodbye."; break; default : cout << "\nEnter a valid menu choice.\n"; break; } } }