Q. Wap to sort an array using Bubble Sort
public class bubble
{
public static void main(String[] args)
{
int[] arr = {16,45,66,52,78,96,77,71,79,72};
int n = arr.length;
System.out.print("Array : ");
for(int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
for(int i = 0; i < n-1; i++) {
for(int j = 0; j < n-i-1; j++)
{
if(arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
System.out.print("Sorted Array : ");
for(int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}
Comments
Post a Comment