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

(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.
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.
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...
Your neighbor heard you are taking this fabulous nutrition class, and wants to know about cholesterol....
Your neighbor heard you are taking this fabulous nutrition class, and wants to know about cholesterol. Her recent checkup was not very good. It revealed very high cholesterol levels and she wants you to help her with a diet low in fat.
1- A student at UHD heard the following statement made about the Business Cornerstone class. "Oh...
1- A student at UHD heard the following statement made about the Business Cornerstone class. "Oh no!--that teacher is BORING!" Susie realized that this statement does not meet the intellectual standard of __________ and asked more questions before making her mind up about taking that teacher. accuracy significance precision relevance 2- Based on the information you learned in Blackboard on resumes, you should only go back approximately 10 years in listing jobs on your resume. True False 3- All reasoning...
Wiremu operates a small business selling natural health products. He recently heard about one of the...
Wiremu operates a small business selling natural health products. He recently heard about one of the latest trends - an expensive new organic herbal tea called T4. While looking through a trade magazine Wiremu saw an advertisement, promoting the supply of T4 by a company called Eaze Supplies. The advertisement stated, amongst other things, “Great Offer” and “Prices start at $100 per kg”. Wiremu phoned up Eaze Supplies and spoke to Sally who was Head of Sales, and asked whether...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT