0% found this document useful (0 votes)
148 views

CS1010 ETutorial 6 Solution

This document contains sample problems and answers related to searching and sorting algorithms: Problem 1 asks about searching in a sorted vs unsorted array, with binary search being most efficient for a sorted array. Problem 1b discusses searching a sorted array, with binary search being more efficient than sequential search for large arrays. Problem 2 asks about checking if a string is a palindrome by comparing the first and last characters moving inward. Problem 3 asks to write a program to take student data, sort by grade, assign a letter grade based on percentile, then sort by student ID and print the results.

Uploaded by

Yu Shu Hearn
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
148 views

CS1010 ETutorial 6 Solution

This document contains sample problems and answers related to searching and sorting algorithms: Problem 1 asks about searching in a sorted vs unsorted array, with binary search being most efficient for a sorted array. Problem 1b discusses searching a sorted array, with binary search being more efficient than sequential search for large arrays. Problem 2 asks about checking if a string is a palindrome by comparing the first and last characters moving inward. Problem 3 asks to write a program to take student data, sort by grade, assign a letter grade based on percentile, then sort by student ID and print the results.

Uploaded by

Yu Shu Hearn
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CS1010E Tutorial 6

Problem 1a) Consider array of numbers, [1 3 5 7 9 2 4 6 8 10] Which of the following is the most of the efficient method of searching for a given number in the list? a) Binary Search b) Sequential Search c) Bubble Sort followed by Binary Search d) Selection Sort followed by Binary Search Explain your choice. Answer: A sequential search will be most efficient here if we are only searching once. The time needed to first sort an unordered array then going to search it will almost always be less than just brute forcing with a sequential search. However, if we are going to search it multiple times, then sorting it first will make much more sense, since the time we save on searching later on will easily cover the sort time.

Problem 1b) Now consider this array of numbers, 1 2 3 4 5 6 7 8 9 10. Which method is the most efficient now? Answer: If we know it is ordered, then we only need to apply a search algorithm. In this case, the efficiency we save for using the sequential sort is actually not significant. The average number of execution for sequential is 5 times(in a 10 object array), and 2.32(log2(10)-1) for binary

search. However, once the array size becomes much larger, the save in time is considerable. If we have a 10000 object long array, sequential will on average require 5000 executions, whereas binary will require 12.3 only.

Problem 2) In this question, you are given a character array (this is what we call a string) of length greater than 2 and that you are to determine if that string is a palindrome. A String is a palindrome if it reads the same from front and back. eg. kayak If it indeed is a palindrome, you should print out the palindrome. If not, print a message stating it is not To simplify the question, you may use the function strlen() in string.h. This function returns the length of the array. You may assume the length of the word given never exceed 100 characters. A skeleton code is given in palindrome.c, fill in the empty functions. Answer : Refer to palindromeSolution.c

Problem 3) Imagine you are the professor teaching CS1010E, after giving your students a test you need to be able give them grades based on the accursed bell curve. [NOTE: The actual process for determining the bell curve is NOT like this.] Write a program that takes a list of 100 students of matric numbers and corresponding grades, and store them into 2 arrays. First you should sort them by marks, and assign them grades.

For simplicity we have 6 types of grades A , B, C, D ,E and F. 90 percentile and above give A, 60 percentile and above give B, 40 percentile and above give C, 20 percentile and above give D, 10 percentile and above give E, Anything below should get an F. After this, you will need to sort the student according to their matric number. Finally, print out the list of students with grades, sorted by their matric number. There is a sample input and output file, you can use it to match your data. The skeleton code can be found in bellCurve.c. This question brings you into the world of scripting, a very powerful tool useful for many other modules. For both parsing data and generating data. Answer : Refer to bellCurveSolution.c

You might also like