본문 바로가기

DEMO CODE/JAVA 기초

[JAVA] Ex008 BubbleSort - 정렬

반응형

public class  BubbleSortMain

{

public static void main(String[] args

{

int [] a={5,6,3,7,4,8,2,9};

BubbleSort.print(a);

BubbleSort bubble=new BubbleSort();

int [] b=bubble.bbsortinc(a);

BubbleSort.print(b);

}

}

class  BubbleSort{

public  int[] bbsortinc(int [] a){

int n=a.length;

if(n<=0){

return new int[0];

}

for(int i=0;i<n-1;i++){

for(int j=0;j<n-1-i;j++){

if(a[j]>a[j+1]){  

int temp=a[j+1];  //스왑

a[j+1]=a[j];

a[j]=temp;

}

}

}

return a;

}

public static void print(int [] a){

int count=a.length;

System.out.print("["+a[0]+", ");

for(int i=1;i<count-1;i++){

System.out.print(a[i]+", ");

}

System.out.println(a[count-1]+"]");

}

public  int[] bbsortdec(int [] a){

int n=a.length;

if(n<=0){

return new int[0];

}

for(int i=0;i<n-1;i++){

for(int j=0;j<n-1-i;j++){

if(a[j]<a[j+1]){  

int temp=a[j+1];  //스왑

a[j+1]=a[j];

a[j]=temp;

}

}

}

return a;

}

}



반응형

'DEMO CODE > JAVA 기초' 카테고리의 다른 글

[JAVA] Ex010 로또번호 출력하기  (0) 2015.05.19
[JAVA] Ex009 ReverseArray - 정렬  (0) 2015.05.19
[JAVA] Ex007 Calendar, Date 정보출력  (0) 2015.05.19
[JAVA] Ex006 다중for문, if 예제  (0) 2015.05.19
[JAVA] Ex005 Math method  (0) 2015.05.19