Programmer must know 8 big sort (① direct insertion sort ② Hill sort ③ Simple Select sort ④ heap sort ⑤ bubble sort ⑥ sort ⑦ Merge sort ⑧ cardinal order) __java

Source: Internet
Author: User

The relationship between 8 sorts:

1, direct insertion sort

(1) Basic idea: In the set of numbers to be sorted, suppose that the front (n-1) [n>=2] number is already a row

In a good order, now you have to insert the nth number into the preceding ordered number so that the number of n

It's a good order, too. Repeat the loop until all the order is sorted.

(2) Example

(3) Implement with Java[Plain]  View plain  copy    package com.njue;             publicclass insertsort {      public insertsort () {           int a[]={ 49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};          int temp=0;          for (int i=1;i< a.length;i++) {             int j=i-1;             temp=a[i];              for (; j>=0&&temp<a[j];j--) {              a[j+1]=a[j];                        //will be greater than the value of the temp whole to move one unit              }              a[j+1]=temp;          }           for (int i=0;i<a.length;i++)              system.out.println (A[i]);     }      }  


2, Hill Sort (minimum increment sort)

(1) Basic idea: The algorithm first will be sorted by a group of numbers by an increment D (n/2,n to the number of sorted numbers) into several groups, the subscript that is recorded in each group differs D. Inserts a direct sort of all elements in each group, and then groups it with a smaller increment (D/2). Make a direct insert sort in each group. When the increment is reduced to 1 o'clock, the sort completes after a direct insert sort.

(2) Example:

(3) implemented in Java [plain]   View plain  copy publicclass shellsort {      publicshellsort () {          int a[]={ 1,54,6,3,78,34,12,45,56,100};          double d1=a.length;           int temp=0;           while (true) {             d1= math.ceil (D1/2);              int d= (int)  d1;             for (int x=0;x<d;x++) {                  for (Int i=x+d;i<a.length;i+=d) {     

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.