Sorting is the process of arranging data in a specific order - typically in ascending or descending sequence - based on one or more keys or criteria.
Sorting is a fundamental operation in computer science used to organise, search, analyze, and optimize data processing.
Common Ordering Criteria
1. Numerical Sorting: Sorting based on numeric values, either ascending (low to high) or descending (high to low).
Input: [1, 3, 2, 10, 5]
Output (Ascending): [1, 2, 3, 5, 10]
Output (Descending): [10, 5, 3, 2, 1]
2. Lexicographic (Alphabetical) Sorting: Sorting strings based on the dictionary order, comparing characters from left to right using Unicode or ASCII values.
Input: ["banana", "apple", "zebra", "grape"]
Output: ["apple", "banana", "grape", "zebra"]
3. Custom Key Sorting: Sorting using a custom property or attribute of an object or record, not the entire object itself.
students = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 20},
{"name": "Charlie", "age": 22}
]
Sort by age:
[
{"name": "Bob", "age": 20},
{"name": "Charlie", "age": 22},
{"name": "Alice", "age": 25}
]
4. Multi-Key Sorting (Compound Sort): Sort by multiple keys, in a specified order. If the first keys are equal, the second (or third…) keys determine the order.
people = [
{"first": "John", "last": "Smith"},
{"first": "Alice", "last": "Smith"},
{"first": "Bob", "last": "Anderson"}
]
Sort by last name, then by first name
[
{"first": "Bob", "last": "Anderson"},
{"first": "Alice", "last": "Smith"},
{"first": "John", "last": "Smith"}
]
Stable Sorting
A stable sorting algorithm preserves the relative order of records that have equal keys (or values).
Input:
[
{name: "Alice", age: 25},
{name: "Bob", age: 25},
{name: "Charlie", age: 20}
]
sorting people by age:
A stable sort by age will preserve the original order of Alice and Bob (since both have age 25).
[
{name: "Charlie", age: 20},
{name: "Alice", age: 25},
{name: "Bob", age: 25}
]
Unstable Sorting
An unstable sorting algorithm may change the relative order of records with equal keys.
An unstable sort may return:
[
{name: "Charlie", age: 20},
{name: "Alice", age: 25},
{name: "Bob", age: 25}
]
1. Given an array of integers. Design the following sorting algorithms:
Each algorithms should sort the input array in:
Each algorithms should return both sorted arrays.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | [1, 2, 3, 4, 5] | [5, 4, 3, 2, 1] | [7, 7, 7] / [] |
Output (ASC) | [1, 2, 3, 4, 5] | [1, 2, 3, 4, 5] | [7, 7, 7] / [] |
Output (DESC) | [5, 4, 3, 2, 1] | [5, 4, 3, 2, 1] | [7, 7, 7] / [] |
Explanation | No swaps needed | Maximum number of swaps | Algorithms should handle gracefully without error |
2. Given an array consisting only of integers 0, 1, and 2. Design an algorithm that sort the array in-place.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | [0, 0, 1, 1, 2, 2] | [2, 2, 1, 1, 0, 0] | [0, 0, 0] / [1] / [] |
Output | [0, 0, 1, 1, 2, 2] | [0, 0, 1, 1, 2, 2] | Same as input |
Explanation | Array is already sorted | Requires most swaps | Should not cause errors |
3. Given an array of n integers, where each element is at most k positions away from its correct position in the fully sorted array.
Design an algorithm to sort the array.
An array is said to be k-sorted (or nearly sorted) if the element that should be at index i in the sorted array is located somewhere in the range [i - k, i + k] in the unsorted array.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | [1, 2, 3, 4, 5], k = 0 | [5, 4, 3, 2, 1], k = 4 | [], k = 0 or [1], k = 0 |
Output | [1, 2, 3, 4, 5] | [1, 2, 3, 4, 5] | [] or [1] |
Explanation | Already sorted, no movement | Fully reversed but still k-sorted | No elements or only one |
4. Given an unsorted array of n integers and an integer k. Design an algorithm to find and return:
kth smallest = the value that would be at index k-1 if the array were sorted in ascending order.
kth largest = the value that would be at index n-k if the array were sorted in ascending order.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | arr = [1, 2, 3, 4, 5], k = 2 | arr = [9, 8, 7, 1, 2, 3], k = 6 | arr = [], k = 1 or k > n |
Output | 2 (smallest), 4 (largest) | 1 (smallest), 9 (largest) | null or error |
Explanation | Values already close to expected | Elements are reversed or unordered | Invalid due to empty input or bad k |
1. Given an array of integers. Design the following sorting algorithms:
Each algorithm should sort the array in ascending order and return the sorted array.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | [1, 2, 3, 4, 5] (already sorted) | [5, 4, 3, 2, 1] (reverse order) | [], [42], [3, 3, 3] |
Output | [1, 2, 3, 4, 5] | [1, 2, 3, 4, 5] | [], [42], [3, 3, 3] |
Explanation | No major reordering needed; few comparisons | Requires full sorting with most recursive calls | Checks behavior for no input or duplicates |
2. Given multiple sorted arrays in ascending order.
Design an algorithm that merges all the arrays into a single sorted array.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | [[1, 3], [2, 4]] | [[1,4,5], [1,3,4], [2,6]] | [], [[]], [[], [], []], [[], [1,2]] |
Output | [1, 2, 3, 4] | [1, 1, 2, 3, 4, 4, 5, 6] | [], [1,2] |
Explanation | Simple merge, no overlap | Full overlap of values, multiple pushes/pops from the heap | Must handle empty input or sparse arrays gracefully |
3. Given a head of a singly linked list containing integers.
Design an algorithm that sort the linked list in ascending order and returns the head of the sorted list.
Linked List Node structure -
class ListNode {
constructor(value = 0, next = null) {
this.value = value;
this.next = next;
}
}
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | 1 → 2 → 3 | 5 → 4 → 3 → 2 → 1 | null, 1, 7 → 7 → 7 → 7 |
Output | 1 → 2 → 3 | 1 → 2 → 3 → 4 → 5 | null, 1, 7 → 7 → 7 → 7 |
Explanation | Already sorted; minimal changes | Fully reversed; full recursion/merge needed | Checks empty list, single-node, and all duplicates |
4. Given an array of integers that may contain duplicate values. Design an algorithm that stable sorts the array in ascending order.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | [1, 2, 3] | [9, 8, 7, 6, 5, 4, 3, 2, 1] | [], [5], [2, 2, 2, 2] |
Output | [1, 2, 3] | [1, 2, 3, 4, 5, 6, 7, 8, 9] | [], [5], [2, 2, 2, 2] |
Explanation | Already sorted; no swaps | Reversed array requires maximum effort | Handles empty input and identical values correctly |
5. Given an array of integers. Desing an algorithm that count the total number of inversions in the array.
An inversion is a measure of how far a list is from being sorted. If nums[i] > nums[j] and i < j, that is an inversion.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | [1, 2, 3, 4, 5] | [5, 4, 3, 2, 1] | [], [7], [2, 2, 2] |
Output | 0 | 10 | 0 |
Explanation | Already sorted | Completely reversed: all pairs are inverted | No inversions possible when ≤1 element or duplicates |
6. Given a string s consisting of ASCII characters (letters, digits, symbols). Design an algorithm that rearranges the characters of s so that they appearin descending order of frequency.
If multiple characters have the same frequency, they can appear in any order relative to each other.
The output string must include all characters the number of times they appear in the original string.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | "aaa" | "abcdefg" | "", "a", "ababab" |
Output | "aaa" | "abcdefg" (any permutation) | "", "a", "aaabbb" |
Explanation | All chars same → no reordering needed | All unique → frequency is 1 for all → any order is valid | Empty or single-character string produces same output |