Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Random;
- import static java.lang.System.*;
- public class BubbleSort {
- public static void main (String[] args){
- //made by APAD
- //generates the random array
- int[] myarray = new int[10];
- Random rand = new Random();
- for(int i = 0; i<myarray.length; i++){
- myarray[i] = rand.nextInt(101);
- }
- //int[] myarray = {41, 61, 31, 61, 22, 59, 72, 46, 36, 46};
- out.println("Original Array " + Arrays.toString(myarray));
- //organizes it one at a time in "passes"
- for(int z = 0; z<myarray.length-1; z++){
- boolean flag = true;
- int item = myarray[0];
- for(int j = 1; j<myarray.length; j++){
- if(item<=myarray[j]) item = myarray[j];
- else flag = false;
- }
- if(flag) break;
- int test = z+1;
- for(int f = 0; f<myarray.length-1; f++){
- if(myarray[f]>myarray[f+1]){
- int temp = myarray[f+1];
- myarray[f+1] = myarray[f];
- myarray[f] = temp;
- }
- }
- out.println("Pass " + test + " : " + Arrays.toString(myarray));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement