Presentation is loading. Please wait.

Presentation is loading. Please wait.

Manipulating lists of data

Similar presentations


Presentation on theme: "Manipulating lists of data"— Presentation transcript:

1 Manipulating lists of data
Searching & Sorting I Manipulating lists of data for quick retrieval

2 Searching and Sorting “Which are the top three college football teams?” “Who was the baseball player with the longest unbroken string of games?” “Which Presidential Candidate has the best chance of winning?” “What are your top five recreational activities?”

3 Common Problems Computers are often used to do two things
Search: find a particular item or type of item. Sort: put items in their proper order. Who searches and sorts? Airlines: find your reservation, ticket, seat Catalog retailers/e-tailers: who are you? Have you ordered before? What did you order? Did you pay? What is the best selling item? Credit Card Companies: what did you buy? ... almost any company

4 Olsen’s Law of Computer Performance
There are only three types of computers in the world: Too small Too slow All of the above.

5 Searching Does a search have to be fast ? (Yes!)
How can we make the search faster ? By keeping the records in some order Using an efficient search algorithm Search algorithms Sequential search Binary search

6 Sequential Search on an Unordered File
Searches records in sequence. Get the search criterion from the user Get the first record from the file While the record doesn’t match the criterion && there are still more records in the file do get the next record When do we know: The file contained a matching record? The file did not contain a matching record?

7 Sequential Search of an Ordered File
Search the file in sequence when the file is in a known order. Get the search criterion from the user Get the first record from the file While the record is less than the criterion do get the next record If the record matches the criterion then success else there is no match in the file. When do we know: The file contained a matching record? The file did not contain a matching record?

8 Sequential Search of Ordered vs. Unordered List
Customer list sorted in alphabetical order by last names. How would the search for John Adams on the ordered list compare to the search on the unordered list? Ordered List if John Adams was in the list ? if John Adams was not in the list ? Unordered List

9 Ordered vs Unordered (continued)
How about George Washington ? unordered in the file not in the file ordered James Madison ?

10 More Searching Overall, ordered files yield no consistent improvement if we’re using the sequential search. Improvement depends on place in order. “Adams” --- big difference “Washington” --- little difference How else could we search an ordered file?

11 Binary Search Search algorithm for ordered lists of known length.
Binary Search --- named because each step divides the region of uncertainty in half.

12 How Binary Search Works
Always look at the center value. Each time you get to get to discard half of the remaining list. Is this fast ?

13 How fast is Binary Search ?
Worst case : 11 Items in the list took 4 tries How about a list with 32 items ? 1st try - list has 16 items 2nd try - list has 8 items 3rd try - list has 4 items 4th try - list has 2 items 5th try - list has 1 item

14 More examples List has 512 items List has 250 items
1st try items 2nd try items 3rd try - 64 items 4th try - 32 items 5th try - 16 items 6th try - 8 items 7th try - 4 items 8th try - 2 items 9th try - 1 item List has 250 items 1st try items 2nd try - 63 items 3rd try - 32 items 4th try - 16 items 5th try - 8 items 6th try - 4 items 7th try - 2 items 8th try - 1 item

15 What’s the pattern ? List of 11 took 4 tries List of 32 took 5 tries
32 = 25 and 512 = 29 9 < 11 < < 11 < 24 128 < 250 < < 250 < 28

16 The fastest ! How long (worst case) will it take to find an item in a list 30,000 items long ? 210 = = 8192 211 = = 16384 212 = = 32768 So it will take 15 tries. It only takes 15 tries to find what we want out of 30,000 items.

17 Twenty Questions Ask 20 “yes” or “no” questions.
How many items can you distinguish? items ~ 10(20/lg2(10)) ~ 10(20/3.32) ~ 10(6.02)

18 Lg n Resolution ~ 2n where n is the number of questions.
“The binary search algorithm runs in lg n time.” lg n means the log to the base 2 of some value of n 8 = lg 8 = 3 16 = lg 16 = 4 There are no search algorithms that run faster than lg n time. Sequential search runs in linear time meaning execution time is proportional to n.

19 Courses at UMBC Algorithms - CMSC 441 Cryptology - CMSC 443
Studies algorithms and their speed Cryptology - CMSC 443 The study of making & breaking codes - write programs that can break the code like Pdeo eo pda yknnayp wjosan

20 Searching and Sorting (continued)
We have a very fast search algorithm - Binary search But, the list has to be sorted, before we can search it with binary search. To sort the list efficiently, we also need a fast sort algorithm.

21 Some Sort Algorithms Bubble Sort Heap Sort Selection Sort Merge Sort
Insertion Sort Quick Sort In an effort to find a very fast sorting algorithm, we have many known sorting algorithms. Bubble sort is the slowest, running in n2 time. Too slow !

22 Speed of Sorting Algorithms
Most Sorting algorithms run in n lg n time for the worst case. Quick Sort runs a little faster for the average case. So it is usually the sort that’s used. The algorithm for Quick Sort is quite complicated. It is shown in your book. There is a pre-written function called qsort in the C standard library.

23 Insertion Sort Insertion sort is almost as fast, and it is easy to understand Insertion sort works the same way as arranging your hand when playing cards. Out of the pile of unsorted cards that were dealt to you, you pick up a card and place it in your hand in the correct position relative to the cards you’re already holding.

24 Arranging Your Hand 7 5 7

25 Arranging Your Hand 5 7 5 6 7 5 6 7 K 5 6 7 8 K

26 Insertion Sort Unsorted - shaded 7 K 1 7 5 v 7 5 7 > 2 5 7 3 <
Look at 2nd item - 5 Compare 5 to 7 5 is smaller, so move to temp, leaving an empty slot in position 2 Move 7 into the empty slot, leaving position 1 open Move 5 into the open position 1 7 5 v 7 5 7 > 2 5 7 3 <

27 Insertion Sort Look at next item - 6 5 7 6 K 1 5 7 v 5 7 6 5 7 > 2
Compare to 1st - 5 6 is larger, so leave Compare to next - 7, is smaller, so move to temp, leaving an empty slot Move 7 into the empty slot, leaving position 2 open Move 6 to the open 2nd position 1 5 7 v 5 7 6 5 7 > 2 5 6 7 3 <

28 Insertion Sort Look at next item - King 5 6 7 K Compare to 1st - 5
King is larger, so leave 5 where it is Compare to next - 6, King is larger, so leave 6 where it is Compare to next King is larger, so leave 7 where it is

29 Insertion Sort 5 6 7 K 8 1 5 6 7 K 8 v 5 6 7 K 8 5 6 7 K > 2 5 6 7
3 <

30 In-Place/Out-of-Place Sorts
Insertion sort is an “in-place” sort because it can sort a list in the space occupied by the list. “Out-of-place” sorts require a separate workspace about as big as the list. Do you want to retain the unsorted list?


Download ppt "Manipulating lists of data"

Similar presentations


Ads by Google