//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