Question

In: Computer Science

Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1]...

  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 }

    }

}

  1. Illustrate the algorithm on the following array by showing each comparison/swap operation. What is the total number of comparisons made for this worst-case data?

A = (5, 4, 3, 2, 1)

  1. Write a recursive version of this algorithm.

  1. Let f(n) be the worst-case number of key comparisons made by this algorithm to sort n elements. Write a recurrence equation for f(n). (Note that the sequence of comparisons are exactly the same for both non-recursive and recursive versions. But, you may find it more convenienet to write the recurrence for the recursive version.)

  1. Find the solution for f(n) by repeated substitution.

  1. Consider the bubble-sort algorithm described below.

void bubble (dtype A[ ], int n)

{ int i, j;

for (i = n − 1; i > 0; i − −)        //Bubble max of A[0..i] down to A[i].

    for (j = 0; j < i; j + +)

        if (A[j] > A[j + 1]) SWAP(A[j], A[j + 1]);

}

  1. Analyze the time complexity, T(n), of the bubble-sort algorithm.

  1. Rewrite the algorithm using recursion.

  1. Let f(n) be the worst-case number of key-comparisons used by this algorithm to sort n elements. Write a recurrence for f(n). Solve the recurrence by repeated substitution (i.e, iteration method).

Solutions

Expert Solution

Hey there!!

a) For this part observe that insertion sorts swap an element from its current position to its sorted position only by swiping it with its immediate neighbor that means for the worst case in which the first element is at the first position and has to be moved to the last position, it will take n-1 comparison now the second element has to be placed second to the last place then it will take n-2 comparisons where n is the size of the array for the third element it would be n-3 than n-4 and so on.

As a result the total number of comparisons would be comp=(n-1)+n-2+(n-3)+.......+(n-n-1)

comp={n(n-1)}/2b)

b)

if n <= 1 
    return; 
insertionSort(arr, n) 

    insertionSort(arr, n-1) 

j = n-2;

while j >= 0 and A[j] > A[n-1]{

    A[j+1]=A[j];

    j = j – 1;
}
A[j+1]=A[n-1];

  

This is the recursive algorithms it calls for til size n-1 until the array size has become 1 then it starts inserting the element to its sorted location after that the function ends and now the function call for array size=2 is called from the stack. The array it receives is an array of size 1 sorted. It then sorts the array of size =2 and function ends . Again the function call of size 3 gets a sorted array of size 2 it sorts this array and then passes it to the function call of array size 4. The sorting part works exactly like a normal insertion sort.

c) The recurrence relation would be T(n) =T(n-1)+O(n)

d)We know that T(1)=T(0)+1=1

then T(2)=T(1)+2=1+2=3

T(3)=T(2)+3=1+2+3=6.......

observing the pattern T(n)=1+2+3+4.....n=(n(n-1))/2 = (n2-n)/2 in terms of Big-Oh notation it would be T(n)=O(n2)

Ans-2 a). For every element, bubble sort does n-1 comparisons. In Big-Oh notation, bubble sort performs O(n)O(n) comparisons. Now for n element, the complexity for each individual element would be O(n) so to sort the whole array the complexity would be no.of elements *complexity to sort each element which gives us O(n2) complexity.

b)

bubbleSort(arr[], n)
{

    for (int i=0; i<n-1; i++)
        if (arr[i] > arr[i+1])
            swap(arr[i], arr[i+1]);

    bubbleSort(arr, n-1);;
} 

After the first call, the greatest element is moved to the last place then we call for n-1 size in which the second greatest element is moved to the second last place then we call for size n-3,n-4, and so on.

c)The recursive relation for bubble sort is the same as insertion sort and it would be solved the same way.

If there is any doubt about the answer please put it in the comments I will get back to you asap.

Please upvote if you like the answer .

Thanks for asking!!


Related Solutions

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.
The Binary Insertion Sort Algorithm is a variation of the Insertion Sort Algorithm that uses a...
The Binary Insertion Sort Algorithm is a variation of the Insertion Sort Algorithm that uses a binary search technique rather than a linear search technique to insert the ith element in the correct place among the previously sorted elements. (i) Express the Binary Insertion Sort Algorithm in pseudocode. (ii) Compare the number of comparisons of elements used by the Insertion Sort Algorithm and the Binary Insertion Sort Algorithm when sorting the list (7,4,3,8,1,5,4,2). (iii) Show that the Insertion Sort Algorithm...
Question: Consider the following variation of insertion sort: For 1 ≤ i < n, to insert...
Question: Consider the following variation of insertion sort: For 1 ≤ i < n, to insert the element A[i] among A[0] ≤ A[1] ≤ … ≤ A[i-1], do a binary search to find the correct position for A[i]. a. How many key comparisons would be done in the worst case? b. How many times are elements moved in the worst case? c. What is the asymptotic order of the worst case running time?
Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ......
Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ... n - 1]) if (n = 2) and A[0] > A[1] swap A[0] and A[1] else if n > 2 m = ceiling(2n/3) STOOGESORT(A[0 ... m - 1]) STOOGESORT(A[n - m ... n - 1]) STOOGESORT(A[0 ... m - 1])
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...
Data Structure and Algorithm: 1. You are asked to use insertion sort to sort a singly...
Data Structure and Algorithm: 1. You are asked to use insertion sort to sort a singly linked list in ascending order. Why is this a bad idea? How does insertion sort’s runtime complexity change if you were to use it to sort a linked list? 2. Your classmate thinks that input arrays to the merge operation of merge sort don’t need to be in sorted order. Is your classmate right or wrong? Why?
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
Consider the following program specification: Input: An integer n > 0 and an array A[0..(n –...
Consider the following program specification: Input: An integer n > 0 and an array A[0..(n – 1)] of n integers. Output: The largest index b such that A[b] is the largest value in A[0..(n – 1)]. For example, if n = 10 and A = [ 4, 8, 1, 3, 8, 5, 4, 7, 1, 2 ] (so A[0] = 4, A[1] = 8, etc.), then the program would return 4, since the largest value in A is 8 and...
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT