Counting the Cost Recall linear search & binary search Number of items Worst CaseExpected Case Number of probes (comparisons) LinearBinary Linear Binary NNlog 2 NN/2log 2 N
Algorithm to find the maximum Consider 8 numbers. abcdefgh max of a & b max of c & d max of e & f max of g & h max of a - d max of e - h max of a - h comparisons Total number of comparisons?
Sorting Algorithms Sorting algorithms rearrange items from smallest to largest (or largest to smallest). One sorting algorithm: - repeatedly find the maximum and move it immediately ahead of all prior maximums. Example (sort 100 values) Step 1 - find the maximum of 100 values 99 comparisons Step 2 - find the maximum of 99 values 98 comparisons Step 3 - find the maximum of 98 values 97 comparisons Total comparisons for sorting 100: Total comparisons for sorting N: (N-1)+(N-2)+(N-3)+...+1
Comparing Algorithm Performance binary search linear search sorting algorithm
Functional Growth x (note 1) (note 2) nlog 2 nn2n2 94 n note 1 - note 2 - linear search binary search sort password cracker