A Way To Improve Sorting Algorithms? - sorting

For sorting algorithms, why can't you just cut the array in half, and just use selection sort or insertion sort on both, and put them back together, to significantly improve the speed?

You're saying that your algorithm is faster than existing sorts, for example, selection sort and insertion sort. But then, once you've split your array in half, you'd be better using your algorithm rather than selection/insertion sort to sort the halves (perhaps unless the halves are small).
This is exactly merge-sort.

You are right. This approach is followed in some sorting algorithms. For example in Merge sort which divides the array into two halves and if these two halves are small, you can apply insertion sort directly on them but if they are large then it would not be feasible as you better divide the halves too (please see the details of Merge sort) . Insertion sort/Selection sort/Bubble sort perform better when array is small or generally on nearly sorted data. If you are tackling long data then choose Merge sort/Quick sort/Redix sort.

Related

In what situations are slower sorting algorithms (bubble sort, selection sort, etc) more useful than faster algorithms such Quicksort?

I just wrote an essay about the efficiency and usefulness of different sorting algorithms. I concluded that merge sort and quicksort were far better when sorting completely randomized lists. I just wanted to ask in what situations would the slower sorting algorithms for this scenario (bubble sort and selection sort) be more useful or as useful as quicksort and merge sort.
Notice that both merge sort and quicksort may require additional memory, whether this is heap space required to save the stack for recursion or actual copy of the buffer.
Bubble sort and selection sort does not require additional memory. So in situations where memory is a strict restriction they would be used.
Recursion involved in quick sort or merge can be expensive when we are soting quite a few numers (may be less than 100)
In this case insertion sort performs much better.
Even the standard implementations of sorting libraries in Java uses a mix of quick-merge and insertion sort
HTH
There are stable sorting algorithms which keep items that compare equal arranged in the original order. If this is important to you, then you would use a stable sorting algorithm even if slower.
There are some cases where simpler algorithms are faster (because obviously a slower algorithm is never more useful if it doesn't have other advantages).
I have seen one situation where an array was sorted, and then each array element was modified by a tiny amount. So most items were in the right position, and only a few needed exchanging. In that case shakersort proved optimal.
If you know that the array was sorted, and then a small number of items was changed, there is a clever algorithm for that (which you find somewhere on cs.stackexchange.com): If k items have been changed, you can extract at most 2k items into a separate array, sort that (using Quicksort most likely) and merge the two arrays.
If you use a library function, it is unlikely to be plain Quicksort. For example, Apple's implementation looks for a sorted range at the beginning and the end of the array, and if there are substantial numbers of items already sorted, takes advantage of this (for example sorting the concatenation of two sorted arrays runs in linear time).

Combining Merge and Insertion Sort

Is there a way to combine merge and insertion sort in such a way that the new algorithm is faster than each algorithm (merge and insertion sort) individually? I've been thinking about it for the past couple of hours but all I can come up with is once the division of the array in merge sort reaches a certain threshold (i.e. length of 10) then insertion sort kicks in and sort the array. But that won't make the algorithm faster it'll just reduce overhead. Any ideas regarding this?

Better sorting technique for almost sorted list

Which is the best sorting technique for an almost sorted list and why? For a list of many elements I found that quick sort is helpful.But which sort have a better running time when it is already sorted?
Insertion Sort has linear performance when list is almost sorted.
Check : http://www.sorting-algorithms.com/nearly-sorted-initial-order
Insertion places an element at correct position considering the relative ordering hence need n - 1 iterations. Other sorts like quick sort again tries to sort a sorted segment again as it is unaware of relative ordering (ie if segment is already sorted).
Your Question is too broad , however If the input array is already sorted, insertion sort performs as few as n-1 comparisons thats make insertion sort more efficient
you may find this paper helpful
Best sorting algorithm for nearly sorted lists

About bubble sort vs merge sort

This is an interview question that I recently found on Internet:
If you are going to implement a function which takes an integer array as input and returns the maximum, would you use bubble sort or merge sort to implement this function? What if the array size is less than 1000? What if it is greater than 1000?
This is how I think about it:
First, it is really weird to use sorting to implement the above function. You can just go through the array once and find the max one.
Second, if have to make a choice between the two, then bubble sort is better - you don't have to implement the whole bubble sort procedure but only need to do the first pass. It is better than merge sort both in time and space.
Are there any mistakes in my answer? Did I miss anything?
It's a trick question. If you just want the maximum, (or indeed, the kth value for any k, which includes finding the median), there's a perfectly good O(n) algorithm. Sorting is a waste of time. That's what they want to hear.
As you say, the algorithm for maximum is really trivial. To ace a question like this, you should have the quick-select algorithm ready, and also be able to suggest a heap datastructure in case you need to be able to mutate the list of values and always be able to produce the maximum rapidly.
I just googled the algorithms. The bubble sort wins in both situations because of the largest benefit of only having to run through it once. Merge sort can not cut any short cuts for only having to calculate the largest number. Merge takes the length of the list, finds the middle, and then all the numbers below the middle compare to the left and all above compare to the right; in oppose to creating unique pairs to compare. Meaning for every number left in the array an equal number of comparisons need to be made. In addition to that each number is compared twice so the lowest numbers of the array will most likely get eliminated in both of their comparisons. Meaning only one less number in the array after doing two comparisons in many situations. Bubble would dominate
Firstly I agree with everything you have said, but perhaps it is asking about knowing time complexity's of the algorithms and how the input size is a big factor in which will be fastest.
Bubble sort is O(n2) and Merge Sort is O(nlogn). So, on a small set it wont be that different but on a lot of data Bubble sort will be much slower.
Barring the maximum part, bubble sort is slower asymptotically, but it has a big advantage for small n in that it doesn't require the merging/creation of new arrays. In some implementations, this might make it faster in real time.
only one pass is needed , for worst case , to find maximum u just have to traverse the whole array , so bubble would be better ..
Merge sort is easy for a computer to sort the elements and it takes less time to sort than bubble sort. Best case with merge sort is n*log2n and worst case is n*log2n. With bubble sort best case is O(n) and worst case is O(n2).

Insertion sort better than Bubble sort?

I am doing my revision for the exam.
Would like to know under what condition will Insertion sort performs better than bubble sort given same average case complexity of O(N^2).
I did found some related articles, but I can't understand them.
Would anyone mind explaining it in a simple way?
The advantage of bubblesort is in the speed of detecting an already sorted list:
BubbleSort Best Case Scenario: O(n)
However, even in this case insertion sort got better/same performance.
Bubblesort is, more or less, only good for understanding and/or teaching the mechanism of sortalgorithm, but wont find a proper usage in programming these days, because its complexity
O(n²)
means that its efficiency decreases dramatically on lists of more than a small number of elements.
Following things came to my mind:
Bubble sort always takes one more pass over array to determine if it's sorted. On the other hand, insertion sort not need this -- once last element inserted, algorithm guarantees that array is sorted.
Bubble sort does n comparisons on every pass. Insertion sort does less than n comparisons: once the algorithm finds the position where to insert current element it stops making comparisons and takes next element.
Finally, quote from wikipedia article:
Bubble sort also interacts poorly with modern CPU hardware. It
requires at least twice as many writes as insertion sort, twice as
many cache misses, and asymptotically more branch mispredictions.
Experiments by Astrachan sorting strings in Java show bubble sort to
be roughly 5 times slower than insertion sort and 40% slower than
selection sort
You can find link to original research paper there.
I guess the answer you're looking for is here:
Bubble sort may also be efficiently used on a list that is already
sorted except for a very small number of elements. For example, if
only one element is not in order, bubble sort will take only 2n time.
If two elements are not in order, bubble sort will take only at most
3n time...
and
Insertion sort is a simple sorting algorithm that is relatively
efficient for small lists and mostly sorted lists, and often is used
as part of more sophisticated algorithms
Could you provide links to the related articles you don't understand? I'm not sure what aspects they might be addressing. Other than that, there is a theoretical difference which might be that bubble sort is more suited for collections represented as arrays (than it is for those represented as linked lists), while insertion sort is suited for linked lists.
The reasoning would be that bubble sort always swaps two items at a time which is trivial on both, array and linked list (more efficient on arrays), while insertion sort inserts at a place in a given list which is trivial for linked lists but involves moving all subsequent elements in an array to the right.
That being said, take it with a grain of salt. First of all, sorting arrays is, in practice, almost always faster than sorting linked lists. Simply due to the fact that scanning the list once has an enormous difference already. Apart from that, moving n elements of an array to the right, is much faster than performing n (or even n/2) swaps. This is why other answers correctly claim insertion sort to be superior in general, and why I really wonder about the articles you read, because I fail to think of a simple way of saying this is better in cases A, and that is better in cases B.
In the worst case both tend to perform at O(n^2)
In the best case scenario, i.e., when the array is already sorted, Bubble sort can perform at O(n).

Resources