+ 9

Why is it faster to process a sorted array than an unsorted array ?

16th Mar 2017, 5:56 AM
Mallesh Techie
Mallesh Techie - avatar
3 odpowiedzi
+ 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).
16th Mar 2017, 6:23 AM
Álvaro
+ 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
16th Mar 2017, 7:25 AM
EagleEye
EagleEye - avatar
+ 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
16th Mar 2017, 4:51 PM
Gaurav Sharma
Gaurav Sharma - avatar