Barron's: AP Computer Science A: Chapter 9: Sorting and Searching

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

Binary Search: n is a power of two case

*Case 1:* The key is at either of the endpoints of the array. These are worst cases in which the number of comparisons is equal to the exponent plus one *Case 2:* The key is not the array, and is also less than a[0] or greater than a[n-1] then the number of comparisons is the exponent plus one *Case 3:* The key is not in the array, and its value lies between a[0] and a[n-1] then equal to the exponent

Binary Search: n not a power of two case

*Case:* Round n up to the nearest power of 2. The number of comparisons in the worst case is equal to the exponent. It represents the cases in which the key is at either of the endpoints of the array, or not in the array.

Selection Sort

Find the smallest element in the array and exchange it with a[0], the first element. Now find the smallest element in the subarray a[1]...a[n-1] and swap it with a[1], the second element in the array. Continue this process →the array is sorted after n-1 passes →after the nth pass, the first k elements are sorted

Quick Sort

If At Least Two Elements (Recursive Step): Partition the array Quick sort the left subarray Quick sort the right subarray Marker up at 0 Marker down at n-1 Move the up marker until a value less than the pivot is found, or down equals up Move the down marker until a value greater than the pivot is found, or down equals up Swap a[up] and a[down] Continue until down equals up; this s the pivot position so swap a[0] with a[pivot] →fastest sort for large n →if pivot is too close to the ends it will take awhile →not on AP exam, don't worry

Merge Sort

If More Than One Element (Recursive Step): Break the array into two halves Merge sort the left half Merge sort the right half Merge the two subarrays If One Element (Recursive Base Case): Sort two adjacent values Merge →needs a temporary array →always will give the same run time for a set of numbers no matter the order

Binary Search

If the elements are in a *SORTED* array, find the midpoint and see if the key is greater, less, or equals to the value; repeat in the half that the key will fall or return the key if it has been found →to find w, the worst case, round n up to the next power of two and take the exponent →if n is a power of two, round n up to the next power of two and take the exponent plus one →if the key is not in the list, it will be the next power of two and take the exponent if in the first half range, OR the next power of two and take the exponent plus one if in the second half range →more efficient than a sequential search

Sequential Search

Starts at the first element and compares the key to each element in turn until the key is found or there are no more elements to examine in the list

Insertion Sort

The idea of insertion sort is to move elements from the unsorted list to the sorted list one at a time; as each item is moved, it is inserted into its correct position in the sorted list →the array is sorted after n-1 passes →after the nth pass, the first k elements are sorted with respect to each other →best for small n (around 7)

sorting classes

make sorting classes with an array as a private instance variable with a constructor that assigns the user's array to the private array variable →to sort objects use <<compareTo>>


Kaugnay na mga set ng pag-aaral

Globalisation Readings/Key Terms

View Set

PLS 21V Midterm Study Guide Questions

View Set

Black Codes, Jim Crow Laws, and Segregation

View Set

Chapter 28: Growth and Development of the School-Age Child

View Set

Posi 2320 civil liberties: Protecting individual rights

View Set

Prepu Reduction of risk potential

View Set