Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. # Lab 3: Basic Sorting Algorithms
  2.  
  3. ## Introduction
  4.  
  5. To become more familiar with basic sorting algorithms and input types, this week you will be testing some algorithms you write yourselves.
  6. More specifically, you will be implementing the Insertion and Bubble Sort algorithms you learned about in class, and benchmarking them against the standard Python library's sorting algorithm.
  7.  
  8. **Be sure to read this lab's instructions extra carefully**, as you may be unfamiliar with some of the concepts.
  9.  
  10. Other things to note about this lab:
  11.  
  12. 1. The content of the lists passed into each of your algorithms should be exactly the same (not just the same length).
  13. 2. If your sorting algorithms work **in-place**, you must be sure to avoid *accidentally* passing in an already sorted list.
  14. 3. (**Important**) You **may not** use the slides or any online resources to guide your implementation, instead you should come up with the algorithms based on your understanding of how the insertion and bubble sort algorithms work.
  15.  
  16. ## Inputs
  17.  
  18. The performance of an algorithm can depend greatly on the type of input it receives.
  19. In fact, most professional sorting algorithms are *hybrid* algorithms, which use different routines depending on the size of the input.
  20. As you've seen, even algorithms that grow in complexity faster than others can be more effective on small input sizes.
  21. Along with input size, as a programmer, you should also consider what state the input will be in on arrival; i.e. is there a chance it is already sorted? Partially sorted?
  22.  
  23. Generally, these different scenarios will be referred to as **best case**, **average case**, and **worst case**.
  24. To explore this topic further, today you will examine what happens to the runtime of your sorting algorithms given a variety of inputs:
  25.  
  26. - Random Unsorted List
  27. - Already Sorted List
  28. - Reverse Sorted List
  29. - Partially Sorted List
  30.  
  31. Each of the above list types is exactly what you would expect (Reverse Sorted List are lists in reverse order, for example) except, perhaps, for partially sorted lists.
  32. A partially sorted list has a definition which is a bit more complicated, but for the purpose of this lab a partially sorted list is one which requires **at most** n swaps to sort, where n is the length of the list.
  33. In other words, the number of *inversions* in the list is at most n.
  34.  
  35. ## Starter Code
  36.  
  37. If you take a look into the starter code for the lab, you will find a lot of the code has already been written!
  38. To make this a bit less intimidating, here is a rough breakdown of the code:
  39.  
  40. ### Sorting Functions
  41.  
  42. The functions used for sorting in this lab are as follows:
  43.  
  44. `def int_sort(arr: list) -> list` Performs insertion sort on the given list, returning a sorted list.
  45.  
  46. `def bub_sort(arr: list) -> list` Performs bubble sort on the given list, returning a sorted list.
  47.  
  48. `void python_sort(arr: list) -> list` Uses the standard Python sorting algorithm (Timsort), returning a sorted list.
  49.  
  50. ### Input Generating Functions
  51.  
  52. The functions for creating lists of different types are as follows:
  53.  
  54. `def gen_list_unsorted(size: int) -> list` Creates and returns a random, unsorted list.
  55.  
  56. `def gen_list_sorted(size: int) -> list` Creates and and returns a random, sorted list.
  57.  
  58. `def gen_list_reversed(size: int) -> list` Creates and returns a list in sorted, reversed order.
  59.  
  60. `def gen_list_partial_sorted(size: int) -> list` Creates and returns a partially sorted list.
  61.  
  62. Here, `size` is the length of the list to be generated, but it also specifies the bounds for the random values in the list (`[-size, size]`).
  63.  
  64. > All of the functions in this section are already completed, and ready to be used.
  65.  
  66. ### Unit Testing
  67.  
  68. Also within the starter code, is a testing class (`class Tests`), which you can use to test your code.
  69.  
  70. > Adding unit tests is not mandatory, but it is highly encouraged.
  71.  
  72. ### `main()`
  73.  
  74. Inside of main we have provided you with a list of algorithm names, a list of input types, the input size you should use, and a dictionary that allows you to call the list generating functions a bit more concisely.
  75.  
  76. ## Instructions
  77.  
  78. For each sorting algorithm, do the following:
  79.  
  80. 1. Consider the problem.
  81. 2. Work through the algorithm on paper.
  82. 3. Write pseudocode.
  83. 4. [Recommended] Implement unit tests.
  84. 5. Implement the algorithm to pass the unit tests.
  85.  
  86. Once you've written all three of the sorting functions, you can begin working on the `main` function.
  87.  
  88. 1. Consider the table you are required to make.
  89. 2. Write pseudocode to solve the problem.
  90. 3. Check, does your pseudocode use the **exact** same input list for each algorithm? (remember: [1, 3, 2, 4] != [1, 2, 3, 4]; Do not accidentally use an already sorted array if the desired input type is not sorted!)
  91. 4. Implement your pseudocode.
  92. 5. Test your implementation, revise your code as necessary.
  93.  
  94. ## Questions
  95.  
  96. Once all of your code is complete, answer the following questions.
  97.  
  98. ### Question 1
  99.  
  100. On which type(s) of input did insertion sort perform best? On which type(s) did it perform worst?
  101.  
  102. ### Question 2
  103.  
  104. What was the overall difference between insertion sort's best input and its worst input? (no need for numbers on this question, just describe the difference the two input types made).
  105.  
  106. ### Question 3
  107.  
  108. On which type(s) of input did bubble sort perform best? On which type(s) did it perform worst?
  109.  
  110. ### Question 4
  111.  
  112. What was the overall difference between bubble sort's best input and its worst input?
  113.  
  114. ### Question 5
  115.  
  116. How do the insertion sort and bubble sort compare with the Python's built-in sorting function?
  117.  
  118. ## Submission
  119.  
  120. You will submit 2 text files named lab.py and lab.txt on Gradescope by the end of your lab session.
  121.  
  122. Please ensure your files contain the following:
  123.  
  124. 1. lab.py should contain your entire source code
  125. 2. lab.txt should contain the table (running times) generated by your program, and the answers to the above questions.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement