Lecture 14 - Algorithms and Sorting


As usual, create a directory to hold today's activities:

$ mkdir ~/cs170/labs/lab14
$ cd ~/cs170/labs/lab14

Comparing Algorithms and Sorting

In computer science, we tend to come across the same (or at least very similar) problems time and time again. Instead of coming up with a unique solution every time, we can apply a well-defined sequence of events, known as an algorithm, to the problem at hand.

There is usually more than one way to skin a cat, however. So for a given problem there might be multiple algorithms that result in the same solution. So at some point, we need to be able to compare these algorithms. That is what we are going to start with today.


Lab Activity 1
Insertion Sort

As you saw in the textbook, insertion sort is a fairly straight forward algorithm that you are probably fairly familiar with. You've seen how the algorithm works, so now it is time to actually implement the algorithm.

Details

Create a file called sorting.py, which you will store all of the sorting algorithms you write this semester in. Create a function called insertion_sort(a_list), which takes a list of integers, and returns a copy of a_list in sorted order. You should implement the insertion sort algorithm discussed in the textbook.

Make sure you test your function on several lists of various sizes, just to make sure that it works correctly.

Example

>>> my_list = [5, 4, 3, 2, 1]
>>> my_sorted_list = insertion_sort(my_list)
>>> print(my_list)
[5, 4, 3, 2, 1]
>>> print(my_sorted_list)
[1, 2, 3, 4, 5]

Hint

  • You will need to nest for loops for this code to work. Both of these should use indicies of the list, as opposed to iterating through the elements.

  • Your external for loop is going to identify not only the end of the sorted segment of the list, but also the item that you are currently trying to place into the sorted segment.

    Your internal for loop will loop through the sorted segment, looking for where the item specified by the external for loop should go.

  • You will need to write some mechanism that will shift elements down in the list, without losing any information. The safest way to do this is to store the integer in the last position of the list, and work backwards copying from the back of the list to the front.

 

Challenge

There is one thing about Python's list slicing syntax that we have not covered yet, since it does not have a lot of real uses. However, it is incredibly useful for shifting subsets of elements, so insertion sort is actually the perfect time to pick it up.

You can use list slicing to assign lists to a subset of a list. If you use the slicing syntax on the left side of the assignment operator, you place an entire list into some subset of the current list! So, instead of needing a loop for your copying, you can simply use the slicing syntax!

>>> my_list = [5, 4, 3, 2, 1]
>>> shift_element = my_list[3]
>>> print(shift_element)
2
>>> my_list[1:4] = my_list[0:3]
>>> print(my_list)
[5, 5, 4, 3, 1]
>>> my_list[0] = shift_element
>>> print(my_list)
[2, 5, 4, 3, 1]

Lab Assignment 14
Selection Sort

Another sorting algorithm is known as selection sort. Both insertion sort and selection sort operate in the same manner. They both keep a sorted sub-list of elements. The key difference between the two is the contents of this sub-list. In insertion sort algorithm sorts the first n elements of the original list. Selection sort finds the smallest n elements of the entire list.

Details

In your sorting.py file, create a function called selection_sort(a_list). This function takes a list of integers (or really anything that can be compared with the comparison operators), and returns a sorted copy of a_list using the selection sort algorithm.

The selection sort algorithm starts by finding the minimum element of the entire list. This minimum element is swapped with the element at the 0th index of the list. Then, the second smallest element is found, and swapped with the element at the 1st index of the list. This process continues for every location in the list.

Example

>>> my_list = [5, 4, 3, 2, 1]
>>> my_sorted_list = selection_sort(my_list)
>>> print(my_list)
[5, 4, 3, 2, 1]
>>> print(my_sorted_list)
[1, 2, 3, 4, 5]

Hint

  • You will need a for loop that iterates through the indicies of a_list. This for loop will identify the location you are looking for the correct element for.

    Inside this for loop, you need another for loop which will identify the minimum element of the list. This is the element you will be placing in the index identified by the first for loop.

  • After you have moved the minimum element to the 0th location of the list, you can find the 2nd smallest element by finding the minimum element of the now "unsorted" sub-list; The list of elements from index 1 to the end of the list. You can continute to do this process for every iteration.

 

Challenge

You have now implemented two different sorting algorithms. However, you might have guessed that both of them have the same Big-Oh class. Let's think of another algorithm. Maybe it has a better Big-Oh class.

Based on the law of probability, if you shuffle a list enough you are bound to eventually get a sorted version of the list. This is the key notion behind bogo sort. In bogo sort, you simply randomly shuffle the list until you end up with the sorted copy.

Implement bogo sort the the same way you implemented selection and insertion sort. Make sure you adequately test your code. In the comments of your file, describe how you think bogo sort compares to selection and insertion sorts, based on their Big-Oh classes. You do not have to define the actual Big-Oh class of bogo, but it might be worth while to have an intuition for what it might be.


Submission

When you have finished, create a tar file of your lab14 directory. To create a tar file, execute the following commands:

cd ~/cs170/labs
tar czvf lab14.tgz lab14/

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 14. Make sure you include a header listing the authors of the file.



Last modified: Mon Feb 17 09:30:38 EST 2014