# Answer to Question #52620 in Java | JSP | JSF for adem

Question #52620

bubble sort java program

Expert's answer

**Solution.**

**public**

**class**MainClass {

**public**

**static**

**void**main(String[] args) {

**int**temp;

// test array

**int**num[]={5,9,5,7,1,3,4,9,2,5,7,6};

System.

**.println("Unsorted array:");**

*out***for**(

**int**i = 0; i < num.length; i++)

System.

**.print(num**

*out**);*

//Bubble Sort

{

{

{

temp=num[j];

num[j]=num[j-1];

num[j-1]=temp;

} // end if

} // end for(j)

} // end for(i)

System..println("\n\nSorted array:");

//Bubble Sort

**for**(**int**i = num.length; i > 0; i--){

**for**(**int**j = 1; j < i; j++){

**if**(num[j] < num[j-1]){

temp=num[j];

num[j]=num[j-1];

num[j-1]=temp;

} // end if

} // end for(j)

} // end for(i)

System.

**[i]out****for**(

**int**i = 0; i < num.length; i++)

System.

**.print(num[i]);**

*out*}

}

## Comments

## Leave a comment