-
-
Notifications
You must be signed in to change notification settings - Fork 378
/
QuickSort.scala
52 lines (41 loc) · 1.03 KB
/
QuickSort.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package Sort
object QuickSort {
/** @param array
* - a sequence of unsorted integers
* @return
* - sequence of sorted integers @array
*/
def quickSort(array: Array[Int]): Array[Int] = {
def quickSortImpl(array: Array[Int], first: Int, last: Int): Array[Int] = {
var pivot: Int = 0
var i: Int = 0
var j: Int = 0
var temp: Int = 0
if (first < last) {
pivot = first
i = first
j = last
while (i < j) {
while (array(i) <= array(pivot) && i < last) {
i += 1
}
while (array(j) > array(pivot)) {
j -= 1
}
if (i < j) {
temp = array(i)
array(i) = array(j)
array(j) = temp
}
}
temp = array(pivot)
array(pivot) = array(j)
array(j) = temp
quickSortImpl(array, first, j - 1)
quickSortImpl(array, j + 1, last)
}
array
}
quickSortImpl(array, 0, array.length - 1)
}
}