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

数组选择排序和冒泡排序

2017-07-13 23:13 211 查看
1、选择排序(直接排序)

public class ArraySelectSort {

public static void main(String[] args) {
int[] arr = {11,12,79,2,5,20};
selectSort(arr); //结果: [79,20,12,11,5,2]
}

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[j]>arr[i]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
}


2、冒泡排序

public class ArrayBubbleSort {

public static void main(String[] args) {
int[] arr = {11,12,79,2,5,20};
bubbleSort(arr); //结果: [2,5,11,12,20,79]
}

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;
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java数组排序
相关文章推荐