0% found this document useful (0 votes)
8 views17 pages

36 BigO Sort

The document reviews key Big O notations relevant to computing, including O(1), O(log n), O(n), O(n log n), and O(n^2), along with examples of algorithms corresponding to each. It discusses sorting algorithms such as Bubble Sort, Selection Sort, and Merge Sort, detailing their time complexities and operational steps. Additionally, it mentions Python's Timsort, a hybrid sorting algorithm that combines aspects of merge sort and insertion sort.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views17 pages

36 BigO Sort

The document reviews key Big O notations relevant to computing, including O(1), O(log n), O(n), O(n log n), and O(n^2), along with examples of algorithms corresponding to each. It discusses sorting algorithms such as Bubble Sort, Selection Sort, and Merge Sort, detailing their time complexities and operational steps. Additionally, it mentions Python's Timsort, a hybrid sorting algorithm that combines aspects of merge sort and insertion sort.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

CS1301- Intro to Computing

Day 36
Review: Big O’s you need to know
• O(1) – constant time:
• the program always takes the same of time, regardless of the amount of data it’s working
on. example – looking for the smallest item in a sorted list
• O(log n) – logarithmic time:
• the program takes log base 2 of the number of items there are in order to work – example
guess a number within a range. An algorithm that reduces the size of work at each step.
• O(n) – linear time:
• the program get slower in proportion to the amount of data elements n – searching for
something by looking at all the items
• O(n * log(n)) - loglinear time:
• the program gets slower as a bit bigger than linear growth in n – for each item, must go
through log of the items – a loop within a loop where one loop only has log n occurrences
• O(n2) – quadratic time:
• the program gets slower as a square of the amount of data elements n – for each item, must
look at all the other items each time – a loop within a loop
Fall 2024 2
Review: Big O’s you need to know
• O(1): Finding the minimum value of a sorted list
• O(log n): Binary Search of a sorted list – divide and conquer
• O(n): Sequential Search – look at all words in dictionary
• O(n log n): Merge Sort (recursive)
• O(n2): Bubble Sort, Selection Sort
• Measuring:
• Additive Property: O(m) + O(n) = O(max(n , m)) if loops are separate
• Multiplicative Property: O(m) * O(n) if loops are nested

Big-O notation in 5 minutes by Michael Sambol


