5.Merging-Sorting-Searching
5.Merging-Sorting-Searching
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.
•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.