int Mymain() { std::vector<int> nums; GenerateRandomArray(nums, 10001); auto nums_s = nums; int k = 1539; int myk = 0, stdk = 2; {TIC //startQuickSort(nums, 0, nums.size() - 1); myk = findKthLargest(nums, k); TOC} {TIC std::sort(nums_s.begin(), nums_s.end()); stdk = nums_s[nums.size() - k]; TOC} cout << "Check increase : " << std::boolalpha << CheckSorted(nums, nIncrease) << std::noboolalpha << endl; cout << "Check K Largest : " << std::boolalpha << (myk == stdk) << std::noboolalpha << endl; return 0; }
void _main(void) { char Line[255] ; char Chose ; do { //2012: lock the interrupt sys_disable_interrupt(); cprintf("\n"); cprintf("!!!!!!!!!!!!!!!!!!!!\n"); cprintf("!!!! MERGE SORT !!!!\n"); cprintf("!!!!!!!!!!!!!!!!!!!!\n"); cprintf("\n"); readline("Enter the number of elements: ", Line); int NumOfElements = strtol(Line, NULL, 10) ; int *Elements = malloc(sizeof(int) * NumOfElements) ; cprintf("Chose the initialization method:\n") ; cprintf("a) Ascending\n") ; cprintf("b) Descending\n") ; cprintf("c) Semi random\n"); cprintf("Select: ") ; Chose = getchar() ; cputchar(Chose); cputchar('\n'); //2012: lock the interrupt sys_enable_interrupt(); int i ; switch (Chose) { case 'a': InitializeAscending(Elements, NumOfElements); break ; case 'b': InitializeDescending(Elements, NumOfElements); break ; case 'c': InitializeSemiRandom(Elements, NumOfElements); break ; default: InitializeSemiRandom(Elements, NumOfElements); } MSort(Elements, 1, NumOfElements); cprintf("Sorting is Finished!!!!it'll be checked now....\n") ; //PrintElements(Elements, NumOfElements); uint32 Sorted = CheckSorted(Elements, NumOfElements); if(Sorted == 0) panic("The array is NOT sorted correctly") ; else { cprintf("===============================================\n") ; cprintf("Congratulations!! The array is sorted correctly\n") ; cprintf("===============================================\n\n") ; } free(Elements) ; cprintf("Do you want to repeat (y/n): ") ; Chose = getchar() ; cputchar(Chose); cputchar('\n'); } while (Chose == 'y'); }
void _main(void) { //int InitFreeFrames = sys_calculate_free_frames() ; char Line[255] ; char Chose ; int Iteration = 0 ; do { int InitFreeFrames = sys_calculate_free_frames() + sys_calculate_modified_frames(); Iteration++ ; // cprintf("Free Frames Before Allocation = %d\n", sys_calculate_free_frames()) ; sys_disable_interrupt(); readline("Enter the number of elements: ", Line); int NumOfElements = strtol(Line, NULL, 10) ; int *Elements = malloc(sizeof(int) * NumOfElements) ; Elements[NumOfElements] = 10 ; // cprintf("Free Frames After Allocation = %d\n", sys_calculate_free_frames()) ; cprintf("Choose the initialization method:\n") ; cprintf("a) Ascending\n") ; cprintf("b) Descending\n") ; cprintf("c) Semi random\nSelect: ") ; Chose = getchar() ; cputchar(Chose); cputchar('\n'); sys_enable_interrupt(); int i ; switch (Chose) { case 'a': InitializeAscending(Elements, NumOfElements); break ; case 'b': InitializeDescending(Elements, NumOfElements); break ; case 'c': InitializeSemiRandom(Elements, NumOfElements); break ; default: InitializeSemiRandom(Elements, NumOfElements); } QuickSort(Elements, NumOfElements); // PrintElements(Elements, NumOfElements); uint32 Sorted = CheckSorted(Elements, NumOfElements); if(Sorted == 0) panic("The array is NOT sorted correctly") ; else { cprintf("\n===============================================\n") ; cprintf("Congratulations!! The array is sorted correctly\n") ; cprintf("===============================================\n\n") ; } // cprintf("Free Frames After Calculation = %d\n", sys_calculate_free_frames()) ; cprintf("Freeing the Heap...\n\n") ; free(Elements) ; ///======================================================================== sys_disable_interrupt(); cprintf("Do you want to repeat (y/n): ") ; Chose = getchar() ; cputchar(Chose); cputchar('\n'); cputchar('\n'); sys_enable_interrupt(); } while (Chose == 'y'); }