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

Java Program To Check Number Using Bubble Sort

This Java program uses bubble sort to sort an integer array from largest to smallest. It prints the array before and after sorting, and shows each pass of the bubble sort algorithm by printing the elements being compared and whether they are swapped. The bubbleSort method performs the sorting by looping through the array multiple times to compare and swap adjacent elements into order from least to greatest.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Java Program To Check Number Using Bubble Sort

This Java program uses bubble sort to sort an integer array from largest to smallest. It prints the array before and after sorting, and shows each pass of the bubble sort algorithm by printing the elements being compared and whether they are swapped. The bubbleSort method performs the sorting by looping through the array multiple times to compare and swap adjacent elements into order from least to greatest.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Program to Check Number using Bubble Sort

package onecaintacollege;

public class BubbleSort {

public static void main(String[] args)


{
int arr[] ={860,8,200,9};

System.out.println("---Array BEFORE Bubble Sort---");

printArray(arr);

bubbleSort(arr);//sorting array elements using bubble sort

System.out.println("---Array AFTER Bubble Sort---");

printArray(arr);

}
static void bubbleSort(int[] array)
{
int n = array.length;
int temp = 0;
for(int i=0; i < n; i++) // Looping through the array length
{ System.out.println("Sort Pass Number "+(i+1));
for(int j=1; j < (n-i); j++)
{
System.out.println("Comparing "+ array[j-1]+ " and " + array[j]);
if(array[j-1] > array[j])
{

//swap elements
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
System.out.println(array[j] + " is greater than " + array[j-1]);
System.out.println("Swapping Elements: New Array After Swap");
printArray(array);
}

}
}

static void printArray(int[] array){

for(int i=0; i < array.length; i++)


{
System.out.print(array[i] + " ");
}
System.out.println();

}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy