Intermediate·Concept·2 min read

Sorting and searching in C#

Practice this question

Sorting

csharp
Array.Sort(arr);
Array.Sort(arr, (a, b) => b.CompareTo(a));
csharp
public 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 17_DSA_DOTNET.md · “11. Sorting and searching in C#”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.