1 / 29

Searching and Sorting

Searching and Sorting. Why do we need Searching and Sorting. Computer systems are constructed to solve data processing problems (electricity bills, payroll processing, air traffic control). In such systems, updating a database is an equally common operation.

dusty
Download Presentation

Searching and Sorting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Searching and Sorting

  2. Why do we need Searching and Sorting • Computer systems are constructed to solve data processing problems (electricity bills, payroll processing, air traffic control). In such systems, updating a database is an equally common operation. • New items are to be added, old items to be deleted and existing items need to have detailed changed. • Searching for an item is nearly always made easier when data items are held in some specific order.

  3. 1. Sequential Search

  4. Sequential linear search • http://www.cosc.canterbury.ac.nz/mukundan/dsal/LSearch.html • This is a simple search. • Each element is examined in turn to see if it is the required item. The search is ended when the item is found or the end of the lost is reached.

  5. Binary Search

  6. Binary Search • This method of searching a list is often compared to looking through a dictionary or telephone book. • Suppose you are working for the word ‘cat’ If you open the book in the middle you might end up in the ‘k’ section • Few people will then look further in the book but will open the lower section in the middle and find for example ‘f’. • Again they open the book again and find ‘b’ • This process is continued until they find ‘c’

  7. Binary Search • Pseudo Code While the list is bigger than 1 item and wanted item is not found calculate mid point of list if value at mid is wanted value then found it else if wanted is in lower half move end point to one less than midpoint else move start point to one more than midpoint end while

  8. Binary Search • http://www.cosc.canterbury.ac.nz/mukundan/dsal/BSearch.html

  9. Binary Search Say the wanted item is 50 First find the midpoint of the list (9+0)/2 = 4 Say the wanted item at 4 is 25. It is not the item wanted It is less than the wanted item so make position 5 the new start point

  10. Binary Search We need to calculate the midpoint (9+5)/2 = 7 The wanted item is 50 and it is found in point 7 so it is found

  11. SORTING

  12. One disadvantage of binary search is that the list of values to be searched must be in order whereas linear search will work with unordered lists

  13. Bubble sort

  14. Bubble sort • This sort starts with the first pair of numbers (0,1) which are compared and swapped if necessary • The next pair (1,2) are compared until the last pair is compared • Since the highest value in the list has now ‘bubbled’ to the top, the whole process is repeated with the first n-1 elements of the list

  15. Bubble sort • http://www.cosc.canterbury.ac.nz/mukundan/dsal/BSort.html

  16. Bubble sort

  17. Bubble sort

  18. Bubble sort

  19. Selection Sort

  20. Selection Sort • http://www.cosc.canterbury.ac.nz/mukundan/dsal/SSort.html

  21. Selection sort • The list is split into 2 parts, the sorted part and the unsorted part (initially these are all elements) • Each element in the unsorted part is examined in turn to locate the smallest and this is swapped (exchanged) with the first element of the unsorted list. • The boundary of the sorted list is then incremented • The process is continued until all elements have been placed in the correct position

  22. Selection sort

  23. 2.1.10 Efficiency of searching and sorting algorithms • The efficiency of a linear or sequential search algorithm depends on the number of items in the array. In the worst case the whole array has to be searched. Therefore there is a linear relationship between the array size and the search time

  24. For binary search, the array is halved in size at each step, doubling the size of the array leads to a single step increase in search time.

  25. Summary

  26. Each binary search step takes a bit longer than each linear search step as the algorithm is more complex

  27. Time complexity • If we allocate a time of 100mSec to each linear search step • If we allocate a time of 500mSec to each binary search step we can construct a graph of array size against time (for a very slow computer!)

  28. Summary • Binary search is faster as N gets larger • Linear search is faster as N gets smaller

More Related