why is it faster to process a sorted array than an unsorted array? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 2

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

Need help

25th May 2019, 11:27 AM
Parth Lakhani
Parth Lakhani - avatar
1 Réponse
+ 2
"Process" as in ...? If you want to find the sum of all elements it doesn't make a difference whether the elements are sorted or not. If you want to find the number 6 in an array of sorted integers like {1, 2, 2, 4, 7, 8, 8, 8, 8, 9}, you can begin from the left and stop at 7 because that tells you that there won't be a 6 anywhere in the array (linear search). If the array wasn't sorted, you'd have to check every number in the array to make sure that there isn't a 6 hiding anywhere in the array, so that would take slightly more time. Other algorithms like binary search won't work at all with an array that isn't sorted.
25th May 2019, 12:00 PM
Anna
Anna - avatar