Write a program on Bubble Sort in java?

Introduction:

Hii everyone, here we are going to discuss the most interesting program i.e. Bubble Sort.Before initiating the discussion we must be aware of the sorting.Sorting is a process in which data is inserted in the array get arranged in ascending order or in descending order.In our code, the arrangement will be in ascending order.

Description Of Program:

In bubble sort, our first strategy is to take a variable Pivot, i.e. balancing instrument. First of all, we will pass an array, then we will run two loops on our program i.e. “i” loop is for running the loop until we sort the complete array and the loop will run up to second last integer of the array “a.lenght-2” because at the end we ended up getting the largest integer in the array.“j” loop will be used to find the minimum integer in one cycle, after getting the minimum integer we will leave that integer and work on the rest of the integers.Hope you find it helpful.

Thank You!

CODE :

package arrays;

public class BubbleSort {
public static void main(String[] args) {
int a[]={1,0,7,9,-3,2,4};
for(int i=0;i<=a.length-2;i++)
{
for(int j=0;j<=a.length-2-i;j++)
{
if(a[j]>a[j+1])
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(int i=0;i<=a.length-1;i++)
System.out.print(a[i] + “,”);
System.out.println();
System.out.println(java.util.Arrays.toString(a));
}
}

OUTPUT :

 

Leave a Reply