Spring 2022 CECS 274 Lab 6 -- Searching & Sorting (corrected)

pdf

School

San Diego State University *

*We aren’t endorsed by this school

Course

229

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

7

Uploaded by HighnessProton15266

Report
CECS 274 Lab 6: Searching & Sorting Due Friday, 4/29 @11:59 PM LAB 6: SEARCHING & SORTING Learning Objectives: CLO 3. Develop problem solving skills by implementing data structures. CLO 4. Compare advantages and disadvantages of different data structure implementations. Function Implementations Files to Modify: algorithms.py Directions: 1. Implement the function linear_search(a, x) which uses the linear search algorithm to re- turn the index of element x if it is found in the ArrayList object a . If x , is not found in a , then the function returns -100. 2. Implement the function binary_search(a, x) which uses the binary search algorithm to return the index of element x if it is found in the sorted ArrayList object a . If x , is not found in a , then the function returns -100. 3. Implement the function _merge(a0, a1, a) which overwrites ArrayList object a by merging the elements of ArrayList object a0 and ArrayList object a1 in increasing order. 4. Implement the function merge_sort(a) which uses the merge-sort algorithm to sort the ArrayList object a . 5. Implement the helper function _quick_sort_f(a, start, end) which uses the quick-sort algorithm with the first element as pivot to sort the ArrayList object a . 6. Implement the helper function _quick_sort_r(a, start, end) which uses the quick-sort algorithm with a random element as pivot to sort the ArrayList object a . NOTE: You may introduce any additional helper functions your quick sort functions might need, as long as you do not change the parameters defined for each function. Page 1 of 7
CECS 274 Lab 6: Searching & Sorting Due Friday, 4/29 @11:59 PM Changes to Book Files to modify: Book.py Directions: Change the implementation of the Book class so that Book instances are compared on the basis of title rather than rank. More specifically, suppose that book1 and book2 are Book instances. Overload the <, >, , and = operators so that book1 < book2 returns True if book1 has a title that comes before the title of book2 , when listed in alphabetical order Aa - Zz (i.e., regardless of letter case) and False otherwise. book1 > book2 returns True if book1 has a title that comes after the title of book2 , when listed in alphabetical order Aa - Zz (i.e., regardless of letter case) and False otherwise. book1 <= book2 returns True if book1 has a title that comes before, or is exactly the same as, the title of book2 , when listed in alphabetical order Aa - Zz (i.e., regardless of letter case) and False otherwise. book1 >= book2 returns True if book1 has a title that comes after, or is exactly the same as, the title of book2 , when listed in alphabetical order Aa - Zz (i.e., regardless of letter case) and False otherwise. book1 == book2 returns True if the title of the book with the longest title begins with the title of the book with the shortest title (regardless of letter case) and False otherwise. For example, if book1 has title, "Home is Where the Heart Is" , book2 has title "Home On the Range" , and book3 has title "home" , then book1 == book2 returns False book1 == book3 returns True book2 == book3 returns True Page 2 of 7
CECS 274 Lab 6: Searching & Sorting Due Friday, 4/29 @11:59 PM Changes to the Bookstore System Files to modify: BookStore.py Directions: 1. Create a BookStore method called sort_catalog(s) that sorts the ArrayList instance self.bookCatalog using the sorting algorithm determined by parameter s . If s = 1, the method uses the merge-sort algorithm. If s = 2, the method uses the quick-sort algorithm with first element as pivot. If s = 3, the method uses the quick-sort algorithm with a randomly chosen element as pivot. Once the book catalog has been sorted, the method must print the message: "Sorted {self.bookCatalog.size()} books in {elapsed_time} seconds." 2. Create a BookStore method called search_by_prefix(prefix, algo) that uses the algo- rithm determined by the parameter algo to search the book catalog for all the book titles containing the given prefix. If algo = 1, the method uses linear search. If algo = 2, the method uses binary search. Note that if the user chooses this option, the catalog must be sorted prior to searching. You must decide which algorithm will be used to sort the catalog if this option is chosen (Keep in mind that it is possible that you may be sorting an already sorted catalog if the user had previously chosen option 10 of the bookstore menu). Hint: Create a copy of the book catalog (i.e., of self.bookCatalog ). Search in the copy of the catalog for a book with the given prefix, print it, and remove it from the copy of the catalog. Repeat until there are no more books in the copy of the catalog with the given prefix. The method must print the time it took to complete its task using a message in the following format: "Found <insert number of> books with prefix <insert given prefix> in {elapsed_time} seconds." 3. Create a BookStore method called display_catalog(n) that displays the first n books of the book catalog. Page 3 of 7
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
CECS 274 Lab 6: Searching & Sorting Due Friday, 4/29 @11:59 PM Changes to Main Files to modify: main.py Directions: 1. Modify menu_bookstore_system() so that it includes the following new options: sorting the book catalog searching for a book by prefix displaying the first n books of the catalog Your new list of options should read as follows: s FIFO shopping cart r Random shopping cart 1 Load book catalog 2 Remove a book by index from catalog 3 Add a book by index to shopping cart 4 Remove from the shopping cart 5 Search book by infix 6 Get cart best-seller 7 Add a book by key to shopping cart 8 Add a book by title prefix to shopping cart 9 Search best-sellers with infix 10 Sort the catalog 11 Search book by prefix 12 Display the first n books of catalog 0 Return to main menu 2. If the user chooses option 10, the system should prompt for an algorithm to use, in the fol- lowing manner: Choose an algorithm: 1 - Merge Sort 2 - Quick Sort (first element pivot) 3 - Quick Sort (random element pivot) Your selection: <Insert integer> If the user’s selection is not 1, 2, or 3, the message "Invalid algorithm" is displayed. 3. If the user chooses option 11, the system should prompt for a prefix and an algorithm to use, in the following manner: Enter prefix: <Insert prefix> Choose an algorithm: 1 - Linear Search 2 - Binary Search Your selection: <Insert integer> If the user’s selection is not 1, or 2, the message "Invalid algorithm" is displayed. Page 4 of 7
CECS 274 Lab 6: Searching & Sorting Due Friday, 4/29 @11:59 PM 4. If the user chooses option 12, the system should prompt for a number of books to display in the following manner: Enter the number of books to display: <insert integer> SUBMISSION PROCESS Submit to CodePost algorithms.py Book.py Submit to BeachBoard ArrayList.py algorithms.py Book.py BookStore.py main.py Page 5 of 7
CECS 274 Lab 6: Searching & Sorting Due Friday, 4/29 @11:59 PM Demo To Your TA Test your program: 1. Select option 1 in the book store system to load the book catalog into an ArrayList . 2. Select option 11 in the book store system to search the catalog for all books with prefix “world of the", using linear search. It should display 6 titles. 3. Select option 11 in the book store system to search the catalog for all books with prefix “world of the", using binary search. It should display 6 titles. 4. Select option 1 to reload the book catalog. 5. Select option 12 to display the first 20 books. 6. Select option 10 to sort the book catalog using merge sort. 7. Select option 12 to display the first 20 books. 8. Select option 1 to reload the book catalog. 9. Select option 12 to display the first 20 books. 10. Select option 10 to sort the book catalog using quick sort with first element pivot. 11. Select option 12 to display the first 20 books. 12. Select option 1 to reload the book catalog. 13. Select option 12 to display the first 20 books. 14. Select option 10 to sort the book catalog using quick sort with random element pivot. 15. Select option 12 to display the first 20 books. 16. Select option 11 in the book store system to search the catalog for all books with prefix “tears of", using linear search. It should display 22 titles. 17. Select option 11 in the book store system to search the catalog for all books with prefix “tears of", using binary search. It should display 22 titles. 18. Answer the following question in the “Comments" section of the Beachboard Dropbox folder. Your TA will grade this portion: Compare the differences in observed times for searches made with linear search vs. binary search. Does the observed time agree with differences in expected runtime? Why or why not? Page 6 of 7
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
CECS 274 Lab 6: Searching & Sorting Due Friday, 4/29 @11:59 PM RUBRICS Level 3 Level 2 Level 1 2 Pt 1 Pt 0.5 Pt Linear Search & Binary Search im- plementation Implementation is cor- rect Implementation is par- tially correct. Implementation is in- correct, incomplete, or results in an unexpected Error . Merge-Sort imple- mentation Implementation is cor- rect Implementation is par- tially correct. Implementation is in- correct, incomplete, or results in an unexpected Error . Quick-Sort with 1st element pivot implementation Implementation is cor- rect Implementation is par- tially correct. Implementation is in- correct, incomplete, or results in an unexpected Error . Quick-Sort with random element pivot implemen- tation Implementation is cor- rect Implementation is par- tially correct. Implementation is in- correct, incomplete, or results in an unexpected Error . Book Implemen- tation Implementation is cor- rect Implementation is par- tially correct. Implementation is in- correct, incomplete, or results in an unexpected Error . BookStore Sys- tem Methods Implementation is cor- rect Implementation is par- tially correct. Implementation is in- correct, incomplete, or results in an unexpected Error . Additional Main Functionalities Implementation is cor- rect Implementation is par- tially correct. Implementation is in- correct, incomplete, or results in an unexpected Error . Discussion on comparison of running times Discussion is thorough and demonstrates satisfactory under- standing of run-time efficiency differences between different data structures. Discussion is thorough but understanding of run-time efficiency differences between dif- ferent data structures is incorrect. Discussion does not in- corporate run-time effi- ciency discussion. Page 7 of 7