---Selection of data structure sorting---Hill---merge

Source: Internet
Author: User

Choose a sort of basic idea: set a benchmark, and then use a circular comparison to find the smallest

Complexity of Time: O (n2)

/** *  */Package com;/** * @author Wenb * @time pm 01:41:21 * @date 2014-10-24*/ Public classSelectsort { Public Static voidMain (string[] args) {int[] A = {1,3,2, +,5, A,98, Wu};        Select (a);  for(intI=0; i<a.length;i++) {System. out. Print (a[i]+" "); }    }    /** * @param a*/    Private Static voidSelect (int[] a) {//TODO auto-generated Method Stub        inttemp;  for(intI=0; i<a.length;i++) {//control the total number of trips                        intK =i; //Find the smallest             for(intJ =a.length-1; j>i;j--){                if(a[j]<A[k]) {k=J; }} Temp=A[i]; A[i]=A[k]; A[K]=temp; }    }}

The basic idea of hill sort: to divide the whole sequence into small sub-sequences to insert the sort separately

Complexity of Time: O (n2)

/** *  */Package com;/** * @author Wenb * @time pm 02:44:18 * @date 2014-10-24*/     Public classShellsort { Public Static voidMain (string[] args) {int[] A = {1,3,2, +,5, A,98, Wu};            Shellsort (a);  for(inti =0; i < a.length; i++) {System. out. print (A[i] +" "); }                  }                 Public Static voidShellsort (int[] data) {                          //calculates the maximum h value            inth =1;  while(H <= data.length/3) {h= h *3+1; }                           while(H >0) {                   for(inti = h; i < data.length; i + =h) {if(Data[i] < data[i-h]) {intTMP =Data[i]; intj = i-h;  while(J >=0&& Data[j] >tmp) {Data[j+ h] =Data[j]; J-=h; } data[j+ h] =tmp; }} H= (H-1) /3;//calculate the next H value            }          }              }  

Merge sort basic idea: merging two (or more than two) ordered tables into a new ordered table divides the ordered sequence into several sub-sequences, each of which is ordered. And then merge the ordered subsequence into the whole ordered sequence .

Complexity of Time: O (NLOGN)

/** *  */Package Com;import java.util.Arrays;/** * @author Wenb * @time pm 03:41:12 * @date 2014-10-24*/ Public classMergeSort { Public Static voidMain (string[] args) {int[] A = {1,3,2, +,5, A,98, Wu}; Mergesort.sort (A,0, a.length-1);  for(inti =0; i < a.length; i++) {System. out. print (A[i] +" "); }      }         Public Static int[] Sort (int[] Nums,intLowintHigh ) {          intMid = (low + high)/2; if(Low <High ) {              //leftsort (nums, low, mid); //RightSort (Nums, Mid +1, high); //merge right and leftmerge (Nums, Low, Mid, high); }          returnNums; }         Public Static voidMergeint[] Nums,intLowintMidintHigh ) {          int[] temp =New int[High-Low +1]; inti = low;//left Pointer        intj = Mid +1;//Right Pointer        intK =0; //move the smaller number first to the new array         while(I <= mid && J <=High ) {              if(Nums[i] <Nums[j]) {Temp[k+ +] = nums[i++]; } Else{temp[k+ +] = nums[j++]; }          }            //Move the left remaining number into the array         while(I <=mid) {temp[k+ +] = nums[i++]; }            //move the remaining number in the right side into the array         while(J <=High ) {Temp[k+ +] = nums[j++]; }            //overwrites the number in the new array with the Nums array         for(intK2 =0; K2 < Temp.length; k2++) {Nums[k2+ Low] =TEMP[K2]; }      }      }

Data structure sorting problem---Select---Hill---merge

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.