Java基础回顾:冒泡排序

COPY
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
package com.lete.Java.Test;

import java.util.Arrays;

/**
* @author Lete乐特
* @createDate 2021- 01-24 10:08
*/
public class BubbleSort {
public static void main(String[] args) {
// 定义数组
int arr[] = {10,2,15,33,1,6,8};
// 定义临时变量
int temp;

// 排序
for (int i = 1; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
// 两者比较后替换
if(arr[i]<arr[j]){
temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
// 打印排序后的结果
System.out.println(Arrays.toString(arr));
}
}

BubbleSort1

Authorship: Lete乐特
Article Link: https://blog.imlete.cn/article/Java-BubbleSort.html
Copyright: All posts on this blog are licensed under the CC BY-NC-SA 4.0 license unless otherwise stated. Please cite Lete乐特 's Blog !