unit 5. the related activities of sorting, searching and merging are central to many computer...

54
MERGING SORTING AND SEARCHING UNIT 5

Upload: valerie-harrison

Post on 20-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

MERGING SORTING AND

SEARCHINGUNIT 5

Page 2: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

INTRODUCTION The related activities of sorting, searching

and merging are central to many computer applications.

Sorting and merging provide us with a means of organizing information take advantage of the organization of information and thereby reduce the amount of effort to either locate a particular item or to establish that it is not present in a data set.

Sorting algorithms arrange items in a set according to a predefined ordering relation.

Page 3: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

INTRODUCTION The two most common types of data are

string information and numerical information.

The ordering relation for numeric data simply involves arranging items in sequence from smallest to largest (or vice versa) such that each item is less than or equal to its immediate successor.

This ordering is referred to as non-descending order.

Page 4: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

INTRODUCTION Sorted string information is generally

arranged in standard lexicographical or dictionary order.

Sorting algorithms usually fall into one of two classes: The simpler and less sophisticated

algorithms are characterized by the fact that they require of the order of n2 comparisons (i.e. 0(n2)) to sort items.

Page 5: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

INTRODUCTIONThe advanced sorting algorithm take of the

order of n log2n (i.e., O(nlog2n)) comparisons to sort n items of data. Algorithms within this set come close to the optimum possible performance for sorting random data

Page 6: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

THE TWO WAY MERGE Problem:

Merge two arrays of integers, both with their elements in ascending order into a single ordered array.

Page 7: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY EXCHANGE Problem

Given a randomly ordered set of n numbers sort them into non-descending order using exchange method.

Page 8: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY EXCHANGEAlmost all sorting methods rely on

exchanging data to achieve the desired ordering.

This method we will now consider relies heavily on an exchange mechanism.

Suppose we start out with the following random data set:

Page 9: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY EXCHANGEWe notice that the first two elements are

“out of order”. If 30 and 12 are exchanged we will have the

following configuration:

After seeing the above result we see that the order in the data can be increased further by now comparing and swapping the second and third elements.

Page 10: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY EXCHANGE With this new change we get the

configuration

The investigation we have made suggests that the order in the array can be increased using the following steps:

For all adjacent pairs in the array do If the current pair of elements is not in non-

descending order then exchange the two elements.

After applying this idea to all adjacent pairs in our current data set we get the configuration below;

Page 11: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY EXCHANGESince there are n elements in the data this

implies that (n-1) passes (of decreasing length) must be made through the array to complete the sort.

Page 12: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY EXCHANGE

Page 13: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY EXCHANGE

Page 14: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY EXCHANGE Algorithm Description

Page 15: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY EXCHANGE Algorithm

ApplicationsOnly for sorting data in which a small

percentage of elements are out of order.

Page 16: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY INSERTION Problem

Given a randomly ordered set on n numbers sort them into non-descending order using an insertion method.

Page 17: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY INSERTIONThis is a simple sorting algorithm that builds

the final sorted array (or list) one item at a time.

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.

Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.

It repeats until no input elements remain.

Page 18: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY INSERTIONSorting is typically done in-place, by

iterating up the array, growing the sorted list behind it.

At each array-position, it checks the value there against the largest value in the sorted list (which happens to be next to it, in the previous array-position checked).

If larger, it leaves the element in place and moves to the next.

If smaller, it finds the correct position within the sorted list, shifts all the larger values up to make a space, and inserts into that correct position.

Page 19: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY INSERTION The resulting array after k iterations has the

property where the first k + 1 entries are sorted ("+1" because the first entry is skipped).

In each iteration the first remaining entry of the input is removed, and inserted into the result at the correct position, thus extending the result:

becomes

with each element greater than x copied to the right as it is compared against x.

Page 20: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY INSERTIONTo understand this sorting algorithm lets

take up an example

Page 21: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY INSERTION

Page 22: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY INSERTION

Page 23: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY INSERTIONOur complete algorithm can now be

described as:To perform an insertion sort, begin at the

left-most element of the array and invoke Insert to insert each element encountered into its correct position.

The ordered sequence into which the element is inserted is stored at the beginning of the array in the set of indices already examined.

Each insertion overwrites a single value: the value being inserted.

Page 24: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY INSERTION Algorithm description

Page 25: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY INSERTION Algorithm

ApplicationsWhere there are relatively small data sets. It is sometimes used for more advanced

quick sort algorithm.

Page 26: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY DIMINISHING INCREMENT Problem

Given a randomly ordered set on n numbers sort them into non-descending order using Shell’s diminishing increment insertion method.

Page 27: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY DIMINISHING INCREMENT Algorithm development

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 progress towards the final sorted order will be quicker if elements are compared and moved initially over longer rather than shorter distances.

This strategy has the effect (on average) of placing each element closer to its final position earlier in sort.

Page 28: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY DIMINISHING INCREMENT

A strategy that moves elements over long distances is to take an array of size n and start comparing elements over a distance of n/2 and then successively over the distances n/4, n/8, n/16 and …. 1.

Consider what happens when the n/2 idea is applied to the dataset below

Page 29: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY DIMINISHING INCREMENT

After comparisons and exchanges over the distance n/2 we have n/2 chains of length two that are sorted.

The next step is to compare elements over a distance n/4 and thus produce two sorted chains of length 4.

Page 30: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY DIMINISHING INCREMENT

Notice that after the n/4 sort the “amount of disorder” in the array is relatively small.

The final step is to form a single sorted chain of length 8 by comparing and sorting elements distance 1 apart.

Since the relative disorder in the array is small towards the end of the sort (i.e. when we are n/8- sorting in this case) we should choose our method for sorting the chains ( an algorithm that is efficient for sorting partially order data).

Page 31: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY DIMINISHING INCREMENT

The insertion short should be better because it does not rely so heavily on exchanges.

The next and most important consideration is to apply insertion sorts over the following distances : n/2, n/4, n/8 , … , 1.

We can implement this as follows

Page 32: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY DIMINISHING INCREMENT

The next steps in the development are to establish how many chains are to sorted and for each increment gap and then to work out how to access the individual chains for insertion sorting.

We can therefore expand our algorithm to

Page 33: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY DIMINISHING INCREMENT Now comes the most crucial stage of the

insertion sort. In standard implementation the first

element that we try is to insert is the second element in the array .

Here for each chain to be sorted it will need to be second element of each chain

The position of k can be given by:

Successive members of each chain beginning with j can be found using

Page 34: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY DIMINISHING INCREMENT Algorithm description

Page 35: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY DIMINISHING INCREMENT Algorithm

Shellsort.txt

ApplicationsWorks well on sorting large datasets by

there are more advanced methods with better performance.

Page 36: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY PARTITIONING Problem

Given a randomly ordered set of n numbers, sort them into non-descending order using Hoare’s partitioning method.

Page 37: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY PARTITIONING Algorithm development

Take guess and select an element that might allow us to distinguish between the big and the small elements.

After first pass we have all big elements in the right half of the array and all small elements in the left half of the array.

Page 38: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY PARTITIONING

To achieve this do the followingExtend the two partitions inwards until a

wrongly partitioned pair is encountered.While the two partitions have not crossed

Exchange the wrongly partitioned pair; Extend the two partitions inwards again until

another wrongly partitioned pair is encountered.Applying this ideas to the sample data set

Page 39: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY PARTITIONING Element 18 is selected as pivot element

This step has given us two independent sets of elements which can be sorted independently.

The basic mechanism to do sort partitions is :

Page 40: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY PARTITIONING

While all partitions are not reduced to size one do: Choose next partition to be processed; Select a new partitioning value from the current

partition; Partition the current partition into two smaller

partially ordered sets.

Page 41: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY PARTITIONING

After creating partitions of size one do the following: Choose the smaller partition to be processed

next; Select the element in the middle of the partition

as the partitioning value; Partition the current partition into two partially

ordered sets; Save the larger of the partitions from step c for

later processing.

Page 42: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY PARTITIONING Algorithm description

Page 43: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

SORTING BY PARTITIONING Algorithm

Applications Internal sorting of large datasets.

Page 44: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

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.

Page 45: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

BINARY SEARCH Algorithm Development:

Page 46: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

BINARY SEARCH Let us now consider an example in order

to try to find the details of the algorithm needed to implement.

Suppose we are required to search an array of 15 ordered elements to find x= 44 is present . If present then return the position of the array that contains 44.

Page 47: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

BINARY SEARCHWe start by examining the middle value in

the array.To get the middle value of size n we can try

middle <- n / 2;For the above problem middle value is 8This gives a[middle] = a[8] =39Since the value we are seeking is greater

than 39 it must be somewhere in the range a[9] … a[15].

That is 9 becomes the lower limit and 15 upper limit.

lower = middle +1

Page 48: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

BINARY SEARCHWe then have

To calculate the middle index 9 +15 / 2 =12a[12]=49 > 44 so search in a[9] .. a[11].

Page 49: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

BINARY SEARCHUsing the same above procedures calculate

the middle position.

Our middle position is 10 and a[10] contains44 which is matching with our key to be found.

Hence return the position 10 .

Page 50: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

BINARY SEARCH Algorithm Description

Page 51: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

HASH SEARCHING Problem

Design and implement a hash searching algorithm.

Page 52: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

HASH SEARCHING Algorithm Description

Page 53: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

HASH SEARCHING

Page 54: UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a

HASH SEARCHING Algorithm

Hashsearch.txt

ApplicationsFast retrieval from both small and large

tables