Problem
Given an array of integers nums, sort the array in ascending order.
Java
Insertion sort
1 | class Solution { |
Submission Detail
- 10 / 10 test cases passed.
- Runtime: 1323 ms, faster than 5.01% of Java online submissions for Sort an Array.
- Memory Usage: 47.2 MB, less than 6.12% of Java online submissions for Sort an Array.
Mergesort
bottom-up mergesort
This method requires even less code than the standard recursive implementation.
© Robert Sedgewick and Kevin Wayne
1 | class Solution { |
Submission Detail
- 11 / 11 test cases passed.
- Runtime: 7 ms, faster than 34.85% of Java online submissions for Sort an Array.
- Memory Usage: 46.7 MB, less than 12.67% of Java online submissions for Sort an Array.
Top-down mergesort
recursive method
© Robert Sedgewick and Kevin Wayne
1 | class Solution { |
Submission Detail
- 11 / 11 test cases passed.
- Runtime: 6 ms, faster than 57.85% of Java online submissions for Sort an Array.
- Memory Usage: 46.3 MB, less than 14.62% of Java online submissions for Sort an Array.
Shellsort
© Robert Sedgewick and Kevin Wayne
Example of simple idea leading to substantial performance gains.
1 | class Solution { |
Submission Detail
- 11 / 11 test cases passed.
- Runtime: 1865 ms, faster than 5.04% of Java online submissions for Sort an Array.
- Memory Usage: 46.2 MB, less than 18.92% of Java online submissions for Sort an Array.
Quicksort
1
© John Bentley, Programming Pearls
1 | class Solution { |
Submission Detail
- 10 / 10 test cases passed.
- Runtime: 4 ms, faster than 91.33% of Java online submissions for Sort an Array.
- Memory Usage: 46.8 MB, less than 6.12% of Java online submissions for Sort an Array.
quicksort with 3-way partitioning
© Robert Sedgewick and Kevin Wayne
1 | class Solution { |
Submission Detail
- 11 / 11 test cases passed.
- Runtime: 4 ms, faster than 89.87% of Java online submissions for Sort an Array.
- Memory Usage: 46.9 MB, less than 12.99% of Java online submissions for Sort an Array.
selection sort
1 | class Solution { |
Submission Detail
- 11 / 11 test cases passed.
- Runtime: 1169 ms, faster than 5.10% of Java online submissions for Sort an Array.
- Memory Usage: 46.3 MB, less than 86.77% of Java online submissions for Sort an Array.
heap sort
iterative
© Robert Sedgewick and Kevin Wayne
Heapsort breaks into two phases: heap construction, where we reorganize the original array into a heap, and the sortdown, where we pull the items out of the heap in decreasing order to build the sorted result.
Q. Why not use a[0] in the heap representation?
A. Doing so simplifies the arithmetic a bit. It is not difficult to implement the heap methods based on a 0-based heap where the children of a[0] are a[1] and a[2], the children of a[1] are a[3] and a[4], the children of a[2] are a[5] and a[6], and so forth, but most programmers prefer the simpler arithmetic that we use. Also, using a[0] as a sentinel value (in the parent of a[1]) is useful in some heap applications.
1 | class Solution { |
Submission Detail
- 11 / 11 test cases passed.
- Runtime: 6 ms, faster than 53.50% of Java online submissions for Sort an Array.
- Memory Usage: 46.6 MB, less than 48.27% of Java online submissions for Sort an Array.
recursion
© HeapSort
1 | class Solution { |
Submission Detail
- 11 / 11 test cases passed.
- Runtime: 7 ms, faster than 30.29% of Java online submissions for Sort an Array.
- Memory Usage: 46.7 MB, less than 32.93% of Java online submissions for Sort an Array.
jdk PriorityQueue
1 | import java.util.PriorityQueue; |
Submission Detail
- 11 / 11 test cases passed.
- Runtime: 18 ms, faster than 16.69% of Java online submissions for Sort an Array.
- Memory Usage: 47.7 MB, less than 11.36% of Java online submissions for Sort an Array.