0% found this document useful (0 votes)
2 views

5.Merging-Sorting-Searching

Uploaded by

predator103722
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

5.Merging-Sorting-Searching

Uploaded by

predator103722
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

MERGING, SORTING

AND SEARCHING
CHAPTER 5
Algorithm Analysis
•Analyzing an algorithm has come to mean predicting the resources that the
algorithm requires.
•Occasionally, resources such as memory, communication bandwidth, or
computer hardware are of primary concern, but most often it is computational
time that we want to measure.
•RAM model: instructions commonly found in real computers:
◦ Arithmetic (such as add, subtract, multiply, divide, remainder, floor, ceiling)
◦ Data movement (load, store, copy)
◦ Control (conditional and unconditional branch, subroutine call and return).
•Each such instruction takes a constant amount of time.
Algorithm Analysis
•In general, the time taken by an algorithm grows with the size of
the input.
•Generally, running time of a program is describes as a function of
the size of its input.
•Input size:
◦ sorting an array of size n - n
◦ finding the shortest path between two vertices in a graph – (|V|,|E|)
•Running time:
◦ number of primitive operations or “steps” executed
Introduction to Sorting
•Sorting algorithms arrange items in a set according to a
predefined ordering relation.
•Most common types of data: string and numeric
◦ Numeric data- ascending (descending) order
◦ String data- lexicographical order.
•Sorting algorithms
◦ Simpler and less complicated algorithms (require more time)
◦ Advanced sorting algorithms
Introduction to Sorting
5.1: Two-Way Merge
•Problem: Merge two arrays of integers, both with their
elements in ascending order, into a single ordered array.
5.1: Two-Way Merge
5.1: Two-Way Merge
•Since we have access to the elements in the array, we can add the largest
element of the two arrays as the last element in the array that doesn’t
have the largest value.

•Advantage: we do not need to check if a particular array is completely


compared.
•Problem:?
5.1: Two-Way Merge
•Since we have access to the elements in the array, we can add the largest
element of the two arrays as the last element in the array that doesn’t
have the largest value.

•Advantage: we do not need to check if a particular array is completely


compared.
•Problem: unnecessary comparisons being done.
5.1: Two-Way Merge
•Since we have access to the last elements of both arrays it is
easy to determine in advance which array will be completed
first in the merge and which array will be copied after the
merge.
•Better algorithm
◦ Reduce the tests in the while loop from 2 to 1.
◦ Cut down on number of comparisons.
5.1: Two-Way Merge

•The merge and copy operations can be implemented as separate procedures or as a single procedure.
•Advantage of separate processes: in the merging process it is possible that the two arrays do not
overlap.
•After determining which array finishes first, we then determine if there is an overlap between the 2
arrays.
5.1: Two-Way Merge
5.1: Two-Way Merge
5.1: Two-Way Merge
•For two arrays of sizes m and n, the number of comparisons vary
from 2 to (n+m+1) to complete the task.
•The behavior of this algorithm is understood with the help of
understanding the procedure shortmerge.
•The number of merged elements after the ith step will be i+j-2
•The loop terminates because with each iteration either i or j is
incremented and since it is guaranteed that there exists a j such that
before shortmerge is called a condition will eventually be
established that i will increase beyond m
5.2: Sorting by Selection
•Problem: Given a randomly ordered set of n integers, sort
them into non-descending order using the selection sort.

•Selection sort divides the array into two portions: sorted and
unsorted.
•It moves the smallest element from the unsorted portion to the
ending position of the sorted part.
5.2: Sorting by Selection
5.2: Sorting by Selection
•3 parameters to analyze
◦ Number of comparisons made
◦ Number of exchanges made
◦ Number of times the minimum is updated
5.2: Sorting by Selection
•3 parameters to analyze
◦ Number of comparisons made – n(n-1)/2
◦ Number of exchanges made – n-1
◦ Number of times the minimum is updated - depends on the data
•Both the for loops terminate so the algorithm terminates.
•The number of comparisons required by the selection sort can be
reduced by finding the minimum and maximum at the same time.
•Time complexity: ?
5.2: Sorting by Selection
•3 parameters to analyze
◦ Number of comparisons made – n(n-1)/2
◦ Number of exchanges made – n-1
◦ Number of times the minimum is updated - depends on the data
•Both the for loops terminate so the algorithm terminates.
•The number of comparisons required by the selection sort can be
reduced by finding the minimum and maximum at the same time.
•Time complexity:
5.3: Sorting by Exchange
•Problem: Given a randomly ordered set of n numbers sort them
into non-descending order using an exchange method.

