Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ICSE Java Programming Bubble Sort 2023 Q5 SPECIMEN PAPER

Friday, December 30, 2022

ICSE Java Programming Bubble Sort 2023 Q5 SPECIMEN PAPER

ICSE Java Programming Bubble Sort 2023 Q5 SPECIMEN PAPER

Bubble sort in java.




import java.util.Scanner;

public class ICSEJava2023 {
    public static void main(String[] args) {
        BubbleSort obj = new BubbleSort();
        obj.inputArray();
        System.out.println("Original Array;");
        obj.display();
        obj.sortArray();
        System.out.println("Sorted Array;");
        obj.display();
    }
}

class BubbleSort {
    public void sortArray() {
        for (int i = 0; i < this.array_size - 1; i++)
            for (int j = 0; j < this.array_size - i - 1; j++)
                if (this.array[j] < this.array[j + 1]) {
                    int temp = this.array[j];
                    this.array[j] = this.array[j + 1];
                    this.array[j + 1] = temp;
                }
    }

    public void inputArray() {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < this.array_size; i++) {
            System.out.println("Enter the element:");
            this.array[i] = in.nextInt();
        }
        in.close();
    }

    public void display() {
        for (int i = 0; i < this.array_size; i++)
            System.out.print(this.array[i] + " ");
        System.out.println();
    }

    private int[] array = new int[10];
    private static int array_size = 10;
}

No comments:

Post a Comment