Java, Scala, go in the same way to achieve fast sort code comparison __java

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.