Bubble Sort Java Program With Output

Bubble sort in java example. In the Java programming language, build a basic pattern program. To master any programming language, learn pattern programming. The pattern is printed in the first step of a complex level program written in a programming language. A decorative pattern that is replicated. In needlework and other crafts, a model or pattern is used as a reference.

Bubble Sort in Java

To sort the array elements, Bubble Sort is one of the most basic sorting algorithms in Java. The goal is to go from the first to the last element by comparing nearby items and exchanging them if they are out of order. Bubble sort is a basic sorting algorithm that compares neighbouring array elements and swaps them if the rightmost element is smaller than the leftmost.

It’s an in-place sorting method, which means it doesn’t take up any extra space and just modifies the array itself. The simplest sorting algorithm is Bubble Sort, which operates by periodically exchanging nearby components if they are in the wrong order. In this program we can execute bubble sort in java example.

Temp Variable in Java

Temp is only a name for a local variable, not a term. To give this variable a meaningful name, you can call it temp, blub, or even better: entry or value. A temporary variable is a variable in computer programming that has a brief lifetime and is used to retain data that will be removed quickly or before it can be stored in a more permanent memory location. 

It’s commonly stated as a local variable, or a variable with local scope, because it’s short-lived. A temporary array persists only for the period of the data step in which it is defined. A temporary array can be used to store constant values used in calculations. There are no comparable variables to identify the array elements in a temporary array.

public class Main
{ 
static void sort(int[] arr)
{ 
int n = arr.length; 
int temp = 0; 
for(int i=0; i < n; i++)
{ 
for(int j=1; j < (n-i); j++)
{ 
if(arr[j-1] > arr[j])
{ 
temp = arr[j-1]; 
arr[j-1] = arr[j]; 
arr[j] = temp; 
} 
} 
} 
} 
public static void main(String[] args) 
{ 
int arr[] ={300,78,900,34,1,565,34,887,44,66}; 
System.out.println("Before sorting"); 
for(int i=0; i < arr.length; i++)
{ 
System.out.print(arr[i] + " "); 
} 
System.out.println(); 
sort(arr);
System.out.println("After sorting"); 
for(int i=0; i < arr.length; i++)
{ 
System.out.print(arr[i] + " "); 
} 
} 
} 

Output:

Before Sorting     

300 78 900 34 1 565 34 887 44 66 

After sorting 

1 34 34 44 66 78 300 565 887 900