Why is processing a sorted array faster than processing an unsorted array?
In this sneppet let’s learn why is processing a sorted array is faster than processing an unsorted array. Modern CPU design and branch prediction enable faster processing of sorted arrays. The architecture of modern CPUs, combined with their use of branch prediction, allows for more efficient processing of sorted arrays compared to unsorted ones, resulting in improved performance.
Processing a sorted array faster than processing an unsorted array
Whenever CPU come across a conditional statement like an if statement, then it will try to predict which branch will be taken. Once the prediction is done and it is correct, the CPU will execute the code more efficiently.
In case of sorted array processing, the CPU makes more accurate branch predictions because the data is already sorted/ organized which can be predicted easily. Hence there is a lesser chance for mispredictions which can slow down the CPU.
But, in case of an unsorted array the data is no sorted/ organized and have more random pattern which can hinder branch prediction. Hence it will make it harder for CPU to make accurate branch predictions. That’s why processing an unsorted array may result in more mispredictions which in turn slow down the CPU performance.
Additionally, some algorithms, like binary search, are specifically designed to take advantage of sorted data and can run much faster on sorted arrays.
In a sorted array, binary search can find an element in O(log n) time by repeatedly dividing the search space in half. This is much faster than linear search, which has a time complexity of O(n) for both sorted and unsorted arrays.
Branch Prediction Example: Sorted Array Vs Unsorted Array
Here’s an example that demonstrates the impact of branch prediction on performance when searching for an element in a sorted array versus an unsorted array.
public class BranchPredictionExample { public static void main(String[] args) { int[] sortedArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int[] unsortedArray = {3, 1, 4, 2, 6, 8, 5, 9, 7, 10}; int target = 5; // Sorted array search long startTime = System.nanoTime(); int index = binarySearch(sortedArray, target); long endTime = System.nanoTime(); System.out.println("Sorted array search time: " + (endTime - startTime) + " ns"); // Unsorted array search startTime = System.nanoTime(); index = linearSearch(unsortedArray, target); endTime = System.nanoTime(); System.out.println("Unsorted array search time: " + (endTime - startTime) + " ns"); } public static int binarySearch(int[] arr, int target) { int left = 0; int right = arr.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; } public static int linearSearch(int[] arr, int target) { for (int i = 0; i < arr.length; i++) { if (arr[i] == target) { return i; } } return -1; } }
Output:
Sorted array search time: 110 ns Unsorted array search time: 520 ns
In this example, we have two arrays: a sorted array and an unsorted array. We search for the same target element (5) in both arrays using two different search algorithms:
- Binary search: This algorithm takes advantage of the sorted array and uses branch prediction to quickly find the target element.
- Linear search: This algorithm searches the unsorted array sequentially and does not benefit from branch prediction.
The results show that the binary search on the sorted array is significantly faster than the linear search on the unsorted array. This is because the branch predictor is able to accurately predict the outcome of the binary search, reducing the number of mispredictions and improving performance.
However, please note that the performance difference between sorted and unsorted arrays is usually only significant for very large datasets or in performance-critical code.
- Sort an array of objects by a string property value ?
- Python Programs to Print Patterns – Pyramid, Triangle using Star
- Java Palindrome Program- Check String or Number is Palindrome
- Read property value from application properties in Java?
- Convert single digit number to double digits string
- Find the greatest of three numbers
- Most Common Element in an Unsorted Array
- Get Common Elements between Two Unsorted Arrays
- Implement Stack using Linked List
- Convert negative to positive number in Python
- TypeError: a bytes-like object is required, not ‘str’ – Python3
- Extract numbers from a string in python
- Java String substring() example program
- Find which users belongs to a specific group in linux
- print multiplication table for any number
- Check if Python Object is a Number ?
- In-place merge sort instead of normal merge sort ?
- Ways to print arrays in Java
- NullPointerException ? What causes them and how to avoid them? Java