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

Here is a C++ code that illustrates that sorting the data miraculously makes the code faster than the unsorted version. Let’s try out a sample C++ program to understand the problem statement better.

`// CPP program to demonstrate processing ` `// time of sorted and unsorted array ` `#include <iostream> ` `#include <algorithm> ` `#include <ctime> ` `using` `namespace` `std; ` ` ` `const` `int` `N = 100001; ` ` ` `int` `main() ` `{ ` ` ` `int` `arr[N]; ` ` ` ` ` `// Assign random values to array ` ` ` `for` `(` `int` `i=0; i<N; i++) ` ` ` `arr[i] = ` `rand` `()%N; ` ` ` ` ` `// for loop for unsorted array ` ` ` `int` `count = 0; ` ` ` `double` `start = ` `clock` `(); ` ` ` `for` `(` `int` `i=0; i<N; i++) ` ` ` `if` `(arr[i] < N/2) ` ` ` `count++; ` ` ` ` ` `double` `end = ` `clock` `(); ` ` ` `cout << ` `"Time for unsorted array :: "` ` ` `<< ((end - start)/CLOCKS_PER_SEC) ` ` ` `<< endl; ` ` ` `sort(arr, arr+N); ` ` ` ` ` `// for loop for sorted array ` ` ` `count = 0; ` ` ` `start = ` `clock` `(); ` ` ` ` ` `for` `(` `int` `i=0; i<N; i++) ` ` ` `if` `(arr[i] < N/2) ` ` ` `count++; ` ` ` ` ` `end = ` `clock` `(); ` ` ` `cout << ` `"Time for sorted array :: "` ` ` `<< ((end - start)/CLOCKS_PER_SEC) ` ` ` `<< endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output :**

Execution 1:Time for unsorted array :: 0.00108 Time for sorted array :: 0.00053Execution 2:Time for unsorted array :: 0.001101 Time for sorted array :: 0.000593Execution 3:Time for unsorted array :: 0.0011 Time for sorted array :: 0.000418

Observe that time taken for processing a sorted array is less as compared to unsorted array. The reason for this optimisation for sorted array is **branch prediction.**

**What is branch prediction ?**

In computer architecture, branch prediction means determining whether a conditional branch(jump) in the instruction flow of a program is likely to be taken or not.

All the pipelined processors do branch prediction in some form, because they must guess the address of the next instruction to fetch before the current instruction has been executed.

**How branch prediction in applicable on above case ?**

The **if** condition checks that **arr[i] < 5000**, but if you observe in case of sorted array, after passing the number 5000 the condition is always false, and before that it is always true, compiler optimises the code here and skips the if condition which is referred as branch prediction.

**Case 1 : Sorted array**

T = if condition true F = if condition false arr[] = {0,1,2,3,4,5,6, .... , 4999,5000,5001, ... , 100000} {T,T,T,T,T,T,T, .... , T, F, F, ... , F }

We can observe that it is very easy to predict the branch in sorted array, as the sequence is **TTTTTTTTTTTT………FFFFFFFFFFFFF**

** Case 2 : Unsorted array**

T = if condition true F = if condition false arr[] = {5,0,5000,10000,17,13, ... , 3,21000,10} {T,T,F, F, T, T, ... , T, F, T}

It is very difficult to predict that **if** statement will be false or true, hence branch prediction don’t play any significant role here.

Branch prediction works on the pattern the algorithm is following or basically the history, how it got executed in previous steps. If the guess is correct, then CPU continue executing and if it goes wrong, then CPU need to flush the pipeline and roll back to the branch and restart from beginning.

In case compiler is not able to utilise branch prediction as a tool for improving performance, programmer can implement his own hacks to improve performance.

**References :**

This article is contributed by **Mandeep Singh**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Find index of first occurrence when an unsorted array is sorted
- Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Sort an array where a subarray of a sorted array is in reverse order
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Check if reversing a sub array make the array sorted
- Maximum in an array that can make another array sorted
- Program for Mean and median of an unsorted array
- k-th missing element in an unsorted array
- K'th Smallest/Largest Element in Unsorted Array | Set 1
- Print all pairs in an unsorted array with equal sum
- Find floor and ceil in an unsorted array
- Find k closest numbers in an unsorted array
- Search, insert and delete in an unsorted array
- Find the two numbers with odd occurrences in an unsorted array