Unformatted text preview: CS 202 Fundamental Structures of Computer Sciences II Assignment 1 – Algorithm Analysis, Sorting and Searching Due Date: March 4, 2011 (Friday), 18:00 Question 1 (15 Points) Trace each of the following sorting algorithms for sorting the first 6 letters of the concatenation of your name and surname into ascending order. a) Insertion sort b) Bubble sort c) Merge sort. Also list the calls to functions mergesort and merge in the order in which they occur. Example: Assume that your full name is STEVE HARRIS. When you concatenate your first name and surname, the first six letters form STEVEH. You have to trace this character array and you have to show each step in the following notation. Steps of Selection Sort applied on this string are presented below. In the figure, shaded elements are selected, while boldface elements are in order. Initial array: S T E V E H st
After 1 swap: S T E H E V After 2nd swap: S E E H T V rd
After 3 swap: H E E S T V th
After 4 swap: E E H S T V th
After 5 swap: E E H S T V Question 2 (45 Points)
Programming Assignment Your aim in this question is to implement the following sorting algorithms for integer arrays, and to empirically evaluate them. Moreover, your program has to meet some specific requirements, which are going to be briefly described hereafter. Firstly, write the functions for the following sorting algorithms. Make sure that your functions sort the given integer array in place and in descending order. • void InsertionSort(int *theArray, int n);
• void QuickSort(int *theArray, int n);
• void RadixSort(int *theArray, int n); Next, write a function to find the time requirements of your functions for different integer arrays. You have to create 9 different arrays and you have to find the time requirements of each of your functions for each of these arrays. The arrays that you have to create: • • • 3 arrays with sizes of 25000, 50000 and 100000. Fill each array with integers in the ascending order (1, 2, 3, 4, …, 25000). Refer these arrays A25000, A50000, and A100000. 3 arrays with sizes of 25000, 50000 and 100000. Fill each array with integers in the descending order (25000, 24999, 24998, …, 1). Refer these arrays D25000, D50000, and D100000. 3 arrays with sizes of 25000, 50000 and 100000. Fill each array with random integers using the random number generator rand. Refer these arrays R25000, R50000, and R100000. You are going to measure the time requirement of your function in two ways. First, use clock() library function (include <ctime>). You should invoke the clock() function before and after the invocation of your sorting function. Then take the difference between these two returned values. The result is the time requirement for your function in clock ticks1. Second, count the operations in your sorting algorithms to find the execution time. After each invocation show your results. For the comparison sorts (insertion sort and quick sort), each comparison can be taken as one operation. For the counting sorts (radix sort), each bucketing can be counted as one operation. You also have to compare the results and present the user, the best and the worst algorithm in different array sizes and scenarios. Your program has to ask the user whether he/she wants to see the results of the first time measurement method or the other. To sum up, your program should display 27 time requirements (9 for each sorting algorithm) for each measurement method. Sample Output When the program is run, it should ask the user what he/she wants to do. Prepare a main menu with 3 choices, two for the time measurement functions and the other for ending the program. Your main menu should look like this: CS202 Assignment1 – Question 2
Algorithm Analysis, Sorting and Searching Program by Salim Arslan, Sec0
Please make a selection:
1. Measure with the clock() Function
2. Measure by Counting Operations
3. Exit 1 A clock tick is an arbitrary measure used by the OS to time events. In Windows it is
1/10000th of a milisecond. In different Unices it varies by kernel version, CPU speed etc. The output of the first selection should be like following. Note that your program should fill “...” with the time requirements that it finds and “???” with the name of the best or worst sorting algorithm. Make sure you follow the same format in your output. It takes ... clock ticks to sort A25000 by insertion sort
It takes ... clock ticks to sort A25000 by quick sort
It takes ... clock ticks to sort A25000 by radix sort
It takes ... clock ticks to sort A50000 by insertion sort
It takes ... clock ticks to sort A50000 by quick sort
It takes ... clock ticks to sort A50000 by radix sort
It takes ... clock ticks to sort A100000 by insertion sort
It takes ... clock ticks to sort A100000 by quick sort
It takes ... clock ticks to sort A100000 by radix sort
For A25000, the best is ??? sort, and the worst is ??? sort.
For A50000, the best is ??? sort, and the worst is ??? sort.
For A100000, the best is ??? sort, and the worst is ??? sort.
ooooooooooooooooooooooIt takes ... clock ticks to sort D25000 by insertion sort
It takes ... clock ticks to sort D25000 by quick sort
It takes ... clock ticks to sort D25000 by radix sort
It takes ... clock ticks to sort D50000 by insertion sort
It takes ... clock ticks to sort D50000 by quick sort
It takes ... clock ticks to sort D50000 by radix sort
It takes ... clock ticks to sort D100000 by insertion sort
It takes ... clock ticks to sort D100000 by quick sort
It takes ... clock ticks to sort D100000 by radix sort
For D25000, the best is ??? sort, and the worst is ??? sort.
For D50000, the best is ??? sort, and the worst is ??? sort.
For D100000, the best is ??? sort, and the worst is ??? sort.
ooooooooooooooooooooooIt takes ... clock ticks to sort R25000 by insertion sort
It takes ... clock ticks to sort R25000 by quick sort
It takes ... clock ticks to sort R25000 by radix sort
It takes ... clock ticks to sort R50000 by insertion sort
It takes ... clock ticks to sort R50000 by quick sort
It takes ... clock ticks to sort R50000 by radix sort
It takes ... clock ticks to sort R100000 by insertion sort
It takes ... clock ticks to sort R100000 by quick sort
It takes ... clock ticks to sort R100000 by radix sort
For R25000, the best is ??? sort, and the worst is ??? sort.
For R50000, the best is ??? sort, and the worst is ??? sort.
For R100000, the best is ??? sort, and the worst is ??? sort.
oooooooooooooooooooooo The output of the second selection should be like following. Note that your program should fill “...” with the time requirements that it finds and “???” with the name of the best or worst sorting algorithm. Make sure you follow the same format in your output. ... operations are done to sort A25000 by insertion sort
... operations are done sort A25000 by quick sort
... operations are done sort A25000 by radix sort
... operations are done sort A50000 by insertion sort
... operations are done sort A50000 by quick sort
... operations are done sort A50000 by radix sort
... operations are done sort A100000 by insertion sort
... operations are done sort A100000 by quick sort
... operations are done sort A100000 by radix sort
For A25000, the best is ??? sort, and the worst is ??? sort.
For A50000, the best is ??? sort, and the worst is ??? sort.
For A100000, the best is ??? sort, and the worst is ??? sort.
oooooooooooooooooooooo... operations are done sort D25000 by insertion sort
... operations are done sort D25000 by quick sort
... operations are done sort D25000 by radix sort
... operations are done sort D50000 by insertion sort
... operations are done sort D50000 by quick sort
... operations are done sort D50000 by radix sort
... operations are done sort D100000 by insertion sort
... operations are done sort D100000 by quick sort
... operations are done sort D100000 by radix sort
For D25000, the best is ??? sort, and the worst is ??? sort.
For D50000, the best is ??? sort, and the worst is ??? sort.
For D100000, the best is ??? sort, and the worst is ??? sort.
oooooooooooooooooooooo... operations are done sort R25000 by insertion sort
... operations are done sort R25000 by quick sort
... operations are done sort R25000 by radix sort
... operations are done sort R50000 by insertion sort
... operations are done sort R50000 by quick sort
... operations are done sort R50000 by radix sort
... operations are done sort R100000 by insertion sort
... operations are done sort R100000 by quick sort
... operations are done sort R100000 by radix sort
For R25000, the best is ??? sort, and the worst is ??? sort.
For R50000, the best is ??? sort, and the worst is ??? sort.
For R100000, the best is ??? sort, and the worst is ??? sort.
oooooooooooooooooooooo Question 3 (15 Points) – Hybrid Sort Use the code you developed in the previous question to implement a hybrid
sorting algorithm: • void HybridSort(int * theArray, int n, int k);
Hybrid sort will start out as a quick sort and will recurse normally. However, it will not divide the problem all the way down to size 1. Instead, once it divides until the size of a subarray is k, it will switch to insertion sort. For example, if you have an array of 100 integers and k=5, the hybrid sort will recursively pivot and divide (on average) 5
6 times and then use insertion sort to sort the final level of <5 subarrays. Write a main function, as above, to test k values between 3 and 15, for the 9 arrays defined in Q.2, and find the best value of k for each. Sample output for the 3 random arrays is given as follows (your program should have similar outputs for the other arrays). Note that your program should fill “...” with the time requirements that it finds and “???” is the value of k for the best and the worst cases. It
It
It
.
.
It takes ... clock ticks and ... ops to sort R25000 with hybrid sort (k=3)
takes ... clock ticks and ... ops to sort R25000 with hybrid sort (k=4)
takes ... clock ticks and ... ops to sort R25000 with hybrid sort (k=5)
takes ... clock ticks and ... ops to sort R25000 with hybrid sort (k=15) It
It
It
.
.
It takes ... clock ticks and ...
takes ... clock ticks and ...
takes ... clock ticks and ... ops to sort R50000 with hybrid sort (k=3)
ops to sort R50000 with hybrid sort (k=4)
ops to sort R50000 with hybrid sort (k=5) takes ... clock ticks and ... ops to sort R50000 with hybrid sort (k=15) It
It
It
.
.
It takes ... clock ticks and ...
takes ... clock ticks and ...
takes ... clock ticks and ... ops to sort R100000 with hybrid sort (k=3)
ops to sort R100000 with hybrid sort (k=4)
ops to sort R100000 with hybrid sort (k=5) takes ... clock ticks and ... ops to sort R100000 with hybrid sort (k=15) For R25000, the best k is ??? and the worst k is ???.
For R50000, the best k is ??? and the worst k is ???.
For R100000, the best k is ??? and the worst k is ???.
oooooooooooooooooooooo Code Format and Notifications To increase the efficiency of the grading process as well as the readability of your code, you have to follow the following instructions about the format and general layout of your program. • Don’t forget to write down your id, name, section, assignment number or any other information relevant to your program in the beginning of your main file. Example: /*
* Title: Algorithm Analysis, Sorting and Searching Program
* Author: Salim Arslan
* ID: 2100000000
* Section: 0
* Assignment: 1
* Description: This program is for comparing different sorting
* algorithms in different scenarios.
*/ • Since your codes will be checked without your observation, you should report everything about your implementation. Well comment your classes, functions, declarations etc. Make sure that you explain each function in the beginning of your function structure. Example: void selectionSort(int *theArray, int n)
// // Summary: Sorts the items in an array into descending order.
// Precondition: theArray is an array of n integers.
// Postcondition: theArray is sorted into ascending order, n is
// unchanged.
// Calls: calledFunction1, calledFunction2 ...
// {
// body of the function
} // end selectionSort Indentation, indentation, indentation... • Pay attention to these instructions, otherwise you may lose some points even though your code has no error. • Question 4 (15 Points) With the help of a spreadsheet program (Microsoft Excel, Matlab or other tools), or manually, present your experimental results graphically. Compare your empirical results with the theoretical ones for each sorting algorithm. Explain any differences between the empirical and theoretical results, if any. Make this graphical analysis for the previous two questions (both Q.2 and Q.3). Question 5 (10 Points) Consider a sequential search of n data items. a) If the data items are sorted into ascending order, how can you determine that your desired item is not in the data? b) What is the order of the sequential search algorithm when the desired item is not in the data collection? Do this for both sorted and unsorted data, and consider the best, average, and the worst cases. c) Show that if the sequential search algorithm finds the desired item in the data collection, the algorithm’s order does not depend upon whether or not the data items are sorted. Bonus Question (15 Points) Consider the following sorting algorithm, called Monkey Sort: void MonkeySort(int *theArray, int n)
{
bool sorted = false;
while(sorted == false) {
for( int i = 0; i < n; i++) {
index = (rand() % (n  1));
tmp = theArray[i];
theArray[i] = theArray[index];
theArray[index] = tmp;
}
sorted = true;
for( int i = 0; i < n  1; i++) {
if(theArray[i] >= theArray[i + 1])
sorted = false;
break;
}
} { }
} Derive the theoretical best, average, and worst case performance bounds of the MonkeySort algorithm. The way you prove the answer is more important than the answer here. ...
View
Full Document
 Spring '11
 Pablo
 Sort, Comparison sort, the00, CLOCK TICKS

Click to edit the document details