Several internal sorting algorithms

Source: Internet
Author: User

//sort by direct insertion. Inserts the sorted array from the original array (from the back to the insertion position)     Public Static int[] Insertsort (int[] nums) {        if(Nums = =NULL|| Nums.length = = 0 | | Nums.length = = 1)            returnNums; inttemp = 0;  for(intI=1; i<nums.length; i++) {            if(Nums[i] < nums[i-1]) {temp=Nums[i]; intj = I-1;  Do{nums[j+1] =Nums[j]; J--; }  while(J >= 0 && Nums[j] >temp); Nums[j+1] =temp; }        }        returnNums; }        //binary Insert Sort, find binary search method every time you look for the insertion position     Public Static int[] Binaryinsertsort (int[] nums) {        if(Nums = =NULL|| Nums.length = = 0 | | Nums.length = = 1)            returnNums; inttemp = 0;  for(intI=1; i<nums.length; i++) {            if(Nums[i] < nums[i-1]) {temp=Nums[i]; intLow = 0, high = i-1;  while(Low <High ) {                    intMid = (low + high)/2; if(Temp <Nums[mid]) high= Mid-1; Else Low= Mid + 1; }                 for(intJ=i-1; j>=low; j--) Nums[j+1] =Nums[j]; Nums[low]=temp; }        }        returnNums; }        //Hill Sort. Each step gradually decreases until it is 1. unstable algorithm     Public Static int[] Shellsort (int[] nums) {        if(Nums = =NULL|| Nums.length = = 0 | | Nums.length = = 1)             returnNums; intI, j, gap =nums.length; inttemp = 0;  Do{Gap= GAP/3 + 1;  for(I=gap; i<nums.length; i++) {                if(Nums[i] < nums[i-Gap]) {J= i-Gap; Temp=Nums[i];  Do{nums[j+ Gap] =Nums[j]; J= J-Gap; }  while(J >= 0 && Temp <Nums[j]); Nums[j+GAP] =temp; }            }        }  while(Gap > 1); returnNums; }            //Quick Sort. Using the divide-and-conquer method, each time a pivot is selected, the elements on the left of the array are smaller than pivot, and the elements to the right of the array are larger than pivot.      Public Static voidQuickSort (int[] Nums,intLeftintRight ) {        if(Left <Right ) {            intPivotpos =partition (Nums, left, right); QuickSort (Nums, left, Pivotpos-1); QuickSort (Nums, Pivotpos+1, right); }    }         Public Static intPartitionint[] Nums,intLeftintRight ) {        intPivotpos = left, pivot =Nums[left];  for(inti=left+1; i<=right; i++) {            if(Nums[i] <pivot) {Pivotpos++; if(Pivotpos! =i) {inttemp =Nums[i]; Nums[i]=Nums[pivotpos]; Nums[pivotpos]=temp; }}} Nums[left]=Nums[pivotpos]; Nums[pivotpos]=pivot; returnPivotpos; }

Several internal sorting algorithms

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.