Sort-quick Sort

Source: Internet
Author: User

The quick sort was developed by C.a.r.hoare (1962), which selects an element called the main element (pivot) in the array, divides the array into two parts, and all the elements in the first part are less than or equal to the primary, and all the elements of the second part are larger than the primary. Apply fast sorting algorithm recursively to the first part, then apply fast sorting algorithm recursively to the second part.

Package ss.sort;/** * Quick Sort * @author zhangss 2016-5-3 11:42:32 * */public class QuickSort {public static void QuickSort (in T[] list) {quickSort (list, 0, list.length-1);}  private static void QuickSort (int[] list, int first, int last) {if (last > first) {int pivotindex = partition (list, first, QuickSort (list, first, pivotIndex-1), QuickSort (list, Pivotindex + 1, last);}}  private static int partition (int[] list, int first, int last) {int pivot = List[first];      Choose the first element as the pivotint low = first + 1;          Index for forward searchint = last; Index for backward Searchwhile (High > Low) {//Search forward from leftwhile (Low <= high && List[low] < = pivot) low++;//Search forward from rightwhile (Low <= high && List[high] > Pivot) high--;//Swap, Element s in the ListIf (High > low) {int temp = List[high];list[high] = List[low];list[low] = temp;}} while (High > First && list[high] >= pivot) high--;//Swap PivotWith list[high]if (Pivot > List[high]) {List[first] = List[high];list[high] = Pivot;return high;} Else{return first;}} /** * A test method */public static void Main (string[] args) {int[] list = {2, 3, 2, 5, 6, 1,-2, 3, +, 12};quicksort (LIS t); for (int i = 0; i < list.length; i++) System.out.print (List[i] + "");}}

  

Sort-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.