您的位置:首页 > 编程语言

经典代码之冒泡排序,选择排序

2016-08-16 12:22 92 查看
public class Sort {
public static void main(String[] args) {
int[] arr = { 24, 69, 80, 57, 13 };

bubbleSort(arr);

selectSort(arr);

for (int s : arr) {
System.out.print(s + "/");
}
}

//冒泡排序
public static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int a = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = a;
}
}
}
}

//选择排序
public static void selectSort(int[] arr){
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i+1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
int a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: