Week 11
--------------------------------------------------------------------------------------
Sorting and Effeciency
Here are the Run Times for Quick
Sort versus Bubble Sort:
Quick sort works by choosing a
pivot value and splits the list based on that value and sorts the list in a
divide and conquer fashion. Bubble sort on the other hand, makes multiple passes through a list as it compares
adjacent items and exchanges those that are out of order. Each pass through the
list places the next largest value in its proper place.
Random list generated
FOR 1000 ITEMS:
Time in Seconds
--------------------------------------------------------------------------------
Unsorted List
0.006000995635986328 - quick sort, pivot = L[0]
0.18508195877075195 – bubble sort
Sorted List
0.1340789794921875 - quick sort, pivot = L[0]
0.09106993675231934 – bubble sort
As Shown above, quick sort is much more efficient
than bubble sort when it comes to sorting an unsorted list. The efficiency of
quick sort can be denoted in big-oh notation as O(nlogn),
while bubble sort is O(n^2).
Though quick sort is more efficient than bubble sort when it
comes to an unsorted list, it is less efficient using an already sorted list.
This makes sense because of the pivot we have chosen for quick sort. In this
case, the pivot is L[0] so the split points are not in
the middle and are very skewed to the left, causing uneven division.
Parham
Taher