Sorting searching
This presentation is the property of its rightful owner.
Sponsored Links
1 / 15

Sorting & Searching PowerPoint PPT Presentation


  • 48 Views
  • Uploaded on
  • Presentation posted in: General

Sorting & Searching. 27 10 12 20. divide. 27 10. 12 20. divide. divide. 10. 12. 20. 27. merge. merge. 10 27. 12 20. merge. 10 12 20 27. Mergesort. Merge two sorted arrays. first array. second array. result of merge. 10 27. 12 20. 10. 10 27.

Download Presentation

Sorting & Searching

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Sorting searching

Sorting&Searching


Mergesort

27 10 12 20

divide

27 10

12 20

divide

divide

10

12

20

27

merge

merge

10 27

12 20

merge

10 12 20 27

Mergesort


Merge two sorted arrays

Merge two sorted arrays

first array

second array

result of merge

10 27

12 20

10

10 27

12 20

10 12

10 27

12 20

10 12 20

10 27

12 20

10 12 20 27


Analysis of mergesort

Analysis of Mergesort

  • Dividing two arrays takes constant time

    • just compute midpoint of array

  • Merging two arrays of size m requires at least m and at most 2m - 1 comparisons

    • one comparison at each step of the merge

    • elements in result array are not compared again

    • add at least one element to result at each step

  • Time to merge is linear in the size of the array

  • Total number of comparisons is the sum of the number of comparisons made at each merge


Logarithm

Logarithm

  • Logarithm of a number n

    • power to which any other number a must be raised to produce n

    • a is called the base of the logarithm

  • Frequently used logs have special symbols

    • lg n = log2 nlogarithm base 2

    • ln n = loge nnatural logarithm (base e)

    • log n = log10 ncommonlogarithm (base 10)


Logarithm base 2

Logarithm base 2

  • lg 1 = 0because20 = 1

  • lg 2 = 1because20 = 2

  • lg 4 = 2because22 = 4

  • lg 8 = 3because23 = 8

  • lg 16 = 4because24 = 16

  • If we assume n is a power of 2, then the number of times we can recursively divide n numbers in half is lg n


Total number of comparisons

1

1

1

1

1

1

2

2

2

4

4

n/2

n/2

n

Total number of comparisons

< 2n comparisons

< 4*(n/2) = 2n comparisons

< 8*(n/4) = 2n comparisons

lg n

< 4*(n/2) = 2n comparisons


Running time

Running time

  • Running time of Mergesort in (n lg n)

    • best, worst and average-case

  • Compare with Insertion sort:


Quicksort

Quicksort

  • Most commonly used sorting algorithm

  • One of the fastest sorts in practice

  • Best and average-case running time in

    O(n lg n)

  • Worst-case running time is quadratic

  • Runs very fast on most computers when implemented correctly


Searching

Searching

  • Determine the location or existence of an element in a collection of elements of the same type

  • Easier to search when the elements are already sorted

    • finding a phone number in the phone book

    • looking up a word in the dictionary

  • What if the elements are not sorted?


Sequential search

Sequential search

  • Collection of n unsorted elements

  • Compare each element in sequence

  • Worst-case: Unsuccessful search

    • search element is not in input

    • make n comparisons

    • search time is linear

  • Average-case:

    • expect to search ½ the elements

    • make n/2 comparisons

    • search time is still linear


Searching sorted input

Searching sorted input

  • If the input is already sorted, we can search more efficiently than linear time

  • Example: “Higher-Lower”

    • think of a number between 1 and 1000

    • have someone try to guess the number

    • if they are wrong, you tell them if the number is higher than their guess or lower

  • Strategy?

  • How many guesses should we expect to make?


Best strategy

Best Strategy

  • Always pick the number in the middle of the range

  • Why?

    • you eliminate half of the possibilities with each guess

  • We should expect to make at most

    lg1000  10 guesses

    • lg 512 = 9

    • lg 1024 = 10

    • 512 < 1000 < 1024


Binary search

15

9

12

4

8

7

6

1

15

9

12

8

9

9

8

Binary search

  • Search n sorted inputs in logarithmic time

  • Example: Search for 9

  • At most threecomparisons


Exercises

Exercises

  • An algorithm takes 2 seconds to run for input size 64. How long will it take for input size 512 if the running time is

    • logarithmic?

    • linear?

    • quadratic?


  • Login