Introduction to Algorithms
Introduction to Algorithms
3rd Edition
ISBN: 9780262033848
Author: Thomas H. Cormen, Ronald L. Rivest, Charles E. Leiserson, Clifford Stein
Publisher: MIT Press
Question
Book Icon
Chapter 2, Problem 1P

(a)

Program Plan Intro

To show that the insertion sort can sort the n/k sub lists, each of length k in Θ(nk) time.

(a)

Expert Solution
Check Mark

Explanation of Solution

The procedure of insertion sort in non-increasing order is as below:

INSERTION-SORT(A)

For j=2 to A .length

  key=A[j]

  i=j1

while i>0 and A[i]<key

  A[i+1]=A[i]

  i=i1

  A[i+1]=key

The j loop is running from 2 to the length of the array and then value in the index of array is stored in the key variable. After that the variable j is decreased by 1 and stored in the variable i.

Now, while loop is used in which two conditions are given: first condition is variable i is greater than 0 and the value of the array at index i must be less than key value.

The value at index i is swapped at the index i1 . The variable i is decreased by 1 and the key value is stored at the index i+1 . The only change in the insertion sort of non-decreasing order to non-increasing order is flip the condition.

Analysis:

The worst case running time of the insertion sort to sort a list of length n is Θ(n2) . Therefore, for sorting the n/k

sub lists of length k , the time taken by insertion sort is calculated as follows:

  Θ(k2nk)=Θ(nk)

Hence, the worst case running time of insertion sort to sort the n/k sub lists, each of length k is Θ(nk) .

(b)

Program Plan Intro

To show that the worst case running time to merge the sub lists is Θ(nlg(n/k)) .

(b)

Expert Solution
Check Mark

Explanation of Solution

There are n/k sub lists of length k and then merge the n/k sorted sub lists in a list of length n .

For this, take two sublists and merge them simultaneously. The time taken by this process is lg(n/k) steps. Now, compare n elements in each step.

Hence, the worst case running time to merge the sub listsis Θ(nlg(n/k)) .

(c)

Program Plan Intro

To find the largest value of k as a function of n so that the modified algorithm has same running time as standard merge sort.

(c)

Expert Solution
Check Mark

Explanation of Solution

Merge sort is a sorting algorithm that uses divide and conquer method. In merge sort, divide the input sequence in two halves and then sort them, Finally, merge the sorted halves.

The modified algorithm has same running time as standard merge sort if Θ(nlg(n/k))=Θ(nlgn) and consider that k=Θ(lgn) .

  Θ(nlg( n/k ))=Θ(nk+nlgnnlgk)=Θ(nlgn+nlgnnlg( lgn))=Θ(2nlgnnlg( lgn))=Θ(nlgn)

Hence, the running time of the modified algorithm has same as standard merge sort if Θ(nlg(n/k))=Θ(nlgn) and k=Θ(lgn) .

(d)

Program Plan Intro

To choose the value of k in practice.

(d)

Expert Solution
Check Mark

Explanation of Solution

Merge sort is a sorting algorithm that uses the divide and conquer method. In merge sort, divide the input sequence in two halves and then sort them. Finally, merge the sorted halves.

For considering the value of k, choose the largest length of sub list as kso that insertion sort becomes faster than merge sort.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
2.The design of Divide & Conquer can be simplified to General Recurrence Equation T(n) = a * T(n/b) + combining time. : D&C solves a problem of size ‘n’ by resursively solving ‘a’ subproblems of size ‘n/ b’. List the Recurrence of Binary Search, Merge Sort, Quick Sort that I mentioned in the talk Equations Please explain how a and b are set
Show how quick sort sorts the following sequences of keys in ascending order. 12, 25, 35, 43, 48, 59, 77, 85, 86, 94? b) Discuss the time complexity of the quick sort algorithm for the above case?
Title: Comparison of Exhaustive, Greedy, and Dynamic Programming approaches for the Partition problem.Description: In this assignment, we are given a set S containing n integers and we are expected to output a partition with two subsets such that they have the minimumdifference in their sums. For this purpose, we can use the simple exhaustive approach, which takes every subset A of S and compare the difference d of the sums: d =sum(A)-sum(S-A). The greedy approach is to use the numbers in S in pairs in descending order as described in class (you can also see the Wikipedia entry for thepartition problem). The dynamic programming algorithm that we covered in class is also to be implemented.We are also asked to compare the three algorithms in their runtime (in msecs) and difference found (d) for various values of k=log2(n). Your program will produce theseperformance measures, and then using any utility such as Excel, draw the plots of their runtimes versus k and d versus k (use…
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education