UNIT 5. The related activities of sorting, searching and merging are central to many computer...

Post on 20-Jan-2016

213 views 0 download

Tags:

Transcript of UNIT 5. The related activities of sorting, searching and merging are central to many computer...

MERGING SORTING AND

SEARCHINGUNIT 5

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.

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.

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.

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

THE TWO WAY MERGE Problem:

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

SORTING BY EXCHANGE Problem

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

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:

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.

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;

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.

SORTING BY EXCHANGE

SORTING BY EXCHANGE

SORTING BY EXCHANGE Algorithm Description

SORTING BY EXCHANGE Algorithm

ApplicationsOnly for sorting data in which a small

percentage of elements are out of order.

SORTING BY INSERTION Problem

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

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.

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.

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.

SORTING BY INSERTIONTo understand this sorting algorithm lets

take up an example

SORTING BY INSERTION

SORTING BY INSERTION

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.

SORTING BY INSERTION Algorithm description

SORTING BY INSERTION Algorithm

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

quick sort algorithm.

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.

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.

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

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.

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).

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

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

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

SORTING BY DIMINISHING INCREMENT Algorithm description

SORTING BY DIMINISHING INCREMENT Algorithm

Shellsort.txt

ApplicationsWorks well on sorting large datasets by

there are more advanced methods with better performance.

SORTING BY PARTITIONING Problem

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

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.

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

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 :

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.

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.

SORTING BY PARTITIONING Algorithm description

SORTING BY PARTITIONING Algorithm

Applications Internal sorting of large datasets.

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.

BINARY SEARCH Algorithm Development:

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.

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

BINARY SEARCHWe then have

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

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 .

BINARY SEARCH Algorithm Description

HASH SEARCHING Problem

Design and implement a hash searching algorithm.

HASH SEARCHING Algorithm Description

HASH SEARCHING

HASH SEARCHING Algorithm

Hashsearch.txt

ApplicationsFast retrieval from both small and large

tables