CS 202 Fundamental Structures of Computer Science II Assignment 1 – Algorithm Efficiency and Sorting Due Date: 15 October 2014 (Wednesday) Question-‐1 (30 points) Trace the following sorting algorithms to sort the array [ 9 7 1 4 5 3 ] into ascending order. Use the array implementation as described in the textbook/lectures. a) Insertion sort. b) Selection sort. c) Bubble sort. d) Merge sort; also list the calls to mergesort and merge in the order they occur. e) Quick sort; also list the calls to quicksort and partition in the order they occur. Assume that the last item is chosen as pivot. Question-‐2 (60 points) Programming Assignment -‐-‐ You are asked to implement the insertion sort and quick sort algorithms for linked lists of integers and then perform the measurements as detailed below. 1. For each algorithm, implement the functions that take a linked list of integers and then sort it in ascending order. Add a counter to count the number of pointer changes within the linked list. The counter is only for this purpose and does not participate in sorting. Note that your implementations should sort the linked list in place, that is, they should not create any new linked list or other new data structure of non-‐trivial size (such as an array, an STL vector, a stack, a queue, and others). 2. For the quick sort algorithm, you are supposed to take the first element of the linked list as pivot. 3. Write a main function to measure the time required by each sorting algorithm. To this end, use the library function clock(), which is defined in time.h. Invoke the clock() library function before and after each sorting algorithm to measure the elapsed time in milliseconds. After implementing the sorting algorithms, 1. Create two identical linked lists with random 25,000 integers using the random number generator function rand. Use one of these linked lists for the insertion sort algorithm and the other for the quick sort algorithm. Output the number of pointer changes and the elapsed time to sort these integers using each of these algorithms. Repeat this experiment for at least 5 different input sizes that are greater than 25,000 (i.e., 50 000, 75 000, 100 000, 150 000, 200 000). With the help of a graphical plotting tool, present your experimental results graphically. 2. Then, create two more linked lists with 25,000 integers that are sorted in descending order. Use each linked list for each algorithm. Repeat all of the experiments and present your experimental results graphically. (That is, for each algorithm, create linked lists with at least 5 different input sizes and output the number of pointer changes and the elapsed time to sort these linked lists and present your results graphically.) 3. Lastly, create two more linked lists with 25,000 integers that are sorted in ascending order. Use each linked list for each algorithm. Repeat all of the experiments present your experimental results graphically. IMPORTANT: In this programming assignment, you are supposed to implement your own linked list class. That is, you are not allowed to use an array-‐based list implementation or the list class in the C++ standard template library (STL). You can use and adapt the linked list codes in the Carrano book. However, you cannot use any existing linked list code from other sources. Question-‐3 (10 points) Interpret your experimental results that you obtained in Question-‐2. Compare these results with the theoretical ones for each sorting algorithm. Explain any differences between the experimental and theoretical results. HAND-‐IN You should submit both the softcopy and hardcopy of your homework. Before 18:00 of October 15, 2014, send an email, with a subject line CS202 HW1, to your TA, Cem Orhan (cem.orhan at bilkent edu tr), attaching one zipped file that contains o hw01.doc, the file containing the answers to Questions 1 and 3, the sample output of the program, and the graphical findings of the experiments for Question 2, o hw01.cpp and hw01.h (if needed), the files containing the C++ source code and the non-‐standard library used in Question 2, and o readme.txt, the file containing anything important on the compilation and execution of your program in Question 2. o Do not forget to put your name, student id, and section number, in all of these files. Well comment your implementation. o IMPORTANT: The subject line of your email must be CS202 HW1, otherwise, it may be considered as a junk email, which means that your homework may not be graded. Turn in the hardcopy of your homework, which includes solutions to all questions, to Cem Orhan on the due date before 17:00. The hardcopy should be identical to the files zipped in your email. Keep all the files before you receive your grade. This homework will be graded by your TA, Cem Orhan. Thus, you may ask your homework related questions directly to him. DO THE HOMEWORK YOURSELF. PLAGIARISM AND CHEATING ARE HEAVILY PUNISHED!!!
© Copyright 2017