Sort Array
Program Info
Program Code
×

Sort Array

We are doing sort an array with the help of Bubble Sort Algorithm.

Footer

×

Sort Array

import java.util.Scanner;

public class Sort_Array_Using_Bubble_Sorting_Algorithm {

    public static void bubbleSort(int[] arr) {
        int n = arr.length;
         for(int i=0;i<n-1;i++){
            for(int j=0;j<n-1-i;j++){
                if(arr[j]>arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }

    public static void print(int[] arr) {
         for(int ele : arr){
            System.out.print(ele+" ");
         }
         System.out.println();
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        // First array input
        System.out.print("Enter size of array: ");
        int n = sc.nextInt();
        int[] arr = new int[n];

        System.out.println("Enter elements of array:");
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        System.out.println("Array Before sorted :");
        print(arr);
        bubbleSort(arr);
        System.out.println("Array after sorted :");
        print(arr);

        
    }

}

Footer

Print Diamond Pattern
Program Info
Program Code
×

Print Diamond Pattern

We are doing print an pattern of diamond shape.

Footer

×

Print Diamond Pattern

import java.util.Scanner;

public class Print_Diamond_Pattern {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int n = scan.nextInt();
        int nsp=n-1, nst=1;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= nsp; j++) {
                System.out.print("  ");
            }

            for (int k = 1; k <= nst; k++) {
                System.out.print("* ");
            }
            nsp--;
            nst +=2;
            System.out.println();
        }

        nsp=1; 
        nst=2*n-3;
        for (int i = 1; i <= n-1; i++) {
            for (int j = 1; j <= nsp; j++) {
                System.out.print("  ");
            }

            for (int k = 1; k <= nst; k++) {
                System.out.print("* ");
            }
            nsp++;
            nst -=2;
            System.out.println();
        }

        scan.close();
    }
}

Footer


×

Krishna Verma | Address : Dayampur, Kanker Khera, Meerut Cantt (250001), UP | Contact : 9520335394 , 7500045310 | Email Id : krishnaverma28081997@gmail.com | Website : krishnaportfolio.in | Technologies: C, C++, Java, HTML, CSS, Javascript, SQL, PHP, Python, TypeScript, Bootstrap, W3.CSS, Jquery, Node JS, Express JS, NumPy, Pandas, Wordpress, Codeigniter, Laravel, Angular, VueJS, Tailwind CSS, MySQL, MongoDB, AJAX, JSON, OOPS, Git & GitHub, AI Tools |