Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Bubble Sort(Java)

This Java program implements the Bubble Sort algorithm to sort an array of integers in ascending order. It prompts the user to input the number of elements and the elements themselves, then sorts the array using nested loops to compare and swap elements. Finally, it displays the sorted array.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Bubble Sort(Java)

This Java program implements the Bubble Sort algorithm to sort an array of integers in ascending order. It prompts the user to input the number of elements and the elements themselves, then sorts the array using nested loops to compare and swap elements. Finally, it displays the sorted array.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

/* Java program to implement Bubble Sort in Ascending order(Smallest to Largest) */

import java.util.Scanner;

public class Bubble_Sort

public static void main(String args[])

Scanner in = new Scanner(System.in);

System.out.println("Enter the number of elements in the array:");

int n = in.nextInt(); //Represents the number of elements

int arr[] = new int[n]; //Declaring the array

System.out.println("Enter the elements of the array:");

for (int i = 0; i < n; i++)

arr[i] = in.nextInt(); //Entering the elements into the array in random order

//Performing Bubble Sort

for (int i = 0; i < n - 1; i++) //Loop for tracking the pass number

for (int j = 0; j < n - i - 1; j++) //Loop for tracking rounds in each pass

if (arr[j] > arr[j + 1]) //Swapping of elements

int t = arr[j];

arr[j] = arr[j+1];

arr[j+1] = t;
}

System.out.println("The elements of the Sorted Array are:");

for (int i = 0; i < n; i++) //Displaying the elements of the arry in Ascending order

System.out.print(arr[i]);

System.out.print(" ");

You might also like