In the program Ape World, the language dispute is an eternal topic, here, I want to say: Let the argument come more violent.
Here, I want to achieve a quick sort in the same way. First, let's briefly review what a quick sort is.
Quick sort:
The essence of the fast platoon is "divide and conquer". Note: This is two aspects, and "min" is about taking advantage of the selected element (often the first element, named key at this point, the list is divided into two parts: larger than the key element and smaller than the key element; "Governance" is about dividing the two parts in the same way ("sub") and continuing to sort, Until completely finished. As shown in the following illustration:
Obviously, the "recursive" way is very good. Below, each language implements this sort in the simplest way, and then compares the amount of code:
The sequence to be sorted is: 6, 2, 7, 3, 9, 4, 8, 5
Java:
private void Setquicksort (int[] Array,int left,int right) {
int i = left;
int j= right;
int key = Array[left];
int keyflag = left;
while (I < j) {
while (I < J&&key <= Array[j]) {
j--;
}
if (I < j) {
int tmp = ARRAY[J];
ARRAY[J] = Array[i];
Array[i] = tmp;
Keyflag = j;
}
while (I < J&&key >= Array[i]) {
i++;
}
if (I < j) {
int tmp1 = Array[j];
ARRAY[J] = Array[i];
Array[i] = TMP1;
Keyflag = i;
}
}
if (keyflag>left+1) {
Setquicksort (array, left, keyFlag-1);
}
if (keyflag<right-1) {
Setquicksort (Array,keyflag+1,right);
}
}
}
Go:
Func setquicksort (Array *[9]int, left, right int) {
I, J: = left, right
M: = Left
Key: = Array[left]
For I < J {
For i < J && Key <= Array[j] {
j--
}
If I < J {
Array[i], array[j] = Array[j], array[i]
m = j
}
For i < J && Key >= Array[i] {
i++
}
If I < J {
Array[i], array[j] = Array[j], array[i]
m = i
}
}
If M > left+1 {
Setquicksort (array, left, m-1)
}
If M < right-1 {
Setquicksort (array, m+1, right)
}
}
Scala:
def quickSort (Array:list[int]): list[int] = {
if (array.isempty) array
Else
QuickSort (Array.filter (_<array.head))::: Array.head::quicksort (Array.filter (_>array.head))
}
Scala to the victory. Haha, please don't spray me.