You are asked to write a program which can read an integer is_p (meaing whether to print out A or not), natural number n, a lcase n a_min a_max IS MS HS OS CS RS BS 11 10,000 1 10,000 2 10,000 10,000 30,000 310,000 1 30,000 50,000 150,000 5 50,000 20

c++

You are asked to write a program which can read an integer is_p (meaing whether to print out A or not), natural number n, a lower bound integer a_min, an upper bound integer a_max, and an integer as the random seed rs allocate memory for an array A of n integers (here we assume to use A[1]….,A[n], if you want to use A[O),…,A(n-1), please do whatever necessary changes by yourself) using the random seed rs to generate n random integer numbers which are uniformly distributed in the range [a_min, a_max]. and store these n random integer numbers in A[1]…,A[n]. if is_p=1 then print out A[1]….A[n] and STOP; Otherwise do nothing and CONTINUE write the following 7 sorting functions IS(A,n): do the insertion sort MS(A,1,n): do the merge sort HS(A,n): do the heap sort QS(A,1,n): do the quick sort CS(A,n,k): do the counting sort RS(A,n,k): do the radix sort BS(A,n): do the bucket sort these five functions all sort the same array A in nonincreasing order your code should also contain the following 4 other functions printout_array(A,1,n): to printout the integer 1-D array A (from previous HW, make necesary changes by yourself) duplicate_array(A,B,1,n): to duplicate 1-D array A to be array B, where both A & B have dimensions 1xn (from previous HW, make necesary changes by yourself) is_A_desc(A,n): to check whether A[i]>=A[j] for any isj if it passes the checking, then return “1” and printout “order OK!”; otherwise, return “0” and printout “!!WRONGLY SORTED!!”. (from HW4, make necesary changes by yourself) mytimer(): to return a time tag. please use related files in this example file To be more specific, your code has to read [is_p, n, a_min, a_max, rs) from screen allocate memory for 2 arrays of integers A and B, each with size=n using rs to generate n random integers uniformly distributed in the range [a_min,a_max], store them in A if is_p=1 then print out A and STOP copy A to B i.e. duplicate A’s value into B) DO INSERTION SORT execute t1=mytimer), IS(A,n), execute t2=mytimer), compute t_is=t2-t1 execute is_A_desc(A,n), if it returns “0” then print out “!!INSERTION SORT WRONG!!” and STOP. copy B back to A so that A has its original values DO MERGE SORT execute t1=mytimer), MS(A,1,n), execute t2=mytimer(), compute t_ms-t2-t1 execute is_A_desc(A,n), if it returns “0” then print out “!!MERGE SORT WRONG!!” and STOP. copy B back to A so that A has its original values DO HEAP SORT execute t1=mytimer(), HS(A,n), execute t2=mytimer(), compute t_hs=t2-t1 execute is_A_desc(A,n), if it returns “0” then print out “!!HEAP SORT WRONG!!” and STOP. copy B back to A so that A has its original values DO QUICK SORT execute t1=mytimer), QS(A,1,n), execute t2=mytimer), compute t_qs=t2-t1 execute is_A_desc(A,n), if it returns “0” then print out “!!QUICK SORT WRONG!!” and STOP. copy B back to A so that A has its original values DO COUNTING SORT execute t1=mytimer), CS(A,n), execute t2=mytimer(), compute t_cs=t2-t1 execute is_A_desc(A,n), if it returns “0” then print out “!!COUNTING SORT WRONG!!” and STOP. DO RADIX SORT execute t1=mytimer(), RS(A,n,k), execute t2=mytimer(), compute t_rs-t2-t1 execute is_A_desc(A,n), if it returns “0” then print out “!!RADIX SORT WRONG!!” and STOP. DO BUCKET SORT execute t1=mytimer(), BS(A,n,k), execute t2=mytimer(), compute t_bs=t2-t1 execute is_A_desc(A,n), if it returns “0” then print out “!!BUCKET SORT WRONG!!” and STOP. printout n, a_min, a_max, rs, t_is, t_ms, t_hs, t_qs, t_cs, t_rs, and tubs free all the memories and end the program Thus, if your code stops in the middle somehwere showing a warning message saying that A is not sorted in nonincreasing order, that must mean your sorting algorithm is wrong. A correct code should successfully sort the same n integers without problems. Small examples: (suppose your code is compiled with a name “allsort”) allsort Input [is_p,n,a_min,a_max,rs] = 1 10 10 1000 1 (input 1,10, 10,1000, 1) 783 34 872 546 215 89 965 235 77 284 (output 10 random num between 10~1000) allsort Input [is_p,n,a_min,a_max,rs] = 0 10 10 1000 1 (input 0,10, 10,1000, 1) 10 10 1000 1 20.1 15.2 14.4 8.9 5.3 7.6 9.7 (output n, a_min, a_max, rs, t_is, t_ms, t_hs, t_qs, t_cs, t_rs, t_bs) You have to input an n that is large enough (e.g. n=10,000,000, a_min=1, a_max=10,000,000) to make the computational time differentiable. In particular, you have to test the following 9 random [n, a min, a_max] categories:(10000,1,10000]; (10000, 10000,300001; (10000, 1, 30000); (50000, 1, 50000]; (50000, 20000, 70000]; (50000, 1, 70000); [100000,1,100000]; 1100000, 50000, 150000); [100000,1,150000); For each category, you should generate 20 random cases, each with a different random seed. After the test, make a table to summarize the average running time with standard deviation) spent for each algorithm and each category as the following table, where the blank cells should be filled in with the mean time (with stdev) spent by algorithms IS, MS, HS, QS, CS, RS and BS, respectively. case n a_min a_max IS MS HS OS CS RS BS 11 10,000 1 10,000 2 10,000 10,000 30,000 310,000 1 30,000 50,000 150,000 5 50,000 20,000 70,000 650,0001 70,000 7100,000 1 100,000 8 100,000 50,000 150,000 9 100,000 1150,000 Besides this table, you may conduct more experiments by yourself and then write down your observation for the computational time spent for different sorting algorithms in different catogires, and try to give them a rank showing which one is more efficient and which one is not, in which situation In order to handle the totally 20×9=180 test cases automatically (i.e. not by manually input), you had better learn how to write a batch code. In particular, for this homework you may create a file named run_all.bat (in cygwin/unix you should also chmod 777 run_all.bat to make this file executable) which may contain the following scripts: echo 0 10000 1 10000 1 > input.txt allsort results.txt echo 0 10000 1 10000 2 > input.txt allsort > results.txt echo 0 10000 1 10000 20 > input.txt allsort > results.txt echo 0 10000 10000 30000 1 > input.txt allsort > results.txt echo 0 10000 10000 30000 2 > input.txt allsort > results.txt echo 0 10000 10000 30000 20 > input.txt allsort > results.txt echo 0 100000 1 150000 1 > input.txt allsort > results.txt echo 0 100000 1 150000 2 > input.txt allsort > results.txt echo 0 100000 1 150000 20 > input.txt allsort > results.txt after you have finished executing this batch file, you can use Excel to open results.txt and conduct statistical analysis on the results i.e. sorting by n, a min, a_max, rs, calculate the running time average of the 100 cases for each category). You HAVE TO dynamically create an array of n real numbers. If you use the fixed size (e.g. A[1000]) in your variable declaration, you will get minus points. (The TAs may test your codes using a large number (say, n=100000000 or even much more) of random numbers. Thus if you fix the size of A, your code will show wrong results in our testing.) You DO NOT HAVE TO take care of any invalid input (e.g. if someone input a character/string, your code does not need to detect these invalid input, even if the output is invalid.



Source link

Leave a Reply

Your email address will not be published.