View stunning SlideShares in full-screen with the new iOS app!Introducing SlideShare for AndroidExplore all your favorite topics in the SlideShare appGet the SlideShare app to Save for Later — even offline
View stunning SlideShares in full-screen with the new Android app!View stunning SlideShares in full-screen with the new iOS app!
In merge, two sorted sequences are merged into one. Clearly, two sorted sequences each of length n can be merged into a sorted sequence of length 2 n in O (2 n )= O ( n ) steps. However in order to do this, we need space in which to store the result.
Sorting by merging is a recursive, divide-and-conquer strategy. In the base case, we have a sequence with exactly one element in it. Since such a sequence is already sorted, there is nothing to be done. To sort a sequence of n> 1 elements:
Divide the sequence into two sequences of length n/2
and n/2 ;
recursively sort each of the two subsequences; and then,
merge the sorted subsequences to obtain the final result.
14.
Merge Sort (continue..) Implementation in C++ Merging The Merge function of the TwoWayMergeSorter<T> class is defined in Program . Altogether, this function takes four parameters: The first is a reference to the array to be sorted. The remaining three, left, middle, and right, are unsigned integers. It is assumed that Furthermore, it is assumed that the two subsequences of the array, and are both sorted. The Merge routine merges the two sorted subsequences using the temporary array specified by tempArray. It then copies the merged (and sorted) sequence into the array at
Quicksort is a divide-and-conquer style algorithm. A divide-and-conquer algorithm solves a given problem by splitting it into two or more smaller sub problems, recursively solving each of the sub problems, and then combining the solutions to the smaller problems to obtain a solution to the original one.
To sort the sequence S={s1,s2,s3,…..,sn} , Quicksort performs the following steps:
Select one of the elements of S . The selected element, p , is called the pivot .
Remove p from S and then partition the remaining elements of S into two distinct sequences, L and G , such that every element in L is less than or equal to the pivot and every element in G is greater than or equal to the pivot. In general, both L and G are unsorted .
Rearrange the elements of the sequence as follows: (continue..)
Notice that the pivot is now in the position in which it belongs in the sorted sequence, since all the elements to the left of the pivot are less than or equal to the pivot and all the elements to the right are greater than or equal to it.
Recursively quicksort the unsorted sequences L and G .
The first step of the algorithm is a crucial one. We have not specified how to select the pivot. Fortunately, the sorting algorithm works no matter which element is chosen to be the pivot. However, the pivot selection affects directly the running time of the algorithm. If we choose poorly the running time will be poor.
Next example illustrates the detailed operation of quicksort as it sorts the sequence { 3,1,4,1,5,9,2,5,4) . To begin the sort, we select a pivot. In this example, the value 4 in the last array position is chosen.
Next, the remaining elements are partitioned into two sequences, one which contains values less than or equal to 4 ( L={3,1,2,1} ) , and one which contains values greater than or equal to 4 ( G={5,9,4,6,5} ). Notice that the partitioning is accomplished by exchanging elements. This is why quicksort is considered to be an exchange sort.
44.
Heap Sort (Continue..) Implementation in C++ The Sorting Phase : Once the max heap has been built, heapsort proceeds to the sorting phase.b The sorting phase of heapsort works like this: We repeatedly swap the largest element in the heap (always in position 1) into the next position of the sorted sequence. After each such swap, there is a new value at the root of the heap and this new value is pushed down into the correct position in the heap using the Percolate Down routine.
45.
Heap Sort (Continue..) Implementation in C++ The Sorting Phase : The next example explains this phase
Views
Actions
Embeds 0
Report content