Question

In: Computer Science

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?

Solutions

Expert Solution

1.

Insertion sort splits an array into 3 parts as the current element, sorted part and an unsorted part.Sorted part inclueds the elements prior to the current element and unsorted part inculdes elements next to the current element.

The current element is compared against each of its previous elements in the sorted part and greater elements are moved one position up to make space for the currrent element.

Performing insertion sort on singly linked lists will be a bad idea ,since singly linked lists can be traversed in forward direction only and not in backward direction.

2.

The classmate is wrong .

Input arrays to the merge operation of merge sort needs to be in sorted order ,otherwise the runtime may furthur increase.

In merge sort, an array is divided into two smaller subarrays recursively till a single element is reached and then the subarrays are compared and merged.Let's see an example,

Consider the array with values {14,8,9,24,5,1}

Now after the divide step is completed, the resulting sub arrays are combined as follows,

Here at each step, elements of a subarray are compared against elements of another subarray while getting merged , since each subarray is sorted.If the subarrays were unsorted ,the elements within each subarray might have to be compared against one another too.And this have to be repeated at each step and the time taken for merge sort will be higher.

When the input arrays to the merge operation are sorted , number of comparisons of elements needs to be performed at each step decreases tremendously and hence the time required to perform merge sort also decreases.

So, input arrays to the the merge operation of merge sort needs to be in sorted order.


Related Solutions

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...
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 pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 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 }     }...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
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...
One way to improve quick sort is to use an insertion sort on lists that have...
One way to improve quick sort is to use an insertion sort on lists that have a small length (call it the “partition limit”). Why does this make sense?
1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1,...
1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1, 0, 2, 1, 5, 0, 4                                                                     No code or programs, please. Manually solve the problem, please. Thanks
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java...
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java program.
1) Use insertion sort to sort: 25, 17, 31, 13, 2. Show your work step-by-step. 2)...
1) Use insertion sort to sort: 25, 17, 31, 13, 2. Show your work step-by-step. 2) Use Euclidean algorithm to find gcd(248, 198) 3) (ABCDEF)16 to binary, octal and decimal
A template (generic) function for insertion sort is given to you. Part-1: design a function to...
A template (generic) function for insertion sort is given to you. Part-1: design a function to compare 2 strings using case-insensitive comparison, a function to test if 2 records are not equal (using case-insensitive string comparison). Part-2: define your own compare function required by the template insertionSort function. #include <iostream> #include <string> #include <cstdlib> #include <iomanip> #include <ctime> #define BASE 1000000000LL // long long int using namespace std; struct record { long long value; string str; }; // Part-1: case-insensitive...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT