The sorting algorithm of data structure Java implementation (6)--Binary insertion sorting algorithm for inserting class sort

Source: Internet
Author: User

Binary Insert sort is an improvement on the direct insertion sort, which improves the query efficiency by using the binary search algorithm when looking for the insertion point.

Sort Ascending:

/** * Binary Insert sort * ascending sort * * * @Overridepublic <t extends comparable<? Super t>> void Sortbyasc (t[] data) {for (int i = 1;i < data.length;i++) {if (Data[i].compareto (data[i-1]) < 0) {/** record I value */t temp = data[i];/** records the left edge of the search range */int low = 0;/** records the right border of the search range */int high = i-1;while (Low <= high) {/** record middle position */in T mid = (high + low)/2;/** compares intermediate position data and I data size to narrow the search */if (Data[mid].compareto (temp) < 0) {low = mid + 1;} Else{high = Mid-1;}} /** move the data low~i the whole backward movement */for (int j = i; j > low; j--) {data[j] = data[j-1];} Data[low] = temp;}}}

Sort Descending:

/** * Binary Insert Sort * descending sort * * @Overridepublic <t extends comparable<? Super t>> void Sortbydesc (t[] data) {for (int i = 1;i < data.length;i++) {if (Data[i].compareto (data[i-1]) > 0 {/** records the value of i */t temp = data[i];/** records the left edge of the search range */int low = 0;/** records the right border of the search range */int high = i-1;while (Low <= high) {/** record middle position */i NT MID = (high + low)/2;/** compares intermediate position data and I data size to narrow the search */if (Data[mid].compareto (temp) > 0) {low = mid + 1;} Else{high = Mid-1;}} /** move the data low~i the whole backward movement */for (int j = i; j > low; j--) {data[j] = data[j-1];} Data[low] = temp;}}}

  

The sorting algorithm of data structure Java implementation (6)--Binary insertion sorting algorithm for inserting class 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.