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

Bubble Sort

Bubble sort java

Uploaded by

4mt21cs182
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Bubble Sort

Bubble sort java

Uploaded by

4mt21cs182
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Problem Statement : Create a Bubble Sort Program to sort an input array of numbers in

ascending order.

Assumptions :
1.Input: A list of numbers (integers or floats).

2.Size: Any number of elements; an empty list returns empty.

Test Cases:
1. Test Case 1: General Case

Input: [64, 34, 25, 12, 22, 11, 90]

Expected Output: [11, 12, 22, 25, 34, 64, 90]

2. Test Case 2: Already Sorted Array

Input: [1, 2, 3, 4, 5]

Expected Output: [1, 2, 3, 4, 5]

3. Test Case 3: Reverse Sorted Array

Input: [5, 4, 3, 2, 1]

Expected Output: [1, 2, 3, 4, 5]

Program :
Import java.util.*;

Public class sort {

Static void bubble_sort(int[] arr, int n) {

For (int I = n – 1; I >= 0; i--) {

For (int j = 0; j <= I – 1; j++) {

If (arr[j] > arr[j + 1]) {

Int temp = arr[j];


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

Arr[j + 1] = temp;

System.out.println(“After bubble sort: “);

For (int I = 0; I < n; i++) {

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

System.out.println();

Public static void main(String args[]) {

Int arr[] = {13, 46, 24, 52, 20, 9};

Int n = arr.length;

System.out.println(“Before Using Bubble Sort: “);

For (int I = 0; I < n; i++) {

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

System.out.println();

Bubble_sort(arr, n);

Output:
Before Using Bubble Sort:

13 46 24 52 20 9
After bubble sort:

9 13 20 24 46 52

You might also like