JAVAバブルアルゴリズム
泡出し方法1:
発泡方法2:
package com.java.work;
public class MaoPao {
public static void main(String args[]){
/* MaoPao mp=new MaoPao();
int[] as=new int[]{2,5,4,1,3,6};
mp.sort(as);
for(int i=0;i<as.length;i++){
System.out.println(as[i]);
}*/
MaoPao mp=new MaoPao();
int[] as=new int[]{6,5,4,3,2,1};
mp.sort(as);
for(int i=0;i<as.length;i++)
System.out.println(as[i]);
}
public static void sort(int data[]){
int temp;
for(int i=0;i<data.length;i++){
for(int j=data.length-1;j>i;j--){
if(data[i]>data[j]){
temp=data[i];
data[i]=data[j];
data[j]=temp;
}
}
}
}
}
発泡方法2:
package com.java.night.pracise;
public class Order {
public static void main(String args[]) {
int as[] = { 3, 5, 2, 1, 4, 6 };
sort(as);
for (int it : as) {
System.out.println(as[it - 1]);
}
}
public static void sort(int data[]) {
int temp;
for (int i = 0; i < data.length; i++) {
for (int j = data.length - 1; j > i; j--) {
if (data[i] > data[j]) {
temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
}
}
}