Tell me the difference between a Comparison and Non-Comparison Sorting Algorithm?
Having a deep knowledge of how all five Sorts work and their differences is a question I would be happy to answer. The four Sorts are Quick Sort, Insertion Sort, Shell Sort, Heap Sort, and Merge Sort. Let me start with the Shell Sort: It's quite different from the others because it's an in-place non-recursive algorithm that compares very well to the other algorithms. Heap Sort: It's an in-place non-recursive sorting algorithm. It's not the fastest, but it's the de-facto sorting algorithm to ensure that the sorting will not take longer than O(nlogn). Insertion Sort: It's only good for smaller arrays (usually less than 100 items). Quick Sort: It's the most popular sorting algorithm of the group. It sorts in place and is usually very fast. Merge Sort: It's a truly O(nlogn) algorithm, and it's stable.
This is actually a common interview question asked of software developers. It probes which sorting algorithm is fastest. This question doesn't have an easy or unambiguous answer. On the one hand, the speed of sorting can depend on the environment in which the sorting is done, and on the other hand, it can depend on the type of items that are sorted and the distribution of these particular items. For example, if you are sorting a large database that cannot fit into memory all at once, this would be quite different from sorting an array of 100 integers. Adding to that, not only will the implementation of an algorithm be quite different, but it may even be the same algorithm. It might also help to know the five Sorts that will likely be brought up in an interview. They are: Quick Sort, Insertion Sort, Shell Sort, Heap Sort, and Merge Sort. Study them and research examples of how they are used and the differences between them. I will give examples that you can use here as well.
"One of the first things that I do before making any comparison is to use a test environment to test the speed of the different sorting algorithms (Comparison and Non-Comparison) in this case. I test each algorithm several times for randomly generated arrays to gather the most accurate data before proceeding to the next step in the process. The next step is to look for random numbers between 0 and 10 times the array size to create array content. I may or may not do a high-repetition test with numbers between 0 and 1/100 times in the same context. After I do this, the results may come back as completely random, sorted, or reversed. I will also run some test-cases to determine the number of repetitions from low to high for each value if the array repeats."
Unlock all 30 Software Developer questions
Unlock expert responses to technical and behavioral questions interviewers ask developers.
Get Started