# Processing a sorted array is faster than an unsorted one?

DevFeed: [Processing a sorted array is faster than an unsorted one?](<https://devfeed.tech/articles/processing-a-sorted-array-is-faster-than-an-unsorted-one-25311.md>)

Original publisher: [Read original article](<https://kau.sh/blog/processing-sorted-array-is-faster-than-processing-unsorted-array/>)

Author: Kaushik Gopal

Published: 2019-06-08T07:00:00Z

Content type: article

Language: en

Sources: [Kaushik Gopal's Site](<https://devfeed.tech/sources/kaushik-gopal-s-site.md>)

Topics: [data](<https://devfeed.tech/topics/data.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Stack Overflow](<https://devfeed.tech/topics/stackoverflow.md>)

Tags: [data](<https://devfeed.tech/tags/data.md>), [programming](<https://devfeed.tech/tags/programming.md>)

## AI overview

A sorted array can be faster to process than an unsorted array because its values make a conditional branch easier for the processor to predict. In the example, data[c] >= 128 is false for an initial streak and true afterward, while an unsorted array causes more branch-prediction costs.

## Source excerpt

This super intesting stack overflow answer explains why -in programming- if you have a sorted array, somehow magically it can seem like it's easier to process each element vs processing the same array if it were unsorted. tl;dr - branch prediction With a sorted array, the condition data[c] >= 128 is first false for a streak of values, then becomes true for all later values. That's easy to predict. With an unsorted array, you pay for the branching cost.