Quick Sort is one of the most popular and efficient sorting algorithms used in computer science. It follows a smart idea called divide and conquer, where a large problem is broken into smaller parts, solved separately, and then combined. The main concept behind Quick Sort is choosing a value called a pivot, placing it in its correct position, and arranging smaller values on one side and larger values on the other side. This process repeats until the entire list is sorted.
with hands-on learning.
get the skills and confidence to land your next move.
Quick Sort matters because it is very fast in practice and is widely used in real applications such as databases, search engines, and system-level software. Even though the logic may feel slightly more complex than basic sorting algorithms, learning Quick Sort helps beginners understand recursion, partitioning, and efficient problem-solving. Writing a Quick Sort program in TypeScript also strengthens your understanding of arrays, functions, and clean coding practices used in modern development.
Program 1: Basic Recursive Quick Sort Using Last Element as Pivot
This program demonstrates the classic Quick Sort implementation using recursion. It selects the last element as the pivot and arranges values around it. This is the most commonly taught version of Quick Sort.
function quickSort(arr: number[]): number[] {
if (arr.length <= 1) {
return arr;
}
const pivot = arr[arr.length - 1];
const left: number[] = [];
const right: number[] = [];
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return [...quickSort(left), pivot, ...quickSort(right)];
}
let numbers: number[] = [10, 7, 8, 9, 1, 5];
console.log("Sorted array:", quickSort(numbers));This program works by placing values smaller than the pivot on the left and larger values on the right. Each side is then sorted recursively. Beginners can think of this as repeatedly placing one number in the correct position until everything falls into place.
Program 2: Quick Sort Using First Element as Pivot
This version uses the first element of the array as the pivot instead of the last. It helps beginners understand that pivot selection can vary while the core idea remains the same.
function quickSortFirstPivot(arr: number[]): number[] {
if (arr.length <= 1) {
return arr;
}
const pivot = arr[0];
const left: number[] = [];
const right: number[] = [];
for (let i = 1; i < arr.length; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return [...quickSortFirstPivot(left), pivot, ...quickSortFirstPivot(right)];
}
let data: number[] = [4, 2, 6, 9, 2];
console.log("Sorted array:", quickSortFirstPivot(data));This program shows that the pivot does not always need to be the last element. Beginners can experiment with different pivot choices and observe how the sorting process still works correctly.
Program 3: In-Place Quick Sort Using Partition Logic
This program performs Quick Sort without creating new arrays. It rearranges elements directly inside the original array, which is closer to how Quick Sort is used in performance-critical systems.
function partition(arr: number[], low: number, high: number): number {
const pivot = arr[high];
let i = low - 1;
for (let j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
[arr[i + 1], arr[high]] = [arr[high], arr[i + 1]];
return i + 1;
}
function quickSortInPlace(arr: number[], low: number, high: number): void {
if (low < high) {
const pi = partition(arr, low, high);
quickSortInPlace(arr, low, pi - 1);
quickSortInPlace(arr, pi + 1, high);
}
}
let values: number[] = [8, 7, 6, 1, 0, 9, 2];
quickSortInPlace(values, 0, values.length - 1);
console.log("Sorted array:", values);This version teaches beginners how swapping and indexes work together. While it looks more complex, it is useful for understanding how Quick Sort works internally and why it is memory-efficient.
Program 4: Quick Sort in Descending Order
This program sorts the array in descending order instead of ascending order. It helps beginners see how simple comparison changes can affect the final result.
function quickSortDescending(arr: number[]): number[] {
if (arr.length <= 1) {
return arr;
}
const pivot = arr[arr.length - 1];
const left: number[] = [];
const right: number[] = [];
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] > pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return [...quickSortDescending(left), pivot, ...quickSortDescending(right)];
}
let items: number[] = [3, 6, 8, 10, 1, 2];
console.log("Sorted array in descending order:", quickSortDescending(items));By reversing the comparison logic, this program places larger values first. Beginners can easily adapt Quick Sort for different sorting needs using this approach.
Program 5: Quick Sort Wrapper for Clean Usage
This final program wraps Quick Sort logic inside a clean function for easy reuse. It focuses on readability and practical usage in larger programs.
function quickSortWrapper(arr: number[]): number[] {
function sort(innerArr: number[]): number[] {
if (innerArr.length <= 1) {
return innerArr;
}
const pivot = innerArr[Math.floor(innerArr.length / 2)];
const left: number[] = [];
const right: number[] = [];
const equal: number[] = [];
for (let num of innerArr) {
if (num < pivot) {
left.push(num);
} else if (num > pivot) {
right.push(num);
} else {
equal.push(num);
}
}
return [...sort(left), ...equal, ...sort(right)];
}
return sort(arr);
}
let nums: number[] = [5, 3, 8, 4, 2, 7, 1, 10];
console.log("Sorted array:", quickSortWrapper(nums));This version handles duplicate values more clearly and keeps the code organized. Beginners can use this structure when writing clean and maintainable TypeScript code.
Frequently Asked Questions (FAQ)
This section answers common beginner questions about Quick Sort in TypeScript using clear and simple explanations.
Q1. Why is Quick Sort so fast compared to other algorithms?
Quick Sort reduces the problem size quickly by placing the pivot in the correct position early, which makes sorting faster for large datasets.
Q2. Is Quick Sort used in real applications?
Yes, Quick Sort is widely used in databases, system libraries, and performance-sensitive applications.
Q3. Can Quick Sort handle duplicate values?
Yes, Quick Sort can handle duplicate values, especially when written to group equal elements properly.
Q4. Is Quick Sort always faster than Merge Sort?
Not always. Merge Sort guarantees performance, while Quick Sort is usually faster in practice but can be slower in rare cases.
Q5. What should I learn after Quick Sort?
After Quick Sort, beginners often explore Heap Sort, advanced data structures, and algorithm optimization techniques.
Conclusion
Quick Sort is a powerful and efficient sorting algorithm that plays a major role in modern computing. In this article, we explored several TypeScript programs that implement Quick Sort using different pivot choices, recursion styles, in-place logic, and sorting orders. Each example showed how flexible and practical Quick Sort can be.
If you are learning TypeScript or algorithms, spend time running these programs and changing the input values. Practice will help you understand how divide-and-conquer works in real code. Keep exploring, keep coding, and enjoy building strong foundations in programming.




