Created
September 26, 2016 23:44
-
-
Save JoeFurfaro/a67908e6742dcbe2d404afc214fbd499 to your computer and use it in GitHub Desktop.
Random Array Bubble Sorting Practice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BubbleSort | |
{ | |
public static void main(String[] args) | |
{ | |
int[] list = new int[10]; | |
for(int i = 0; i < list.length; i++) | |
{ | |
list[i] = (int) (Math.random() * 100) + 1; | |
} | |
list = bubbleSort(list); | |
for(int i = 0; i < list.length; i++) | |
{ | |
if(i != list.length - 1) | |
{ | |
System.out.print(list[i] + ", "); | |
} else | |
{ | |
System.out.print(list[i]); | |
} | |
} | |
} | |
static int[] bubbleSort(int[] list) | |
{ | |
int holder; | |
boolean flag = true; | |
while(flag) | |
{ | |
flag = false; | |
for(int i = 0; i < list.length - 1; i++) | |
{ | |
if(list[i] > list[i + 1]) | |
{ | |
holder = list[i]; | |
list[i] = list[i + 1]; | |
list[i + 1] = holder; | |
flag = true; | |
} | |
} | |
} | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment