Question

In: Computer Science

Subject: Computer Algorithms 3) Design an efficient algorithm that sorts an array containing 2 sorted sets...

Subject: Computer Algorithms

3) Design an efficient algorithm that sorts an array containing 2 sorted sets of numbers, such as A=[3,4,7,9,13,1,5,6,8] (Note that [3,4,7,9,13] and [1,5,6,8] are both sorted, but A is not). Your algorithm declaration should look like this:

MYSORT(A,p,n)

where A is the input array, p is the index to the first element of the second set of numbers, and n is the length of array A.

Calculate the asymptotic running time and space of you algorithm.

Reference book

Introduction to Algorithms 3rd edition

Ebook URL:

http://ce.bonabu.ac.ir/uploads/30/CMS/user/file/115/EBook/Introduction.to.Algorithms.3rd.Edition.Sep.2010.pdf

Solutions

Expert Solution

Mergesort is a sort algorithm that splits the item to be sorted into two groups ,recursively sort seach group, and merges them into a final sorted sequence.

Features:
•Is a comparison based algorithm
•Is a stable algorithm
•Is a perfect example of divide & conquer algorithm design strategy
•It was invented by John Von Neumann

MYSORT(A,p,n)

{

q=n+1;

if (p < q) then
{
mid:= ceil((p + q)/2);
MergeSort(p, mid);
MergeSort(mid + 1, q);
Merge(p, mid, q);
}
}
---------------------------------------------------------------------------------------------------------------------------

Merge(A, low, mid, high)
n1=mid –low + 1
n2=high–mid
for i =1 to n1
do L[i] =A[low + i –1]
for j =1 to n2
do R[j] =A[mid + j]
L[n1+1] = infinity
R[n2+1] = infinity
i = 1
j = 1
for k =low to high
do if L[i] <=R[j]
then A[k] =L[i]
i =i+ 1
elseA[k] =R[j]
j =j+1

---------------------------------------------------------------------------------------------------------------------

TIME COMPLEXITY:

The conquer step of merge-sort consist of merging two sorted sequences ,each with n/2 elements and implemented by means of a doubly linkedlist, takes atmost bn steps, for some constant b.
•Likewise, the basis case (n<2) will take at b most steps.
•Therefore, if we let T(n) denote the running time of merge-sort:

T(n) = b if n<2

=2T(n/2)+bn if n>=2

We can therefore analyze the running time of merge-sort by finding a closed form solutionto the above equation.
–That is, a solution that has T(n) only on the left-hand side.

  In the iterative substitution, or “plug-and-chug,” technique, we
iteratively apply the recurrence equation to itself and see if we
can find a pattern:  

T(n)=2T(n/2)+bn

=2(2T(n/4))+b(n/2))+bn

=4 T(n/4)+2bn

= 8 T(n/8) +3bn

=16 T(n/16)+4bn

=2^i T(n/2^i)+ibn

Note that base, T(n)=b, case occurs when 2i=n. That is, i = log n.
• So, T(n)=bn + bnlogn
• Thus, T(n) is O(n log n).  

SPACE COMPLEXITY

IN THIS SORT 2 NEW ARRAYS ARE USED WHICH WILL OCCUPY A SPACE APPROXIMATELY EQUAL TO n SO THE SPACE COMPLEXITY IS O(n)   


Related Solutions

The following is the pseudocode of algorithm that searches a sorted array of n items by...
The following is the pseudocode of algorithm that searches a sorted array of n items by dividing it into three sub arrays of almost n/3 items (7pt). Hint: refer Mergesort algorithm) Input: Sorted array of Keys(A) indexed from 1 to n. Output: Location, the Location of K in array A (hint: return 0 if K is no in A) index location3 (int A[1…n ], index L, index H) { index m1, m2; if (L > H) return 0; else   ...
Let iqsort(A, 1, n) be an algorithm that sorts an array A with n integers. It...
Let iqsort(A, 1, n) be an algorithm that sorts an array A with n integers. It works as follows: iqsort(A, p, q){ if p ≥ q, return; r=partition(A, p, q); //run quick sort on the low part quicksort(A, p, r − 1); //run insert sort on the high part insertsort(A, r + 1, q); } Compute the best-case, worst-case, and average-case complexities of iqsort.
Assume that the incoming array is sorted and adjust the algorithm below to allow for faster...
Assume that the incoming array is sorted and adjust the algorithm below to allow for faster exit if the searched for value is not in the list. void linearsearch (int n, int Arr[], int a, int& index){ index = 1; while (index <= n && Arr[index] != a) index++; if (index > n) index = 0; }
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1]...
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1] of n elements. void ISORT (dtype A[ ], int n) { int i, j; for i = 1 to n – 1 {     //Insert A[i] into the sorted part A[0..i – 1]     j = i;     while (j > 0 and A[j] < A[j – 1]) {         SWAP (A[j], A[j – 1]);         j = j – 1 }     }...
(Subject: Data Structures and Algorithms) Faster merging. You are given two sorted sequences of $\lg{n}$ and...
(Subject: Data Structures and Algorithms) Faster merging. You are given two sorted sequences of $\lg{n}$ and $n-1$ keys. We would like to merge those two sorted sequences by performing $o(n)$ comparisons. (Note we are interested in comparisons, not running time.) Show how this can be done or argue that it cannot be done. Note: In class we showed that ordinary merging would require no more than $\lg{n}+n-1+1= n + \lg{n}$ comparisons.
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for find a pair of elements A[i] and A[i+1] such that A[i] != A[i+1]. can you always find such pair and why
The insertion sort algorithm updates its sorted array as each new data value is entered. It...
The insertion sort algorithm updates its sorted array as each new data value is entered. It is an in-place algorithm, in that the sorted array at each step writes over the array from the previous step. • Let us start with sorting in descending order (largest to smallest). The ascending order case follows as a simple modification to the descending case. • Assume that we have thus far read N values and these are sorted in correct order (largest to...
Problem 3 (4+2+2 marks). (a) Implement the following algorithm, which is given a duplicate-free array array...
Problem 3 (4+2+2 marks). (a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output...
Design an O(nlogn) algorithm that takes two arrays A and B (not necessarily sorted) of size...
Design an O(nlogn) algorithm that takes two arrays A and B (not necessarily sorted) of size n of real numbers and a value v. The algorithm returns i and j if there exist i and j such that A[i] + B[j] = v and no otherwise. Describe your algorithm in English and give its time analysis.
Design an algorithm that will read an array of 200 characters and display to the screen...
Design an algorithm that will read an array of 200 characters and display to the screen a count of the occurrences of each of the five vowels (a, e, i, o, u) in the array. Your string is: The quick brown fox jumped over the lazy dog That is the question for my pseudo code question, I don't know if I have this correct can i have someone look this over and finish this and explain what i needed to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT