Starting from:
$30

$24

Lab #8 Searching and sorting Solution

Goals




Learn about simple searching methods
Learn about algorithms for sorting



In this lab, we will use algorithms to search data and sort data.




Requirements




Note: The book or the lectures have several examples of searching and sorting algorithms.




Create Data Files




You need to manually create at least four text files with random integer values. You




don’t need large number of values, which makes testing easie For example, you can create a file with values: 1 2 3 4 5 6 7 8 9. Save it, and make 3

copies. In each copy put a single value of 0: one towards the beginning of file, one

around the middle of file, and one towards the end of file. Rename the 3 copies with

name such as “early.txt”, “middlethe.originaltxt”,one“endwithout.txt”a0.. Keep




Note: All the text files will be used during the lab. Make sure they are included in submission.










The following is the flow of the program




Read Values from Files



The program should, for each file, read the file and put the values into an array at the start of program.




Simple Search



Implement a search algorithm for your program that searches for a target value in your four data files. When implemented, the program should first ask for target value and validate user input, then output whether target value is found for EACH file. Can we use binary search here? Think about it.




Example output




What is target value:




0




num.txt: target value not found

early.txt: target value found




mid.txt: target value found




end.txt: target value found




Sorting



There are a lot of sorting algorithms; find one and implement it in your program and cite the sources. When implemented, the program should, for each file, ask user for the output file name, then use the algorithms to sort the values, and finally output the sorted values to the output files. Don’t forgetprintthe tosrted values in each file onto the screen as well.




Binary Search



Find an algorithm for binary search. Implement it in the program that searches for target value in your sorted data files from task 3. The operation should be fairly similar to the simple search.

More products