+ 9
Why is it faster to process a sorted array than an unsorted array ?
3 ответов
+ 4
With a sorted array you can take advantage of some optimized search algorithms. For example, if you want to check whether a value is inside you array, with a sorted array you can check the middle element: if it's greater than the searched term, repeat the search on the left half of your array. If less, search on the right side. Repeat until you find the term.
With an unsorted array, however, you must traverse all the elements of the array in order.
The first method is way faster (logarithmic time) than the second method (linear time).
+ 2
like Alvaro says. You can also assume that the first element is the smallest of the array and the last the biggest, so if you need one of these you dont have to search for it one by one
+ 1
a sorted array will have a specific pattern and can be easily coded using a loop or any other function while u can't do that for unsorted ones