1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| package org.example;
import java.util.Arrays;
public class Main { public static int[] getPrivot(int[] nums){ privot(nums,0,nums.length-1); return nums; } public static void privot(int[] nums,int left,int right){ if(left > right){ return; } int l = left; int r = right; int privot = nums[left]; int temp = 0; while(l < r){ while(privot <= nums[r] && l < r){ r--; } while(privot >= nums[l] && l < r){ l++; } if(l <= r){ temp = nums[r]; nums[r] = nums[l]; nums[l] = temp; } } nums[left] =nums[l]; nums[l] = privot; privot(nums,left,l-1); privot(nums,l+1,right); }
public static void sort(int[] arrays, int left, int right) { if(left > right) { return; } int l = left; int r = right; int pivot = arrays[left]; int temp = 0; while(l < r) { while(pivot <= arrays[r] && l < r) { r--; } while(pivot >= arrays[l] && l < r) { l++; } if(l <= r) { temp = arrays[r]; arrays[r] = arrays[l]; arrays[l] = temp; } } arrays[left] = arrays[l]; arrays[l] = pivot; sort(arrays, left, l - 1); sort(arrays, l + 1, right); }
public static void main(String[] args) { int[] array = new int[]{19,20,8,7,98,37,40}; sort(array,0,array.length-1); Arrays.toString(array); } }
|