•After first pass

•This method guarantees that the biggest element is in the last


position in the array.
5.3: Sorting by Exchange
•The array may be sorted even before (n-1) passes.
•How to ensure that the algorithm stops if the array is already
sorted before n-1 passes?
•Number of comparisons –
•Number of exchanges –
• Is termination guaranteed? –
5.3: Sorting by Exchange
•The array may be sorted even before (n-1) passes.
•How to ensure that the algorithm stops if the array is already
sorted before n-1 passes?
•Number of comparisons – (n-1) to n(n-1)/2
•Number of exchanges – 0 to n(n-1)/2
•Is termination guaranteed? – Yes
5.3: Sorting by Exchange
•Relies heavily on exchanges which are time consuming.
•Costly algorithm for large data sets
•Suitable for the case when there are only small number of
exchanges and comparisons
•Algorithm lacks order and symmetry:
◦ Large elements make progress to the right end of the array
◦ Small values make only slow progress to the left end of the array.
•Time complexity: ?
5.3: Sorting by Exchange
•Relies heavily on exchanges which are time consuming.
•Costly algorithm for large data sets
•Suitable for the case when there are only small number of
exchanges and comparisons
•Algorithm lacks order and symmetry:
◦ Large elements make progress to the right end of the array
◦ Small values make only slow progress to the left end of the array.
•Time complexity:
5.4: Sorting by Insertion
•Problem: given a randomly ordered set of n numbers sort
them into non-descending order using an insertion method.
•Sorting by insertion is one of the more obvious and natural
ways to sort information.
5.4: Sorting by Insertion
5.4: Sorting by Insertion
5.4: Sorting by Insertion
5.4: Sorting by Insertion
•Number of comparisons:
•Number of elements to be moved:
•Homework: Compare selection sort and insertion sort for
random data based on the number of moves and
comparisons.
5.5: Sorting by Diminishing Increment
•Problem: Given a randomly ordered set of n numbers sort them
into non-descending order using Shell’s diminishing increment
insertion method.
•A comparison of random and sorted data sets indicates that for
an array of size n, elements need to travel on average a distance
of about n/3 places.
•This observation suggests that the final sorted order will be
quicker if elements are compared and moved initially over longer
than shorter distances.
5.5: Sorting by Diminishing Increment
•Start with distance n/2, then n/4, n/8 and so on until the
distance is 1.
5.5: Sorting by Diminishing Increment
•How many chains are to be sorted for each increment gap?
◦ Same as the increment value itself.
•How to access the individual chains for insertion sorting?
◦ In insertion sort we aim to begin by inserting the second element of
the array in its correct spot.
◦ In shell sort this element becomes the second element in every chain.
◦ This can be done by tracking the beginning of every chain with a
variable j.
◦ The elements in the chain are tracked using the variable k.
5.5: Sorting by Diminishing Increment
The outer loop will continue as long as the
increment value is greater than 1.
The increment value decreases by half in each
loop.
The increment value also decides the length of
the chain.
The value of k determines the index of an
element in the chain being sorted.
The bool value inserted determines if the
element x is in the correct place.
currPos and prevPos are used to compare the
elements during insertion
5.5: Sorting by Diminishing Increment
•The analysis of shellsort has proven to be a very difficult
problem.
•It can be shown that there are better choices of the
decrement sequence than n/2, n/4, n/8,… 1.
•For the sequence of decrements … 31,15,7,3,1 the number
of comparisons and moves is proportional to which is better
than but not the best.
5.7 Binary Search
•Problem: Given an element x and a set of data that is in
strictly ascending numerical order, find whether or not x is
present in the set.
•Looking up someone’s number in a telephone directory.
•Strategy applied: divide and conquer.
•Search for value x = 44
5.7 Binary Search
•If x is present in the array we want that after each iteration
this condition holds:

•How to decide the middle value?

•How to decide the lower value?

•How to decide the upper value?


5.7 Binary Search
•How to terminate the algorithm?
◦ Condition:
•A better implementation of the algorithm considers updating of ,
and differently so that we ensure the following
◦ (i) lower and upper values are increased and decreased respectively every
time they are reassigned.
◦ (ii) middle value is always lesser than the upper value

•Terminating condition: only


5.7 Binary Search
•Cases to consider:
◦ Seeking value is less than the first value or greater than the last
value.
◦ When there is only one element in the array.
•Binary search offers a more efficient alternative than the
linear search algorithms.
•In general no more than comparisons are needed. A linear
search on average requires comparisons.

You might also like