Pages

Pages

Bubblesort - Java

Program:
import java.io.*;
class arraylist
{
    int a[];
    int n;
    arraylist(int max)
    {
        a=new int[max];
        n=0;
    }
    void insert(int x)
    {
        a[n]=x;
        n++;
    }
    public void display()
    {
        for(int i=1;i<=n;i++)
            System.out.print(a[i]+" ");
            System.out.print("\n");
    }
    public void bblsort()
    {
        for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            if(a[j]>a[j+1])
            {
                int t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }
    }
}

class sort
{
public static void main(String args[])throws IOException
{
    System.out.println("Enter how many elements:");
    InputStreamReader isr=new InputStreamReader(System.in);
    BufferedReader br= new BufferedReader(isr);
    String s= br.readLine();
    int n=Integer.parseInt(s);
    arraylist a1=new arraylist(100);
    System.out.println("Enter the elements:");
    for(int i=0;i<n;i++)
    {
        InputStreamReader bsr=new InputStreamReader(System.in);
        BufferedReader ar= new BufferedReader(bsr);
        String f= ar.readLine();
        int k=Integer.parseInt(f);
        a1.insert(k);
    }
    a1.bblsort();
    System.out.println("\nThe sorted elements are:- ");
    a1.display();
}
}


Output:
nn@linuxmint ~ $ javac sort.java
nn@linuxmint ~ $ java sort
Enter how many elements:
5
Enter the elements:
5
4
3
2
1

The sorted elements are:-
1 2 3 4 5
nn@linuxmint ~ $

6 comments:

  1. Very cool!

    เว็บไซต์คาสิโนออนไลน์ที่ได้คุณภาพอับดับ 1 ของประเทศ
    เป็นเว็บไซต์การพนันออนไลน์ที่มีคนมา สมัคร Gclub Royal1688
    และยังมีหวยให้คุณได้เล่น สมัครหวยออนไลน์ ได้เลย
    สมัครสมาชิกที่นี่ >>> Gclub Royal1688
    ร่วมลงทุนสมัครเอเย่นคาสิโนกับทีมงานของเราได้เลย

    ReplyDelete
  2. This article is a great article that I have seen so far in my programming career, it has helped me a lot to make an argument for the functionality of Python, and will continue to do so in the future.

    hire python developers in US

    ReplyDelete