Years ago, in a job interview, I was asked: When is it worth to sort an array? I remember not being able to answer properly, recently I did an algorithm course, and I come to the conclusion that providing a more "academical" response would have probably got me that job... anyway, it is not possible to fix the past, so far I am trying to formally answer it to myself, currently, this is where I am:
Given an array, the time to search will be
Considering that quick sort sorts in O(n*log(n))
When is it worth to sort an array? It would of course depend on the number of times we are going to search the array.
What would be the value of x?
Quicksort. Quicksort is generally thought of as the most efficient 'general' sorting algorithm, where nothing is known about the inputs to the array, and it's more efficient than insertion sort on large lists.
It is typically used in computer science to implement static lookup tables to hold multiple values which have the same data type. Sorting an array is useful in organising data in ordered form and recovering them rapidly.
Binary Search is a searching algorithm for finding an element's position in a sorted array. In this approach, the element is always searched in the middle of a portion of an array. Binary search can be implemented only on a sorted list of items. If the elements are not sorted already, we need to sort them first.
It is often necessary to arrange the elements in an array in numerical order from highest to lowest values (descending order) or vice versa (ascending order). If the array contains string values, alphabetical order may be needed (which is actually ascending order using ASCII values).
Personally I would answer, that it is worth sorting an array if any of the following is true:
If it is possible to sort the array in O(n) (for ex., the data fulfills the criteria for counting sort) we would start gaining from search operation (so the total time, including time necessary for sorting, will be smaller than time taken by searching in an unsorted array) after k operations, where k = constantOfSortingOperation / (n/log(n)) (time it took to sort the array divided by gain from searching the sorter array).
If we sort the array in O(nlogn), using for ex. HeapSort or QuickSort (where the constant hidden in big-O notation is small) we would start gaining from search operation after k = (constant*nlogn)/ (n/logn). constant/nlogn is basically how many Times could we search the unsorted array if we spend the time not on sorting, but on searching. n/logn is how much we gain from a single search in a sorter array compared to search in an unsorted array. So if we consider our constant to be small (much smaller than n) the time after we start gaining (= x, more or less) would be approximately n*logn * logn / n = (log(n))^2.
If we include calculations of gains from getting the biggest/smallest value, we start gaining from sorting an array much faster.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With