Program:
import java.util.Scanner;
class BubbleSortProgram {
public static void main(String []args) {
int i,j, swap;
Scanner in = new Scanner(System.in);
System.out.println("Enter Number : ");
int n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
for (i = 0; i < n; i++)
array[i] = in.nextInt();
for (i= 0; i < ( n - 1 ); i++) {
for (j = 0; j< n - i - 1; j++) {
if (array[j] > array[j+1])
{
swap = array[j];
array[j] = array[j+1];
array[j+1] = swap;
}
}
}
System.out.println("After Sorted Elements : ");
for (i = 0; i < n; i++)
System.out.println(array[i]);
}
}
output:
Enter Number :
4
Enter 4 integers
3
2
5
4
After Sorted Elements :
2
3
4
5
0 comments:
Post a Comment