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
+ 6

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

27th Dec 2016, 2:50 PM
Gautam Khattri
Gautam Khattri - avatar
7 Réponses
+ 7
With a sorted array we have knowledge of certain information about the data set, such as the maximum being at the front or end of the array, if it is descending or ascending respectively. This is the same with the minimum as it will be the opposite from the maximum. Now if we had to find the range of this array, largest - smallest, it only takes us two operations to do so, i.e first minus last or last minus first. In an unsorted array we have no idea where the largest and smallest element is. For example finding the range would require us to loop through the entire array to find both the largest and smallest. Going into some basic time complexity this takes us N amount of operations, as we had to loop through the entire array. Compared to only two operations it is much faster (if the data set had 1 million items in it, it makes a big difference). With this understanding you can move into advanced topics such as Binary Search, which only works on a sorted list and topics such as Pre-Sorting which is an algorithm development strategy.
27th Dec 2016, 3:45 PM
Greg Ortiz
Greg Ortiz - avatar
+ 4
It is like what @Greg stated. In a nutshell, it would just be like: Telling a guy to search the letter A from a disorganized list of alphabets VS Telling a guy to search the letter A from an organized list of alphabets
1st Jan 2017, 7:16 AM
Wen Qin
Wen Qin - avatar
+ 1
because there is no time taken for sorting the array in sorted array.but in unsorted array it will be sort first and then process it
1st Jan 2017, 9:10 AM
harish tati
0
it might not be faster. depends on what you wanna do . if you want to add 3 to each element then it can be sorted or not, you still need to go trough each element in turn
1st Jan 2017, 12:09 PM
Seckar
Seckar - avatar
0
You can also formulate better algorithms to manipulate your array when it is sorted :)
1st Jan 2017, 1:55 PM
Erwin Mesias
Erwin Mesias - avatar
0
take a real world example if a give you a list of all countries in the world then I told you find you the country like India. I will give you 2 list list 1 will be in sorted order list 2 will be in unsorted order which one will be your fastest way to find the country?
1st Jan 2017, 4:02 PM
prashant kb
prashant kb - avatar
0
what shall i do to learn C++
1st Jan 2017, 9:58 PM
omar