Basic sort Java Implementation

Source: Internet
Author: User

The basic sort that needs to be mastered

One, insert sort 1) Direct insert sort 2) Hill sort

Two, Exchange sort 1) bubble sort 2) Quick sort

Iii. Select sort 1) simple select sort 2) heap sort

Iv. Merging and sorting

First, insert sort

The simple understanding is to traverse the entire array, and in the process each number is compared to the previous one as long as it is smaller than it is moved forward.

1) Direct Insert Sort implementation

       Public Static voidDirectinsert (int[] data) {          intTemp=0; intlen=data.length;  for(inti=1;i<len;i++) {Temp=Data[i]; intJ=i-1;  while(J>-1 && temp<Data[j]) {Data[j+1]=Data[j]; J--; } data[j+1]=temp; }      }

2) Hill Sort

Understanding: http://blog.csdn.net/morewindows/article/details/6668714

The direct insertion of the array is sorted by the interval of N/2,N/4,N/8,,,, 1. When the array is basically ordered, the number of insertion sort comparisons is minimal, the interval is 1, the time is basically orderly, the last execution of the insertion sort works best.

       Public Static voidShellinsert (int[] data) {          intTemp=0; intlen=data.length;  for(intgap=len/2;gap>0;gap/=2)//the outermost narrowing increment          {               for(intI=0;I&LT;LEN;I+=GAP)//interval sort such as 13579 246810 such that the equal interval grouping is inserted directly into the sort{Temp=Data[i]; intj=i-Gap;  while(J>-1 && temp<Data[j]) {Data[j+gap]=Data[j]; J-=Gap; } data[j+gap]=temp; }          }      }

Second, exchange sort

1) Bubble sort

The number of adjacent two numbers is compared to the size until the interchange is complete.

The first trip to the largest number to the right, the second to the second largest change to the second-to-last, each time the number of the first n-i to sort.

       Public Static voidBubbleint[] data) {          intlen=data.length; intcnt=0;//The number of exchanges. If it's 0, the sort is done jumping out          intTemp=0;  for(inti=1;i<len;i++)//to sort n-1          {               for(intj=0;j<len-i;j++)              {                  if(data[j]>data[j+1]) {temp=Data[j]; DATA[J]=data[j+1]; Data[j+1]=temp; CNT++; }              }              if(cnt==0)                   Break; CNT=0; }      }

2) Quick Sort

  

Basic sort Java Implementation

Related Article

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.