Chapter 19 searching, sorting and Big O

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Big O Notation

Searching algorithms all accomplish the same goal—finding an element (or elements) that matches a given search key, if such an element does, in fact, exist. -Big O notation indicates how hard an algorithm may have to work to solve a problem. ◦For searching and sorting algorithms, this depends particularly on how many data elements there are.

searching data....

involves determingin whether a value (search key) is present in the data and, if so , finding its location.

Efficiency of the Binary Search

-In the worst-case scenario, searching a sorted array of 1023 elements takes only 10 comparisons when using a binary search. ◦The number 1023 (210 - 1) is divided by 2 only 10 times to get the value 0, which indicates that there are no more elements to test. ◦Dividing by 2 is equivalent to one comparison in the binary search algorithm.

Quadratic Run Time

A level of complexity of an algorithm that is characterized by a nested loop process, taking roughly O(N^2) steps to complete, so for an array size of 10 items, it would take roughly 100 steps to complete the process.

Efficiency of the Binary Search(continued)

Thus, an array of 1,048,575 (220 - 1) elements takes a maximum of 20 comparisons to find the key, and an array of over one billion elements takes a maximum of 30 comparisons to find the key. ◦A difference between an average of 500 million comparisons for the linear search and a maximum of only 30 comparisons for the binary search!

Binary Search algorithm

is more efficient than linear search algoriithm, but it requires that the array be sorted.

two popular search algorithms:

linear search binary search

Big O of the linear Search

linear search algorithm runs in O(n) time. -◦The worst case in this algorithm is that every element must be checked to determine whether the search item exists in the array. If the size of the array is doubled, the number of comparisons that the algorithm must perform is also doubled

This results in a big O of O(log n) for a binary search, which is also known as __________________________ and pronounced as "order log n."

logarithmic run time

sorting

plaves data in ascending or descending order, based on one or more sort keys.

Linear Search Algorithm

searches each element in an array sequentially. ◦If the search key does not match an element in the array, the algorithm tests each element, and when the end of the array is reached, informs the user that the search key is not present.

Method _____ sorts the array data's elements in an array in ascending order (by default).

sort

O(n) Algorithms

}An algorithm that requires a total of n - 1 comparisons is said to be O(n). ◦An O(n) algorithm is referred to as having a linear run time. ◦O(n) is often pronounced "on the order of n" or simply "order n."

Insertion Sort

}Insertion sort ◦another simple, but inefficient, sorting algorithm }The first iteration takes the second element in the array and, if it's less than the first element, swaps it with the first element. }The second iteration looks at the third element and inserts it into the correct position with respect to the first two, so all three elements are in order. }At the ith iteration of this algorithm, the first i elements in the original array will be sorted. }The insertion sort algorithm also runs in O(n2) time.

Merge Sort

}Merge sort ◦efficient sorting algorithm ◦conceptually more complex than selection sort and insertion sort }Sorts an array by splitting it into two equal-sized subarrays, sorting each subarray, then merging them into one larger array. }The implementation of merge sort in this example is recursive. ◦The base case is an array with one element, which is, of course, sorted, so the merge sort immediately returns in this case. ◦The recursion step splits the array into two approximately equal pieces, recursively sorts them, then merges the two sorted arrays into one larger, sorted array. }Merge sort has an efficiency of O(n log n).

Massive Parallelism and Parallel Algorithms

}Today's multi-core desktop computers typically have two, four or eight cores. }We're headed towards a world of massive parallelism where instead of two, four or eight, we could be talking about thousands and eventually millions or more processors. }The ultimate search would be to employ massive parallelism to check every cell simultaneously, in which case, you could determine whether a particular value is in an array in one "cycle" of the hardware. }In Chapter 23, Concurrency, we'll talk more about how parallel algorithms and multi-core hardware can improve the performance of searching and sorting algorithms.

Selection Sort

}simple, but inefficient, sorting algorithm Its first iteration selects the smallest element in the array and swaps it with the first element. }The second iteration selects the second-smallest item (which is the smallest item of the remaining elements) and swaps it with the second element. }The algorithm continues until the last iteration selects the second-largest element and swaps it with the second-to-last index, leaving the largest element in the last index. }After the ith iteration, the smallest i items of the array will be sorted into increasing order in the first i elements of the array. }After the first iteration, the smallest element is in the first position. After the second iteration, the two smallest elements are in order in the first two positions, etc. }The selection sort algorithm runs in O(n2) time.

Bindary Search(continued)

◦The first iteration tests the middle element in the array. If this matches the search key, the algorithm ends. ◦If the search key is less than the middle element, the algorithm continues with only the first half of the array. ◦If the search key is greater than the middle element, the algorithm continues with only the second half. ◦Each iteration tests the middle value of the remaining portion of the array. ◦If the search key does not match the element, the algorithm eliminates half of the remaining elements. ◦The algorithm ends by either finding an element that matches the search key or reducing the subarray to zero size.

three common sorting algorithms

◦The first two—selection sort and insertion sort—are easy to program but inefficient. ◦The last algorithm—merge sort—is much faster than selection sort and insertion sort but harder to program.


Set pelajaran terkait

Module 10: Right Triangle Trigonometry and the Unit Circle

View Set

Managing Care: Perioperative Nursing

View Set

Chapter 21: The Newborn at Risk: Congenital Disorders

View Set

Starting out with Java - Chapter 10

View Set