0% found this document useful (0 votes)
356 views

Java Programming Code For Bubble Sort

The document describes how to perform bubble sort in Java by having the user enter the size of an array and its elements, then sorting the array using the bubble sort technique. It provides a Java code example that uses a bubble sort algorithm to sort an integer array in ascending order by repeatedly swapping adjacent elements that are in the wrong order.

Uploaded by

sonam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
356 views

Java Programming Code For Bubble Sort

The document describes how to perform bubble sort in Java by having the user enter the size of an array and its elements, then sorting the array using the bubble sort technique. It provides a Java code example that uses a bubble sort algorithm to sort an integer array in ascending order by repeatedly swapping adjacent elements that are in the wrong order.

Uploaded by

sonam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Bubble Sort in Java

To perform bubble sort in Java programming, you have to ask to the user to enter
the array size then ask to enter the array elements, now start sorting the array elements
using the bubble sort technique.

Java Programming Code for Bubble Sort

Following Java Program sort the array using the Bubble Sort technique :

/* Java Program Example - Bubble Sort */

import java.util.Scanner;

public class JavaProgram


{
public static void main(String args[])
{
int n, i, j, temp;
int arr[] = new int[50];
Scanner scan = new Scanner(System.in);

System.out.print("Enter Total Number of Elements : ");


n = scan.nextInt();

System.out.print("Enter " +n+ " Numbers : ");


for(i=0; i<n; i++)
{
arr[i] = scan.nextInt();
}

System.out.print("Sorting Array using Bubble Sort Technique...\n");


for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

System.out.print("Array Sorted Successfully..!!\n");

System.out.print("Sorted List in Ascending Order : \n");


for(i=0; i<n; i++)
{
System.out.print(arr[i]+ " ");
}
}
}

When the above Java Program is compile and executed, it will produce the following output:

You might also like