Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it worth to sort an array? [closed]

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

  • O(n) if not sorted
  • O(log(n)) if sorted

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.

  • Cost of searching x times in sorted array = O(n*log(n)) + [O(log(n)) * x]
  • Cost of searching x times in unsorted array = O(n) * x

What would be the value of x?

like image 484
Juan Antonio Gomez Moriano Avatar asked Dec 29 '13 22:12

Juan Antonio Gomez Moriano


People also ask

What is the most efficient way of sorting an array?

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.

Why is it necessary to sort an array?

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.

Is it necessary to sort array before binary?

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.

Do arrays need to be sorted?

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).


1 Answers

Personally I would answer, that it is worth sorting an array if any of the following is true:

  • we plan to often ask for the biggest value in the array (cut cost from O(n) to O(1)),
  • we plan to often ask for the smallest value in the array (cut cost from O(n) to O(1)),
  • we will often seek for a given value in the array (cut cost from O(n) to O(log(n)).

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.

like image 72
3yakuya Avatar answered Oct 07 '22 03:10

3yakuya