![]()
package paixu;
import java.util.Arrays;
public class MaoPao {
//冒泡排序:相邻的比较,从小到大去排序
public static void main(String[] args) {
int[] arr = new int[] {2,5,9,11,3443,1,2,3,4};
System.out.println(Arrays.toString(arr));
bubbleSort(arr);
System.out.println(Arrays.toString(arr));
}
//从小到大去排序
public static void bubbleSort(int[] arr) {
//控制比较多少轮数
for(int i=0;i<arr.length-1;i++) {
//控制比较的次数
for(int j=0;j<arr.length-1-i;j++) {
if(arr[j]>arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
1 package paixu;
2 import java.util.Arrays;
3 public class QuickSort {
4 public static void main(String[] args) {
5 int[] arr = new int[] {3,4,6,7,1,5,9,8,2};
6 System.out.println(Arrays.toString(arr));
7 quickSort(arr,0,arr.length-1);
8 System.out.println(Arrays.toString(arr));
9 }
10 public static void quickSort(int[] arr,int start, int end) {
11 if(start<end) {
12 //通常把数组中第0个数字作为标准数
13 int stard = arr[start];
14 //记录需要排序的下标
15 int low = start;
16 int high = end;
17 //循环找比标准数大的数和比标准数小的数
18 while(low<high) {
19 //右边数字比标准数大
20 while(low<high&&stard<=arr[high]) {
21 high--;
22 }
23 //使用右边的数字替换左边的数
24 arr[low] = arr[high];
25 //使用左边的数字比标准数小
26 while(low<high&&arr[low]<=stard) {
27 low++;
28 }
29 arr[high] = arr[low];
30 }
31 //把标准数赋给低所在的位置元素
32 arr[low] = stard;
33 //处理所有的小的数字
34 quickSort(arr,start,low);
35 //处理所有大的数字
36 quickSort(arr,low+1,end);
37 }
38
39 }
40 }