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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
- Find index of first occurrence when an unsorted array is sorted
- Merging two unsorted arrays in sorted order
- Merge two unsorted linked lists to get a sorted list
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Why quicksort is better than mergesort ?
- Check if two sorted arrays can be merged to form a sorted array with no adjacent pair from the same array
- Sort a nearly sorted (or K sorted) array
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Maximum number of partitions that can be sorted individually to make sorted
- Merge K sorted Doubly Linked List in Sorted Order
- Count pairs in a sorted array whose sum is less than x
- Count elements less than or equal to a given value in a sorted rotated array
- Find element in a sorted array whose frequency is greater than or equal to n/2.
- Count pairs in a sorted array whose product is less than k
- Find the number of elements greater than k in a sorted array
- Find the two numbers with odd occurrences in an unsorted array
- K'th Smallest/Largest Element in Unsorted Array | Set 1
- K'th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
- K'th Smallest/Largest Element in Unsorted Array | Set 3 (Worst Case Linear Time)