A sort algorithm of data structure Java implementation (4)--Fast sorting algorithm of Exchange class ordering

Source: Internet
Author: User
Tags comparable

The fast sorting algorithm belongs to the sort of "exchange class", and its efficiency is mainly related to the symmetry of data distribution.

Sort Ascending:

/** * Quick Sort * ascending sort * * @Overridepublic <t extends comparable<? Super T>> Vo ID SORTBYASC (t[] data) {if (data = = NULL | | data.length <= 1) {return;} PARTITIONSORTBYASC (data, 0, data.length-1);} Private <t extends comparable<? Super t>> void Partitionsortbyasc (t[] data, int low, int.) {if (low >= high) {return;} /** uses two pointers start and end*/int start = Low;int end = high; T key = data[low];/** locates the key, then divides data into two parts, recursively */while (Start < end) {while (Start < end && Data[start], respectively. CompareTo (Key) < 0) {start + +;} if (Data[start].compareto (key) > 0) {T temp = Data[start];d Ata[start] = Data[end];d ata[end] = temp;} while (Start < end && Data[end].compareto (key) > 0) {end--;} if (Data[end].compareto (key) <= 0) {T temp = Data[end];d ata[end] = Data[start];d Ata[start] = temp;}} /** */if (Start > Low) PARTITIONSORTBYASC (data,low,start-1) on the left,/** (end < High) */if (data, End + 1, high);} 

Sort Descending:

/** * Quick Sort * descending sort * * * @Overridepublic <t extends comparable<? Super t>> void Sortbydesc (t[] data) {PARTITIONSORTBYDESC (data, 0, data.length-1);} Private <t extends comparable<? Super t>> void Partitionsortbydesc (t[] data, int low, int.) {if (low >= high) {return;} /** uses two pointers start and end*/int start = Low;int end = high; T key = data[low];/** locates the key, then divides data into two parts, recursively */while (Start < end) {while (Start < end && Data[start], respectively. CompareTo (key) > 0) {start + +;} if (Data[start].compareto (key) <= 0) {T temp = Data[start];d Ata[start] = Data[end];d ata[end] = temp;} while (Start < end && Data[end].compareto (key) < 0) {end--;} if (Data[end].compareto (key) > 0) {T temp = Data[end];d ata[end] = Data[start];d Ata[start] = temp;}} /** */if (Start > Low) Partitionsortbydesc (data,low,start-1) on the left,/** (end < High) */if (data, End + 1, high);}

  

A sort algorithm of data structure Java implementation (4)--Fast sorting algorithm of Exchange class ordering

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.