This is a Java program for Bubble Sort. Input the numbers you want to sort in numberArray[] array (below). File: BubbleSort.java
public class BubbleSort {
public static void main(String[] args) {
short numberArray[] = {7,99,-4,13,1,675,-37};
for(byte i = 0;i < numberArray.length - 1;i++)
{
for(byte j = (byte) (i + 1);j < numberArray.length;j++)
{
if(numberArray[i] > numberArray[j])
{
short temporary = numberArray[j];
numberArray[j] = numberArray[i];
numberArray[i] = temporary;
}
}
}
for(byte k = 0;k < numberArray.length;k++)
{
System.out.print(numberArray[k] + " ");
}
}
}
No comments:
Post a Comment