Can someone help me with my homework? my teacher asked us to create a program that lets the user to input 5 integers and sort it using bubblesort. i have already write the code but its not the output that i expected. heres the code:
import java.util.Scanner;
public class bubble {
static final int arraySize=5;
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int[] list=new int[arraySize];
int i,x;
bubbleSort(list,arraySize);
for(x=0;x<arraySize;x++){
System.out.print("Enter a number: ");
list[x]=scan.nextInt();
}
System.out.println("After sorting, the list elements are:");
for(i=0;i<arraySize;i++){
System.out.print(list[i]+" ");
}
System.out.println();
}
public static void bubbleSort(int list[], int listLength)
{
int temp;
int counter,index;
for(counter=0;counter<listLength-1;counter++)
{
for(index=0;index<listLength-1-counter;index++)
if(list[index]>list[index+1])
{
temp=list[index];
list[index]=list[index+1];
list[index+1]=temp;
}
}
}
}
for example i inputted numbers: 5,4,3,1,2
the output is still the same numbers that i inputted. what seems to be the problem. please help :(