Java sort, what sort of a high-efficiency method

Source: Internet
Author: User

Is the same as all other languages. The fastest sort should be the most efficient.

public static void Bubblesort (int a[]) {
int len = a.length;
for (int i = 0; i < len-1; i++) {
for (int j = 0; J < len-1-I; j + +) {
if (A[j] > a[j + 1]) {
int temp = A[j];
A[J] = a[j + 1];
A[j + 1] = temp;
}
}
}
}
public static void Selectsort (int a[]) {
int temp = 0;
int len = a.length;
for (int i = 0; i < len-1; i++) {
int min = a[i];
int index = i;
for (int j = i + 1; j < Len; J + +) {
if (min > A[j]) {
min = A[j];
index = j;
}
}
temp = A[i];
A[i] = A[index];
A[index] = temp;
}
}
public static void Insertsort (int a[]) {
int len = a.length;
for (int i = 1; i < Len; i++) {
int temp = a[i];//value to be inserted
int index = i;//the position to be inserted
while (Index > 0 && a[index-1] > Temp) {
A[index] = a[index-1];//the position to be inserted to re-assign a larger value
index--;//Position move forward
}
A[index] = temp;
}
}
public static int partition (int a[], int. low, int height) {
int key = A[low];
while (Low < height) {
while (Low < height && A[height] >= key)
height--;
A[low] = A[height];
while (Low < height && A[low] <= key)
low++;
A[height] = A[low];
}
A[low] = key;
return low;
}
public static void QuickSort (int. a[], int low, int height) {
if (Low < height) {
int result = partition (A, low, height);
QuickSort (A, low, result-1);
QuickSort (A, result + 1, height);
}
}
Test results
------------------------------------------
Test Data 10000
Bubble Sort: 120ms
Select Sort: 32ms
Insertion Sort: 20ms
Quick sort: 7ms
------------------------------------------
Test data 100000
Bubble Sort: 13098ms
Select Sort: 2334ms
Insertion Sort: 1264ms
Quick sort: 23ms
The efficiency gap is very big AH!!!

Java sort, what sort of a high-efficiency method

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.