Skip to content

Commit 2303010

Browse files
authoredMar 1, 2023
Create 0912-sort-an-array.kt
1 parent 6e3731f commit 2303010

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
 

Diff for: ‎kotlin/0912-sort-an-array.kt

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Merge sort
3+
*/
4+
class Solution {
5+
fun sortArray(nums: IntArray): IntArray {
6+
mergeSort(nums, 0, nums.lastIndex)
7+
return nums
8+
}
9+
10+
private fun mergeSort(nums: IntArray, left: Int, right: Int) {
11+
if(left == right) return
12+
13+
val mid = (left + right) / 2
14+
mergeSort(nums, left, mid)
15+
mergeSort(nums, mid + 1, right)
16+
merge(nums, left, mid, right)
17+
18+
return
19+
}
20+
21+
private fun merge(nums: IntArray, left: Int, mid: Int, right: Int) {
22+
val leftPart = nums.copyOfRange(left, mid + 1)
23+
val rightPart = nums.copyOfRange(mid + 1, right + 1)
24+
var i = left
25+
var j = 0
26+
var k = 0
27+
28+
while(j < leftPart.size && k < rightPart.size) {
29+
if(leftPart[j] <= rightPart[k]) {
30+
nums[i] = leftPart[j]
31+
j++
32+
}else{
33+
nums[i] = rightPart[k]
34+
k++
35+
}
36+
i++
37+
}
38+
39+
while(j < leftPart.size) {
40+
nums[i] = leftPart[j]
41+
j++
42+
i++
43+
}
44+
45+
while(k < rightPart.size) {
46+
nums[i] = rightPart[k]
47+
k++
48+
i++
49+
}
50+
}
51+
}
52+
53+
/*
54+
* Quick sort
55+
* This will fail testcase 17/19 (used to pass earlier, before adding new testcases), I still added it here for interest.
56+
* It fails on test case where we have an array with many elements of which all are 2's. This will have quicksort to run as
57+
* its worst case, which is O(n^2). But on average this will run O(nlogn)
58+
*/
59+
class Solution {
60+
fun sortArray(nums: IntArray): IntArray {
61+
62+
quickSort(nums, 0, nums.lastIndex)
63+
return nums
64+
}
65+
66+
private fun quickSort(nums: IntArray, low: Int, high: Int) {
67+
if (low < high) {
68+
val pivotIndex = partition(nums, low, high)
69+
quickSort(nums, low, pivotIndex - 1)
70+
quickSort(nums, pivotIndex + 1, high)
71+
}
72+
}
73+
74+
private fun partition(nums: IntArray, low: Int, high: Int): Int {
75+
val pivot = nums[high]
76+
var i = low
77+
78+
for(j in low until high) {
79+
if (nums[j] <= pivot) {
80+
nums.swap(i, j)
81+
i++
82+
}
83+
}
84+
85+
nums.swap(i, high)
86+
return i
87+
}
88+
89+
fun IntArray.swap(i: Int, j: Int) {
90+
this[i] = this[j].also{ this[j] = this[i] }
91+
}
92+
}
93+
94+
/*
95+
* Heap sort
96+
*/
97+
class Solution {
98+
fun sortArray(nums: IntArray): IntArray {
99+
100+
heapSort(nums)
101+
return nums
102+
}
103+
104+
private fun heapSort(nums: IntArray) {
105+
val n = nums.size
106+
107+
for(i in (n/2 - 1) downTo 0)
108+
heapify(nums, n, i)
109+
110+
for(i in n-1 downTo 0) {
111+
nums.swap(0, i)
112+
heapify(nums, i, 0)
113+
}
114+
}
115+
116+
private fun heapify(nums: IntArray, n: Int, i: Int) {
117+
var largest = i
118+
119+
val left = 2 * i + 1
120+
val right = 2 * i + 2
121+
122+
if(left < n && nums[left] > nums[largest])
123+
largest = left
124+
if(right < n && nums[right] > nums[largest])
125+
largest = right
126+
127+
if(largest != i) {
128+
nums.swap(i, largest)
129+
heapify(nums, n, largest)
130+
}
131+
}
132+
133+
fun IntArray.swap(i: Int, j: Int) {
134+
this[i] = this[j].also{ this[j] = this[i] }
135+
}
136+
}

0 commit comments

Comments
 (0)