R
R
redq2021-11-05 22:05:37
Algorithms
redq, 2021-11-05 22:05:37

Which sorting algorithm to choose?

There are three arrays: 1. array of random numbers 2. partially sorted array 3. sorted array.
Which sorting algorithm (Selection sort / bubble sort / insertion sort) will be the most beneficial for each of the arrays and why?

As far as I understand, for an array of random numbers, it will be more profitable to apply selection sort.
For a partially sorted array, either selection sort or insertion sort will do.
And for a sorted array, insertion sort would be better.

Am I right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dmshar, 2021-11-05
@dmshar

Well, you answered your own question. Any textbook on algorithm theory has information on each of the sorting algorithms you listed (and others), which tells the reader what computational complexity each of the algorithms has for the best (everything is already sorted), average and worst cases.
Another thing is that you can apply this knowledge not when sorting a specific array, but when you know that you need to sort the array often, and as a rule (or just more often) your array will already be sorted, partially sorted, etc. (This is very often the case when solving specific applied problems).
And for a specific array, without knowing in advance what kind of data comes to the input, nothing concrete can be said. Therefore, in this case, they are guided by the "average" case.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question