package PracticeSort;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class QuickSort {
public static void main(String[] args) {
QuickSort quickSort = new QuickSort();
Random random = new Random();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要排序的个数:");
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = random.nextInt(100);
}
quickSort.sort(arr,0,arr.length-1);
System.out.println("排序后的结果为:"+ Arrays.toString(arr));
}
public void sort(int[] nums,int left,int right){
if (left > right) {
return ;
}
int i = left;
int j = right;
int temp = nums[left];
while (i < j){
while (temp <= nums[j] && j > i) j--;
while (temp >= nums[i] && j > i) i++;
if (i < j) {
int temp1 = nums[i];
nums[i] = nums[j];
nums[j] = temp1;
}
}
nums[left] = nums[j];
nums[j] = temp;
sort(nums,left,j-1);
sort(nums,j+1,right);
}
}
cv别人的,刚开始不理解:如果i,j相遇之后,那个数字比temp大它们还怎么交换,后来我懂了