Sort an array
MediumUpdated: Sep 29, 2025
Practice on:
Problem
Given an array of integers nums, sort the array in ascending order.
You must solve the problem without using any built-in functions in O(nlog(n)) time complexity and with the smallest space complexity possible.
Examples
Example 1
Input: Array of numbers , unsorted. Eg.

Output : Same numbers sorted in some order, say increasing order. Eg.
Input:
nums = [7, 4, 6, 1, 3, 5, 8, 2]
Output:
[1, 2, 3, 4, 5, 6, 7, 8]
Solution
There are multiple solutions to this. Here are some:
- Bubble Or Sinking Sort Algorithm
- Selection Sort
- [Insertion Sort](insertion-sort)
- [Merge Sort Algorithm](merge-sort-algorithm)
- Quicksort Algorithm
- Shell Sort
- Heap Sort
- Cocktail Sort
- Tree Sort
Integer Specific
Sorting an Object
Java
Java Sorting Collections Index