Docsity

Simple Sorts - Data Structures - Lab, Exercises of Data Structures and Algorithms

Toggle sidebar
bg1
Objectives: You will gain experience:
๎˜get a feel for simple sorts: selection, bubble, and insertion sorts
๎˜get a feel for advanced sorts: heap, quick, and merge sorts
To start the lab: Download and unzip the file:
The lab8.zip file you downloaded and extracted contains the following sorting algorithms which all sort in ascending
order (i.e., from smallest to largest):
๎˜bubbleSort.py - bubble sort code which does not check if it can stop early
๎˜bubbleSortB.py - bubble sort code which stops early if no swapping is needed during a scan of the unsorted part
๎˜insertionSort.py - the insertion sort
๎˜selectionSort.py - the selection sort code we developed in class
Each program runs the sorting algorithm several time with different initial orderings of 10,000 list items. The initial
orderings of items are: descending order, ascending order, random order, and random order again to check for
consistence. Complete the following timings by running the each program.
selectionSort.py
insertionSort.py
bubbleSortB.py
bubbleSort.py
Random order 2Random order 1AscendingDescending
Initial Ordering of ItemsType of sorting algorithm
Timings of Sorting Algorithms on 10,000 items (seconds)
Study the code and answer the following questions about the sorting algorithms:
a) Why does the bubbleSort algorithm take less time on the ascending ordered list than the descending ordered list?
b) Why does the bubbleSortB algorithm take A LOT less time on the ascending ordered list than the descending
ordered list?
c) Why does the insertionSort algorithm take A LOT less time on the ascending ordered list than the descending
ordered list?
d) Why does the insertionSort algorithm take less time on the descending ordered list than the bubbleSort algorithm
on the descending ordered list?
Data Structures (CS 1520) Lab 8 Name:___________________________
Lab 8 - 1
Docsity.com
pf3
pf4

Partial preview of the text

Download Simple Sorts - Data Structures - Lab and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Objectives: You will gain experience:  get a feel for simple sorts: selection, bubble, and insertion sorts  get a feel for advanced sorts: heap, quick, and merge sorts

To start the lab: Download and unzip the file: The lab8.zip file you downloaded and extracted contains the following sorting algorithms which all sort in ascending order (i.e., from smallest to largest):  bubbleSort.py - bubble sort code which does not check if it can stop early  bubbleSortB.py - bubble sort code which stops early if no swapping is needed during a scan of the unsorted part  insertionSort.py - the insertion sort  selectionSort.py - the selection sort code we developed in class

Each program runs the sorting algorithm several time with different initial orderings of 10,000 list items. The initial orderings of items are: descending order, ascending order, random order, and random order again to check for consistence. Complete the following timings by running the each program.

selectionSort.py

insertionSort.py

bubbleSortB.py

bubbleSort.py

Descending Ascending Random order 1 Random order 2

Type of sorting algorithm Initial Ordering of Items

Timings of Sorting Algorithms on 10,000 items (seconds)

Study the code and answer the following questions about the sorting algorithms: a) Why does the bubbleSort algorithm take less time on the ascending ordered list than the descending ordered list?

b) Why does the bubbleSortB algorithm take A LOT less time on the ascending ordered list than the descending ordered list?

c) Why does the insertionSort algorithm take A LOT less time on the ascending ordered list than the descending ordered list?

d) Why does the insertionSort algorithm take less time on the descending ordered list than the bubbleSort algorithm on the descending ordered list?

Lab 8 - 1

e) Why does the selectionSort algorithm take less time on the descending ordered list than the insertionSort algorithm on the descending ordered list?

After you have answered the above questions, raise your hand and explain your answers.

Part B: a) Complete the heap sort function in lab8/heapSort.py which contains the template for the heap sort algorithm discussed in class. Recall the steps of the algorithm:

Steps: 1. Create an empty heap

  1. Insert all n list items into heap
    1. delMin heap items back to list in sorted order

myList sorted list with n items

myList unsorted list with n items

heap with n items

b) Time the heap sorting algorithm using lab8/timeHeapSort.py on 100,000 random items, 200,000 random items, and 400,000 random items.

# Items Your Heap Sort Timing

c) Explain the O ( ) for your heap sort algorithm?

Lab 8 - 2

Part C: EXTRA CREDIT (You should work on completing HW #4 before work on extra credit below!!!)

a) Write (pencil-and-paper below) a variation of bubble sort that:  sorts in descending order (largest to smallest)  builds the sorted part on the left-hand side of the list, i.e.,

Sorted Part Unsorted Part

Inner loop scans from right to left across the unsorted part swapping adjacent items that are "out of order"

(Your code does NOT need to stop early if a scan of the unsorted part has no swaps)

def bubbleSortC(myList):

b) Implement and test your bubbleSortC code.

Lab 8 - 4