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
Hands-On Assignments Part II Assignment 1-5: Querying the DoGood Donor Database Review the DoGood Donor data by writing and running SQL statements to perform the following tasks: 1. List each donor who has made a pledge and indicated a single lump sum payment. Include first name, last name, pledge date, and pledge amount. 2. List each donor who has made a pledge and indicated monthly payments over one year. Include first name, last name, pledge date, and pledge amount. Also, display the monthly payment amount. (Equal monthly payments are made for all pledges paid in monthly payments.) 3. Display an unduplicated list of projects (ID and name) that have pledges committed. Don't display all projects defined; list only those that have pledges assigned. 4. Display the number of pledges made by each donor. Include the donor ID, first name, last name, and number of pledges. 5. Display all pledges made before March 8, 2012. Include all column data from the DD PLEDGE table.
Write a FancyCar class to support basic operations such as drive, add gas, honk horn, and start engine. FancyCar.java is provided with method stubs. Follow each step to gradually complete all methods. Note: This program is designed for incremental development. Complete each step and submit for grading before starting the next step. Only a portion of tests pass after each step but confirm progress. The main() method includes basic method calls. Add statements in main() as methods are completed to support development mode testing. Step 0. Declare private fields for miles driven as shown on the odometer (int), gallons of gas in tank (double), miles per gallon or MPG (double), driving capacity (double), and car model (String). Note the provided final variable indicates the gas tank capacity of 14.0 gallons. Step 1 (2 pts). 1) Complete the default constructor by initializing the odometer to five miles, tank is full of gas, miles per gallon is 24.0, and the model is "Old Clunker". 2)…
Find the error: daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday',                     'Wednesday', 'Thursday', 'Friday',                     'Saturday'] for i in range(7):         daily_sales[i] = float(input('Enter the sales for ' \                                      + day_of_week[i] + ': ')
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning