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...
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...
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...
Need this program in python. The data must be taken from user as input. Write a...
Need this program in python. The data must be taken from user as input. Write a program that prompts the user to select either Miles-to-Kilometers or Kilometers-to-Miles, then asks the user to enter the distance they wish to convert. The conversion formula is: Miles = Kilometers X 0.6214 Kilometers = Miles / 0.6214 Write two functions that each accept a distance as an argument, one that converts from Miles-to-Kilometers and another that converts from Kilometers-to-Miles The conversion MUST be done...
write modules to sort by 2 field then 3 fields then 4 fields Use the data...
write modules to sort by 2 field then 3 fields then 4 fields Use the data Structure to guide you. //HW sort by 5 of these fields and ask the user which field to sort by !!! // Use a custom comparator. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; class Emprec { String name; String address; double hours; double rate; int dependents; char gender; boolean degree; // This is the classes's constructor !!!! Emprec(String name, String address, String hours,String...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT