(WORK IN PROGRESS) Big O Notation and Sorting Algorithms | Comp Sci AP

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Order these 7 Big O run times from longest to shortest: - O(1) - O(log(n)) - O(constant^n) - O(n*log(n)) - O(n) - O(n!) - O(n^constant)

1. O(n!) 2. O(constant^n) 3. O(n^constant) 4. O(n*log(n)) 5. O(n) 6. O(log(n)) 7. O(1)

What is Big O notation?

Big O notation is used to describe the performance or complexity of an algorithm. Big O specifically describes the worst-case scenario, and can be used to describe the execution time required or the space used (e.g. in memory or on disk) by an algorithm.

What search algorithm is this? private int search(int [] stuff, int val ) { int bot= 0, top = stuff.length-1; while(bot<=top) { int middle = (bot + top) / 2; if (stuff[middle] == val) return middle; else if (stuff[middle] > val) top = middle-1; else bot = middle+1; } return -1; }

BinarySearch

Arrays.sort() has what Big O notation? Collections.sort()?

For primitive data types (just a reminder; they're boolean , byte , char , short , int , long , float and double), it will use

What does Arrays.binarySearch() return?

If it finds the value, it will return the index it found it at. Otherwise it will return -1 + -location where it should be.

Describe SelectionSort.

Method: Finds the lowest value in the entire array minus the sorted part and moves it to the front position. Takes one ENTIRE loop of the outer for loop to make ONE swap. Big O Notation: O(n^2) (Worst, Best, Average)

Describe LinearSearch.

Method: Looks at first element. "Is this my element? No...." Looks at second element. "Is this my element? No...." Etc.... Big O Notation: O(n) (Worst) O(1) (Best) O(n) (Average) This would be O(1/2n), but constants DO NOT MATTER!

Describe InsertionSort.

Method: Sorts the first part of a list, then brings in each subsequent item into the already sorted part. Big O Notation: O(n^2) (Worst, Average) O(n) (Best, if already sorted)

Describe BubbleSort.

Method: This algorithm looks through a list and compares adjacent values. If they are in order, it lets them be, but otherwise, it swaps them. THIS IS EXTREMELY INEFFICIENT! PLEASE DO NOT USE! Big O Notation: O(n^2) (Worst) O(n), although must be told to exit early once it has found the list is sorted for this to be the case. (If BubbleSort has a boolean or variable that keeps track of if something is changed, then its best case is O(n).) (Best) O(n^2) (Average)

Describe MergeSort.

Method: This will split the array up into many smaller arrays, each with one element. Since these arrays are already sorted, it can splice them together. Then it steps back one more layer and splices the next set of arrays together, making sure to keep them sorted. Repeat until all array fragments are condensed. Big O Notation: O(n*log (n)) (Worst, Best, Average) N COMES FROM ____ LOG N COMES FROM ____

Describe BinarySearch.

Method: Takes a SORTED array, cuts it in half, determines which side the element it's looking for should fall on and seeks it there. Big O Notation: O(log (n)) (Worst) O(1) (Best) O(log (n)) (Average)

What is the difference between QuickSort and MergeSort?

QuickSort picks a random partition (usually recursively), while MergeSort will pick the halfway point. Merge/QuickSort both usually use recursion. QuickSort Worst Case O(n^2) Best Case O(log n) NOTE TO SELF: STUDY HECCING ARRAYLISTS AND COLLECTIONS. (7) STUDY PREVIOUS UNITS. (4) STUDY RECURSION. (5) STUDY BIG O. (4) BINARY AND LINEAR SEARCH WILL NEED TO BE WRITTEN ON THE TEST!!!!!!! Look at Insertion, Bubble, Selection, Merge, Quick

What does the method Comparable.compareTo(O o) do?

When called on an instance of a class that implements Comparable, it will return a negative integer if the object o is less than the instance, 0 if they are the same, and a positive integer if o is greater.


Kaugnay na mga set ng pag-aaral

Health : 2. Physical Health (BENEFITS OF PROPER NUTRITION)

View Set

Microeconomics Exam 1 - Chapter 3

View Set

temporary compromise (unit: rising tensions)

View Set

Psyc 201-Chapter 13: Inferential Statistics

View Set