0% found this document useful (0 votes)
54 views25 pages

More Bi Go

This document discusses sorting algorithms and Big O notation. It provides examples of common sorting algorithms like bubble sort and quicksort. For bubble sort, it explains the basic steps, provides pseudocode, and analyzes it as O(n2). For quicksort, it explains the divide and conquer approach, provides pseudocode, and analyzes the worst case as O(n2). It also discusses different types of algorithm analyses like average complexity and memory complexity.

Uploaded by

Ices Matara
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views25 pages

More Bi Go

This document discusses sorting algorithms and Big O notation. It provides examples of common sorting algorithms like bubble sort and quicksort. For bubble sort, it explains the basic steps, provides pseudocode, and analyzes it as O(n2). For quicksort, it explains the divide and conquer approach, provides pseudocode, and analyzes the worst case as O(n2). It also discusses different types of algorithm analyses like average complexity and memory complexity.

Uploaded by

Ices Matara
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

More Big-O Notation, Sorting Algorithms, Etc.

December 1, 2009

Sorting Algorithms
Sorting: putting a list of items in-order
Usually: numerical or lexicographical

Long time studied array of algorithms/computer science


Silver Bullet?
Simple Efficient

From Last Time.


Big-O Notation
Think: How much work does this algorithm need to do? A way to describe the efficiency of a search Look at the worst case analysis of an algorithm

From Last Time


Suggestions for determining Big-O
Start by looking at the number of nested loops Look for recursion

Remember:
Length of a list (which could be stored in an Array, ArrayList, etc) is usually represented by the lowercase letter n
What if I know the number of elements?
Still do your analysis in terms of n.

Why Do We Care?
What is Analyzing Algorithms (and BigO notation) good for?
Comparison of algorithms Advertising a new algorithm Making decisions on which algorithm to use Determining the scalability of an algorithm

Common Types
O(1) O(log n) O(n) Constant Logarithmic Linear

O(n2)
O(n3)

Quadratic
Cubic

Random Thought
It is important when reporting the Big-O to use proper Big-O Notation If the Big-O of an algorithm is linear,
Then you should report the Big-O as O(n) You must include the letter O and the () Remember: n is not the same as O(n)

Why?
It is the notation that has always been used There are other symbols that are used to mean slightly different thingsbut we will not get into that in this course Plus, I am sure you want to get it right on your final exam

What is the Big-O of:


int n = 20; double[] numbers = new double[n]; numbers = fillArray(); int sum = 0; for(int x=0; x<n; x++) { sum = sum + numbers[x]; }

And the Big-O is:


First look at the loop: for(int x=0; x<n; x++) { sum = sum + numbers[x]; } How much work does this algorithm do?
It goes through all the elements in the array. There are n total elements. Hence: Big-O = O(n)

What is the Big-O of:


int n = 20; double[] numbers = new double[n]; numbers = fillArray(); int sum = 0; for(int x=0; x<n; x++) { sum = sum + numbers[x]; }

for(int x = 0; x<n; x++) { sum = sum + numbers[x]; }

And the Big-O is:


What is the difference between this new code and the old code?
The array of numbers is gone through completely twice. There are n elements in the array Therefore, 2 * n amount of work. What is the Big-O?
Big-O = O(2n) = O(n) Coefficients are dropped

More on Big-O Notation


Coefficients are dropped:
You would not report a Big-O of O(2n). Instead, you would say it was O(n) Why? We want the order of the algorithm for comparison purposes

More on Big-O Notation


The largest terms dominate:
If you calculated that an algorithm took n2 + 3n + 1 of work, the proper and correct Big-O notation is O(n2) The O(n2) is the dominate term and the 3n + 1 part is considered negligible

Stable Sort
A stable sort is a sort algorithm that maintains the relative order of data items in the list In other words, it there are multiple data items in the list that have the same value, then the sort completes these data items are in the same order as the original
8, 1, 5, 2, 3, 3, 4, 3, 6, 7 1, 2, 3, 3, 3, 4, 5, 6, 7, 8

Comparison Sorts
Sort algorithms that mainly use comparison operators (e.g., less than/greater than) Common Comparison Sorts:
Insertion Sort Selection Sort Merge Sort Bubble Sort Quick Sort

Bubble Sort
Another simple comparison sorting algorithm The basics:
Repeatedly walkthrough a list of data that needs to be sorted compare adjacent data items and swapping (if needed) Algorithm stops: when a walkthrough of the list does not require any swaps

Naming: Elements bubble there way to the top


(I dont name, I just teach them)

Bubble Sort
Example:
Start: 5, 1, 3, 6, 4
1, 5, 3, 6, 4 1, 3, 5, 6, 4 1, 3, 5, 6, 4 (no change) 1, 3, 5, 4, 6 1, 3, 5, 4, 6 (no change) 1, 3, 5, 4, 6 (no change) 1, 3, 4, 5, 6 1, 3, 4, 5, 6 (no change)

It will check the array 1 last time and there will be no swaps, so the algorithm is done

Bubble Sort Pseudocode


Given: array A of data to be sorted Do{ IsSwap = false; for(int x=0; x<A.length-1; x++) { if(A[x] > A[x+1]) { swap(A[x], A[x+1]); IsSwap = true; }//end if }//end for loop }While(IsSwap == true)

Big-O For Bubble Sort


How many loops?
Two nested loops
For Loop: n-1 times Do While Loop: Until swaps stop - but when is that?
Remember we are dealing with worst case! What is the worst case? 987654321 Exact opposite of sorted Therefore, n, number of swaps

The Do While controls the number of times the For Loop happens. Therefore, worst case, amount of work for the algorithm: n * (n-1)
n * (n-1) = n2 - n

Therefore, O(n2)

Advantages of Bubble Sort


This algorithm can easily identify any list of data items already in the proper order
Surprisingly, not all sort algorithms are written with this feature in mind
One of the reasons there is yet another O(n2) algorithm - each is designed slightly differently

This algorithm would also work well for data that is close to being in the proper order

Quick Sort
A comparison sorting algorithm using a divide and conquer technique The basics:
Pick an element to be the pivot Partition Operation
Create sub-lists for above and below pivot Place pivot between sub-lists

Recursively sort each sub-lists

Quick Sort Example


Original List: 3, 9, 8, 5, 2, 1, 7, 6, 4 Select 4 as pivot
Sub-list Less: 3, 2, 1 Sub-list More: 9, 8,5, 7, 6

Sort Sub-list Less: 1, 2, 3 Sort Sub-list More: 5, 6, 7, 8, 9

Quick Sort Pseudocode


Given:
ArrayList A of data to be sorted ArrayList SubLess and ArrayList SubMore

QuickSort(A) {
Select pivot point (I used: pivot = A.Size-1) for(int x=0; x<A.Size-1; x++) { if(A.get(x) < pivot) SubLess.add(A.get(x)) else SubMore.add(A.get(x)) }//end of for loop Sorted = Concatenate(QuickSort(SubLess, QuickSort(SubMore))

}//end of QuickSort

Big-O For Quick Sort


Loops? One For Loop: n-1 Recursion? Yes: QuickSort both ArrayLists SubLess and SubMore
Worst case: pick the pivot that only creates sublists of size 1 and n-1
This cases the # of recursive calls to be n-1 number of times Total number of calls to Quicksort: n-1 + 1 = n Total work = n * (n-1) = n2 - n

O(n2)

Just FYI: average case: n log n


Why? Average tree depth: log n

Other Types of Complexity


Average Complexity
Use random inputs Studying what happens most of the time

Memory Complexity
Studying how much memory is needed beyond the list of items to complete the search
Most common sorts have O(1) memory complexity (space for swaps, etc)

You might also like