KnowledgeBoat Logo
|

Computer Applications

For the same array mentioned above in previous question, write a program to sort the above array using bubble sort technique. Give the array-status after every iteration.

Java

Java Arrays

2 Likes

Answer

public class KboatBubbleSort
{
    public static void main(String args[]) {
        int X[] = {13, 7, 6, 21, 35, 2, 28, 64, 45, 3, 5, 1};
        int n = X.length;
        
         //Bubble Sort
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (X[j] > X[j + 1]) {
                    int t = X[j];
                    X[j] = X[j+1];
                    X[j+1] = t;
                }
            } 
            System.out.println("Pass :" + (i + 1));
            for(int k = 0; k < n; k++) {
                System.out.print(X[k] + " ");
            }
            System.out.println();
        }    
        
        System.out.println("Sorted Array:");
        for (int i = 0; i < n; i++) {
            System.out.print(X[i] + " ");
        }
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of For the same array mentioned above in previous question, write a program to sort the above array using bubble sort technique. Give the array-status after every iteration.

Answered By

3 Likes


Related Questions