Skip to content

Instantly share code, notes, and snippets.

@JoeFurfaro
Created September 26, 2016 23:44
Show Gist options
  • Save JoeFurfaro/a67908e6742dcbe2d404afc214fbd499 to your computer and use it in GitHub Desktop.
Save JoeFurfaro/a67908e6742dcbe2d404afc214fbd499 to your computer and use it in GitHub Desktop.
Random Array Bubble Sorting Practice
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