Sorting algorithm (Quick Sort)

Source: Internet
Author: User

There are several optimization methods for the fast sorting algorithm: The selection of datum points, minus the unnecessary switching positions, optimization of fractional groups, optimization of recursion (in the case of short array length, the direct insertion algorithm is OK)

Time Complexity (NLOG2N)

public class QuickSort {

public static void Main (string[] args) {
int a[]={1,3,5,2,7,7,4,9};
Sort (A, 0, 7);
for (int i = 0; i < a.length; i++) {
System.out.print (A[i]);
}
}

public static void Sort (Int. a[],int Low,int High) {

When int point=a[low];//begins, the Datum point is at the beginning of the array
if (LowPoint=patertion (A,low,high);
Sort (a,low,point-1);//recursion Left
Sort (a,point+1,high);//Recursive Right

}

}

private static int patertion (int[] A, int low, int. high) {
int Point=a[low];
while (Lowwhile (Lowhigh--;
}
A[low]=a[high];
Swap (A,low,high);//Our exchange can become an assignment


while (Lowlow++;
}
A[high]=a[low];
Swap (A,low,high),///Our exchange can become an assignment, the last low position of the point, after the end of the assignment back to A[low]=point;

}
A[low]=point;
return low;
}

private static void Swap (int[] A, int low, int. high) {
int temp;
Temp=a[high];
A[high]=a[low];
A[low]=temp;
//    }
//


}

Sorting algorithm (Quick sort)

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.