Question

In: Computer Science

Ryan is implementing a merge sort algorithm that he heard about in computers class. However, he...

Ryan is implementing a merge sort algorithm that he heard about in computers class. However, he was not paying attention, and ended up implementing the merge sort in a very unusual way. The standard merge sort takes a list, and recursively splits it in half, until there is only one element left. It then uses the idea that two sorted lists can be easily merged in O(n) time using "two pointer technique" (this step is usually called merge). Ryan does not know about two pointer technique, so he decided to replace merge with a bubble sort! The bubble sort runs in O(n^2) time. What is the runtime of this merge sort implementation?

Solutions

Expert Solution

Merge sort:

Merge sort is an sorting technique which uses divide and conquer method for sorting the set of elements recursively,hence less consuming less time.It run in 0(n*logn) time in all the cases.It divides the input array in to 2 halves ,call itself for the 2 halves and then merges the 2 sorted halves.

Run time analysis :

Merge sort algorithm

Public static void sort(double[ ]a,int left,int right, double [ ]tmp);

{

Double left, right;

If(left-right)==1)

{

Return;

}

Int middle=(left+right)/2;

Sort(a,left,middle,tmp);

Sort(a,middle,right,tmp);

Merge(a,left,middle,right,tmp);

}

Define:T[N]=statement that u need to execute to merge sort an array of N elements

From merge sort algorithm we can see that

Sort(array of N elements)

Will call:

Sort(array of N/2 elements);

Sort(array of N/2 elements);

Merge(array of N elements);

There fore:

Amount workdone by sort(array of size N)=

Amount workdone by sort(array of size N)+amount workdone by sort(array of size N)+N

Or

T(N)=T(N/2)+T(N/2)+N

Further more:

T(1)=1//time to execute if statement

*The performance of merge sort is given by recurrence equation=

T(N)=n*T(N/2)+N....(1)

T(1)=1......…................(2)

Solving the reccurence equation we get

*We use the expansion method to find a solution then we can detect a pattern

T(N)=2*T(N/2)+N

4*T(N/4)+2*N =2^2+T(N/(2^2))+2*N

For simplicity we can assume N= some power of 2

N=1024(2^10)

*N=2^k then we can solve can solve exactly as follows:

T(N)=(2^k)+T(N/2^k)+k*N like this

The final result will be

=(Log(N)+1)*lg(N)

Since sorting an array of size N=2^k will take

running time of merge sort =( log(N))+1*N

then sorting array size<2^k will take atmost

running time of merge sort <=(log(N))+1*N

There fore the running time of merge sort to sort an array of size N is bounded by :

(Log(N))+1*N=0(N*log(N))

​​​​​


Related Solutions

I'm having trouble implementing the merge function on this recursive merge sort. I am not allowed...
I'm having trouble implementing the merge function on this recursive merge sort. I am not allowed to change the parameter list in the functions. This is what I have so far, written in C. So far I've been getting segfaults, and I feel like it's because I'm off by 1, somewhere... void merge_the_array(int *list, int l1, int h1, int l2, int h2){ // create a temp array for l1-h1 int arr_a[(h1-l1)+1]; // create a temp array for l2-h2, adding 1...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
Compare and Contrast Bubble Sort with Merge Sort. Discuss about the efficiency of both.
Compare and Contrast Bubble Sort with Merge Sort. Discuss about the efficiency of both.
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After...
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After division, each process sorts its part of the array using an efficient algorithm. Then, the subarrays are merged into larger sorted subarrays. b. Analyze the communication and computation times if the number of processes is equal to the number of array elements, n. c. Repeat Part b if the number of processes is less than the number of array elements. Assume that the computation...
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain...
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain size. The list is to be implemented using a dynamic array as well as a singly linked list. You are required to compare the performance (sorting time) for the dynamic array and singly-linked list-based implementations. You are given the startup codes for the dynamic array and singly-linked list based implementations. You are required to implement the Selection Sort function in each of these codes....
Create a quick and merge sort algorithm that sorts 6 9 8 12 3 1 7...
Create a quick and merge sort algorithm that sorts 6 9 8 12 3 1 7 In java please.
Design and analysis of Algorithms: How do I write a k way merge sort algorithm from...
Design and analysis of Algorithms: How do I write a k way merge sort algorithm from a merge sort algorithm that splits 2 arrays while sorting?
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude <iostream>)
rofessor Moriarty has never taken a formal statistics course; however, he has heard about the bell-shaped...
rofessor Moriarty has never taken a formal statistics course; however, he has heard about the bell-shaped curve and has some knowledge of the Empirical Rule for normal distributions. Professor Moriarty teaches as Honors Quantum Physics class in which he grades on the bell curve. He assigns letter grades to his students' tests by assuming a normal distribution and utilizing the Empirical Rule. The Professor reasons that if IQ and SAT scores follow a normal distribution, then his students' scores must...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT