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
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
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...
Exercise 4–Timing Sorting AlgorithmCollect the run times for either selection sort or insertion sort (use random...
Exercise 4–Timing Sorting AlgorithmCollect the run times for either selection sort or insertion sort (use random values for an array and sorted values; sorted the same list twice and collect time each time) for the following array sizes: 1000, 2000, and 10000. You should be able to confirm that the runtime is n^2 for unsorted list (i.e., going from 1000 to 2000 should be about 4 times slower and going from 1000 to 10000 should be about 100times slower). Question...
Question: Rotate and sort the list:-   In this problem, you have to first implement a singly...
Question: Rotate and sort the list:-   In this problem, you have to first implement a singly linked list whose each node should have the following attributes, ● key - a positive integer ● next - address/pointer/reference to the next node in the linked list You will receive Q1 queries of following types, ● 1 x - Append a node to the linked list whose key should be x. After appending, print, in a new line, the key of each node...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT