# How do you sort an array? [using bubble sort]

• November 27th, 2013, 02:34 PM
namenamename
How do you sort an array? [using bubble sort]
Here's my code:
Code :

```import java.util.*;   class array1{ public static void main(String args[]){ Random number = new Random();   int num[] = new int[100];   // filling array with random numbers for(int i=0;i<100;i++){ int randomnumber = 10+number.nextInt(89); num[i] = randomnumber; System.out.print(num[i]+" "); } // Sorting the array for(int i=0;i<100;i++){ for(int j=0;j<100;j++){ if(num[j]>num[j+1]){ num[j]=num[j+1]; num[j+1]=num[j];   } } } } }```
What should I put after the "num[j+1]=num[j]"?
• November 27th, 2013, 02:45 PM
aussiemcgr
Re: How do you sort an array? [using bubble sort]
This:
Code java:

```num[j]=num[j+1]; num[j+1]=num[j];```
Will not swap values. For example, let's say:
num[j]=10
num[j+1]=12
After we run the first line of code, the values become:
num[j]=12
num[j+1]=12
And after we run the second line of code, the values stay:
num[j]=12
num[j+1]=12

Why? Because we never "remembered" the previous value of num[j]