Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a."— Presentation transcript:

1 UNIT 5

2  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.

3  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.

4  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 n 2 comparisons (i.e. 0(n 2 )) to sort items.

5  The advanced sorting algorithm take of the order of n log 2 n (i.e., O(nlog 2 n)) comparisons to sort n items of data. Algorithms within this set come close to the optimum possible performance for sorting random data

6  Problem:  Merge two arrays of integers, both with their elements in ascending order into a single ordered array.

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

8  Almost 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:

9  We 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.

10  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;

11  Since 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.

12

13

14  Algorithm Description

15  Algorithm  Applications  Only for sorting data in which a small percentage of elements are out of order.

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

17  This 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.

18  Sorting 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.

19  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.

20  To understand this sorting algorithm lets take up an example

21

22

23  Our 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.

24  Algorithm description

25  Algorithm  Applications  Where there are relatively small data sets.  It is sometimes used for more advanced quick sort algorithm.

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

27  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.

28  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

29  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.

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

31  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

32  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

33  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

34  Algorithm description

35  Algorithm  Shellsort.txt  Applications  Works well on sorting large datasets by there are more advanced methods with better performance.

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

37  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.

38  To achieve this do the following  Extend 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

39  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 :

40  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.

41  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.

42  Algorithm description

43  Algorithm  Applications  Internal sorting of large datasets.

44  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.

45  Algorithm Development:

46  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.

47  We 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 8  This gives a[middle] = a[8] =39  Since 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

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

49  Using 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.

50  Algorithm Description

51  Problem  Design and implement a hash searching algorithm.

52  Algorithm Description

53

54  Algorithm  Hashsearch.txt  Applications  Fast retrieval from both small and large tables


Download ppt "UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a."

Similar presentations


Ads by Google