(https://youtu.be/__vX2sjlpXU)

Fall 2024 3
Review: Big O’s you need to know
• Sequential Search (not efficient) – O(n)
• An algorithm that searching for something by looking at all the
items

• Binary Search (efficient) – O(log n)


• Must start with a sorted list
• Divide and conquer
• An algorithm that cuts the size of work in half at each step.

Fall 2024 4
Miniquiz

Fall 2024 5
Sorting Algorithms you need to know
• Bubble Sort
• O(n2)
• Compare two adjacent elements, if the first greater than the second, then swap
them and continue until there’s nothing left to search.

• Selection Sort
• O(n2)
• Search for the smallest element in the unsorted portion of the list during each pass.
Then swap this smallest element with the element at the current position (which is
typically the end of the sorted portion).

• Merge Sort
• O(n log n)
• It divides an array in two halves, calls itself for the two halves and then merges the
subarrays back together in sorted order.

Fall 2024 6
Bubble Sort O(n2)
• Compare two adjacent elements, if the first is greater than the
second, then swap them and continue.
Given an array of length n
• Set index to 0
• While index is not equal to n
• compare elements index and index + 1
• If index is greater than index + 1 , then swap the elements
• increment index by 1

• Properties
• O(n2) operations and swaps

Fall 2024 7
Example of Bubble Sort O(n2)
• Compare two adjacent elements, if the first greater than the second, then swap elements and
continue.
• Animation: https://csvistool.com/BubbleSort

7 2 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8

2 7 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8

2 7 8 5 4 2 5 7 4 8 2 4 5 7 8 (done)

2 7 5 8 4 2 5 4 7 8

2 7 5 4 8

Fall 2024 8
Sample Bubble Sort exam question O(n2)
Show the steps of a Bubble Sort on this list: [8, 9, 10, 3, 11, 15, 2, 99]
Show the list after each complete pass, and highlight the items that can be
considered already sorted.
Initial List : [8, 9, 10, 3, 11, 15, 2, 99]
List after Pass 1: [8, 9, 3, 10, 11, 2, 15, 99]
List after Pass 2: [8, 3, 9, 10, 2, 11, 15, 99]
List after Pass 3: [3, 8, 9, 2, 10, 11, 15, 99]
List after Pass 4: [3, 8, 2, 9, 10, 11, 15, 99]
List after Pass 5: [3, 2, 8, 9, 10, 11, 15, 99]
List after Pass 6: [2, 3, 8, 9, 10, 11, 15, 99]
List after Pass 7: [2, 3, 8, 9, 10, 11, 15, 99]
Final Sorted List: [2, 3, 8, 9, 10, 11, 15, 99], The sorting is complete after 7 passes.

Fall 2024 9
Selection Sort O(n2)
• Search for the smallest element in the unsorted portion of the list
during each pass. Then swap this smallest element with the element
at the current position.
Given an array of length n,
• Search elements from index 0 to ( n-i ) (where ( i ) is the current pass).
• Select the smallest element found in that range.
• Swap it with the element at index ( n-i ).
• Repeat this process, reducing the range of the unsorted portion until the
entire list is sorted.

• Properties
• O(n2) operations and swaps

Fall 2024 10
Example of Selection Sort O(n2)
7 2 8 5 4
Search for the smallest element in the unsorted portion of the list during each
pass. Then swap this smallest element with the element at the current position
(ascending order).
• Animation: https://csvistool.com/SelectionSort 2 7 8 5 4

• Search elements 0 through n-1 and select the smallest


• Swap it with the element in location 0 2 4 8 5 7
• Search elements 1 through n-1 and select the smallest
• Swap it with the element in location 1
• Search elements 2 through n-1 and select the smallest 2 4 5 8 7
• Swap it with the element in location 2
• Search elements 3 through n-1 and select the smallest
• Swap it with the element in location 3 2 4 5 7 8
• Stop search element n-1 has been reached.

Fall 2024 11
Sample Selection Sort Question O(n2)
Show the steps of a Selection Sort on this list: [8, 9, 10, 3, 11, 15, 2, 99]
Show the list after each complete pass, and highlight the items that can be
considered already sorted.

Initial List: [8, 9, 10, 3, 11, 15, 2, 99]


List after Pass 1: [2, 9, 10, 3, 11, 15, 8, 99]
List after Pass 2: [2, 3, 10, 9, 11, 15, 8, 99]
List after Pass 3: [2, 3, 8, 9, 11, 15, 10, 99]
List after Pass 4: [2, 3, 8, 9, 11, 15, 10, 99]
List after Pass 5: [2, 3, 8, 9, 10, 15, 11, 99]
List after Pass 6: [2, 3, 8, 9, 10, 11, 15, 99]
List after Pass 7: [2, 3, 8, 9, 10, 11, 15, 99]
Final Sorted List: [2, 3, 8, 9, 10, 11, 15, 99] The sorting is complete after 7 passes.

Fall 2024 12
Merge Sort O(n log n)
It works by recursively dividing the array into halves, sorting each half,
and then merging the sorted halves back together.
Given an array of length n,
• mergeSort: This function checks if the array length is 1 or less (base case). If
so, it returns the array as it is already sorted. Otherwise, it divides the array
into two halves and recursively calls itself on each half.
• merge: This function takes two sorted arrays (left and right) and merges them
into a single sorted array. It compares the elements of both arrays and
appends the smaller element to the result array until all elements from both
arrays are merged.

• Properties
• O(n log n) operations and swaps

Fall 2024 13
Example of Merge Sort O(n log n)
It divides an array in two halves, calls itself for the two halves and then merges the subarrays back together in sorted
order..
• Animation: https://csvistool.com/MergeSort

1. Start with the array: [7, 2, 8, 5, 4]


2. Split the array into halves: [7, 2] | [8, 5, 4]
3. Split the left half: [7] | [2] |
4. Merge: [7] and [2] → [2, 7] |
5. Split the right half: | [8] | [5, 4]
6. Split [5, 4]: | | [5] | [4]
7. Merge: | | [5] and [4] → [4, 5]
8. Merge: | [8] and [4, 5] → [4, 5, 8]
9. Merge: [2, 7] and [4, 5, 8]: 4 → [2]
: 7 with 4 → [2, 4]
: 7 with 5 → [2, 4, 5]
: 7 with 8 → [2, 4, 5, 7, 8]
10. Final sorted array: [2, 4, 5, 7, 8]

Fall 2024 14
Sample Merge Sort question O(n log n)
Diagram the steps that would occur if merge sort was used to sort this
list: [8, 9, 10, 3, 11, 15, 2, 99]
Initial List: [8, 9, 10, 3, 11, 15, 2, 99]
First Split: [8, 9, 10, 3] [11, 15, 2, 99]
Second Split: Left: [8, 9] Right: [10, 3] Left: [11, 15] Right: [2, 99]
Third Split: Left: Right: Left: Right: Left: Right: Left: Right:
[8] [9] [10] [3] [11] [15] [2] [99]
Merging: [8, 9] [3, 10] [11, 15] [2, 99]
Next Merging: [3, 8, 9, 10] [2, 11, 15, 99]
Final Merge: [2, 3, 8, 9, 10, 11, 15, 99]
Final Sorted List: [2, 3, 8, 9, 10, 11, 15, 99]

Fall 2024 15
Summary
• Bubble Sort and many other sorts with 2 nested loops are
O(n2)
• We can do much better than this with somewhat more
complicated sorting algorithms (usually recursive)
• Merge sort and Quick sort are examples of faster recursive
sorts that have big O notation of O(n log n)
• Sorting animations:
http://math.hws.edu/eck/js/sorting/xSortLab.html

Fall 2024 16
Which sort does Python’s .sort method use?
Python uses an algorithm called Timsort: Timsort is a hybrid sorting
algorithm, derived from merge sort and insertion sort, designed to
perform well on many kinds of real-world data. It was invented by Tim
Peters in 2002 for use in the Python programming language.

Fall 2024 17

You might also like