Question

In: Computer Science

4. Suppose you need to sort input data that is almost-sorted, meaning large stretches of the...

4. Suppose you need to sort input data that is almost-sorted, meaning large stretches of the data are already in increasing order with occasional out-of-order elements. Argue that INSERTION-SORT would tend to be more time efficient than QUICKSORT in this situation.

Describe a scenario (other than sorting checks by check number or processing date) in which one might need to sort almost-sorted data.

Solutions

Expert Solution

When the data is almost sorted no doubt Insertion Sort is the best algorithm to sort the data.

We know that in the Insertion Sort we keep on moving from left to right ( from smallest toward largest ) and if we found that the element is less than its previous element, we place it in the correct position and move forward.

Now, when we know the array is almost sorted, we require to place very few elements to its correct location, as very few elements are in the wrong position.

Time Complexity of using Insertion Sort = O(n) where n is the no. of elements in the array

Now, in Quicksort, we know the minimum time complexity of the quicksort is O(nlogn). In the case when the array is almost sorted, the condition may arise that the pivot element we are choosing is bad selection.

Suppose we select the first element as pivot, it will return almost (n-1) as almost all elements are greater than pivot element. And in the next case, it will return (n-2), then (n-3) and so on.

So, in the case of an almost sorted array, the time complexity of the Quicksort will be O(n2).

Hence, Insertion Sort is preferable in the case of an almost sorted array.

Example:

1. Arranging students in the queue according to their height. The student can be guessed simply by watching that is he/she is too tall to be stand at last or at the beginning or in between.

2. Arranging books by their length on the shelf. We can easily determine the correct position of the book if one is inserted incorrectly.

3. Arranging notes (money) when a lot of money you are holding. We can place the incorrectly placed note to the correct place easily.

Leave a comment if face any problem.


Related Solutions

1. Prove by induction on the column being sorted that RADIX-SORT correctly sorts its input. The...
1. Prove by induction on the column being sorted that RADIX-SORT correctly sorts its input. The Induction should include the following steps: Loop Invariant, Initialization, Maintenance, and Termination.
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...
develop the Binary search with swap count: Use sorted data from insertion sort (part A) Permutations,...
develop the Binary search with swap count: Use sorted data from insertion sort (part A) Permutations, Insertion Sort – implement algorithms Permutations (Johnson Trotter): {1, 2, 3, 4, 5}; Insertion Sort: {45, 24, 16, 92, 71, 69, 28} develop count of # data “insertions” and develop # of key compares against the following: 16, 77, 24, 92, 44
Suppose you are the CFO of a large MNCs, and you need to raise funds. What...
Suppose you are the CFO of a large MNCs, and you need to raise funds. What factors do you need to consider in raising funds? How is it different from raising funds for a domestic company in terms of risk and opportunity? What different types of sources can you raise funds for the company? What are the pros and cons of each type? Why is the debt market much larger than the equity market? What is Eurocurrency market? Are spreads...
In this assignment, you need to demonstrate your ability in using input, output, data types, and...
In this assignment, you need to demonstrate your ability in using input, output, data types, and if statement in C++ program. Assume that you need write a C++ program for a cash register. There are only four items in the store: Cereal, $3.99 Milk, $3.99 Egg, $0.25 Water, $ 1.50 Once a customer purchases items, you will ask her/his how many of them are bought. The quantity can be in the range of 0-10 (including 0 and 10). Then, calculate...
Sorting with Binary Search Tree This assignment asks you to sort the lines of an input...
Sorting with Binary Search Tree This assignment asks you to sort the lines of an input file (or from standard input) and print the sorted lines to an output file (or standard output). Your program, called bstsort (binary search tree sort), will take the following command line arguments: % bstsort [-c] [-o output_file_name] [input_file_name] If -c is present, the program needs to compare the strings case sensitive; otherwise, it's case insensitive. If the output_file_name is given with the -o option,...
Suppose as company controller, you need a large influx of cash to develop and market a...
Suppose as company controller, you need a large influx of cash to develop and market a new product that will keep the company afloat. You may be able get a bank loan, but not if you report the current inventory on the now-outmoded product at its true value. If you fudge the numbers and misrepresent the company's financial health, you can get the loan and keep the company going. Here, again, is a situation in which being honest and preserving...
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?
C++ Data Structures 4. Write a client function that merges two instances of the Sorted List...
C++ Data Structures 4. Write a client function that merges two instances of the Sorted List ADT using the following specification. MergeLists(SortedType list1, SortedType list2, SortedType& result) Function: Merge two sorted lists into a third sorted list. Preconditions: list1 and list2 have been initialized and are sorted by key using function ComparedTo. list1 and list2 do not have any keys in common. Postconditions: result is a sorted list that contains all of the items from list1 and list2. c. Write...
I need almost two pages for this section Section #3: Issues Regarding Data Access With the...
I need almost two pages for this section Section #3: Issues Regarding Data Access With the amount of information being stored allowing easier access along with the ability to cross-reference many different sources, do you see any areas needing improvement? This could be achieved either by better organization for easier access or through more regulation on availability. Some examples to consider include the rights for the public to obtain data through the US Freedom of Information Act (FOIA) after a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT