A:建一个MaoPaoDemo的类,代码如下:
package com.webservice.demo;
/***
* 冒泡排序
* @author Administrator
*
*/
public class MaoPaoDemo {
public static void main(String[] args) {
int a[] = {11,2,5,82,7,0,4,89,72,42,16,34,58,23};
for(int i=0;i<a.length;i++){
for(int j=0;j<a.length-1-i;j++){
if(a[j] > a[j+1]){
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for(int x:a){
System.out.println(x);
}
}
}
执行上述代码获得运行结果为:
0 2 4 5 7 11 16 23 34 42 58 72 82 89
B:简历一个选择性排序的SelectDemo类,代码如下:
/****
* 选择型排序
* @author Administrator
*
*/
public class SelectDemo {
public static void main(String[] args) {
int arr[] = {11,2,5,82,7,0,4,89,72,42,16,34,58,23};
for(int i=0;i<arr.length;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[i]>arr[j]){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(int x:arr){
System.out.println(x);
}
}
}
执行上述代码获得运行结果为:
0 2 4 5 7 11 16 23 34 42 58 72 82 89
综上所述两种常见的排序方式获得结果是一致的!