Q. Wap to sort an array using Bubble Sort


public class bubble

{

    public static void main(String[] args)

    {

        int[] arr = {16,45,66,52,78,96,77,71,79,72};

        int n = arr.length;

        System.out.print("Array : ");

        for(int i = 0; i < n; i++) {

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

        }

        System.out.println();

        for(int i = 0; i < n-1; i++) {

            for(int j = 0; j < n-i-1; j++)

            {

                if(arr[j] > arr[j+1])

                {

                    int temp = arr[j];

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

                    arr[j+1] = temp;

                }

            }

        }

        System.out.print("Sorted Array : ");

        for(int i = 0; i < n; i++) {

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

        }

    }

}


Comments

Popular posts from this blog

Completed Certification from J.P. Morgan - Software Engineering Job Simulation

Student Registration form using PHP

Create a java Applet program of Calculator