Sorting and searching in Java
Practice this questionWANT TO UNDERSTAND THE CONCEPT FIRST?Learn Sorting and searching in Java
Sorting
Arrays.sort(arr);
Arrays.sort(arr, Collections.reverseOrder());Searching
- Binary search requires sorted input.
Example: binary search
int binarySearch(int[] arr, int target) {
int left = 0, right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}Adapted from 15_DSA_JAVA.md · “11. Sorting and searching in Java”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.