Showing posts with label Sort. Show all posts
Showing posts with label Sort. Show all posts

Java Swing Example : JList, JOptionPane, NetBeans


Java Swing Examples: JList
NetBeans - Internet & Web Programming Lab
Design:

Sourcecode:

/*
 * sw4_list.java
 *
 * Created on Nov 26, 2011, 1:32:59 PM
 */
package swing;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author nn
 */
public class sw4_list extends javax.swing.JFrame {
DefaultListModel l1,l2;
    /** Creates new form sw4_list */
    public sw4_list() {
        initComponents();
        jList1.setModel(l1= new DefaultListModel());
        jList2.setModel(l2= new DefaultListModel());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setViewportView(jList1);

        jScrollPane2.setViewportView(jList2);

        jButton1.setText("ADD");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("REMOVE");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("SORT");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("<");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText(">");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel1.setText("List 1");

        jLabel2.setText("List 2");

        jButton6.setText("<<");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setText(">>");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(57, 57, 57)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel2))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel1)
                .addGap(8, 8, 8)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(jButton1)
                .addGap(1, 1, 1)
                .addComponent(jButton2)
                .addGap(1, 1, 1)
                .addComponent(jButton3)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton4)
                    .addComponent(jButton5))
                .addGap(1, 1, 1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel2)
                .addGap(3, 3, 3)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    Object[] listops= {"List 1","List 2"};
            
    String ch=(String) JOptionPane.showInputDialog(rootPane, "Select","Select List Box", JOptionPane.PLAIN_MESSAGE,null, listops,"list1");
    if(ch.compareTo("List 1")==0)
            l1.addElement(JOptionPane.showInputDialog("Enter the element: "));
    else
        l2.addElement(JOptionPane.showInputDialog("Enter the element: "));
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
    Object[] obj1 = jList1.getSelectedValues();
    Object[] obj2 = jList2.getSelectedValues();
    if((obj1.length==0)&&(obj2.length==0))
            JOptionPane.showMessageDialog(rootPane, "Please select an item");
    else
    {
        int i;
        for(i=0;i<obj1.length;i++)
        l1.removeElement(obj1[i]);
        for(i=0;i<obj2.length;i++)
        l2.removeElement(obj2[i]);
    }
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
     Object[] listops= {"List 1","List 2"};
            
    String ch=(String) JOptionPane.showInputDialog(rootPane, "Select","Select List Box", JOptionPane.PLAIN_MESSAGE,null, listops,"list1");
    if(ch.compareTo("List 1")==0)
           l1= sort(l1);
    else
        l2=sort(l2);
}//GEN-LAST:event_jButton3ActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
// TODO add your handling code here:
     Object[] temp = jList1.getSelectedValues();
   if(temp.length>1)
       JOptionPane.showMessageDialog(rootPane, "Please select only one item...");
   else if(temp.length<1)
       JOptionPane.showMessageDialog(rootPane, "Please select an item...");
   else
   {
       l2.addElement(temp[0]);
       l1.removeElement(temp[0]);
   }
}//GEN-LAST:event_jButton5ActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
// TODO add your handling code here:
    Object[] temp = jList2.getSelectedValues();
      if(temp.length>1)
       JOptionPane.showMessageDialog(rootPane, "Please select only one item...");
   else if(temp.length<1)
       JOptionPane.showMessageDialog(rootPane, "Please select an item...");
   else{
    l1.addElement(temp[0]);
    l2.removeElement(temp[0]);
   }
}//GEN-LAST:event_jButton4ActionPerformed

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
// TODO add your handling code here:
    Object[] temp = jList1.getSelectedValues();
    if(temp.length<1)
       JOptionPane.showMessageDialog(rootPane, "Please select an item...");
   else{
        
        for(int i=0;i<temp.length;i++)
        {
            l2.addElement(temp[i]);
            l1.removeElement(temp[i]);
        }
    }
}//GEN-LAST:event_jButton7ActionPerformed

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
// TODO add your handling code here:
    Object[] temp = jList2.getSelectedValues();
    if(temp.length<1)
       JOptionPane.showMessageDialog(rootPane, "Please select an item...");
   else{
        
    
    for(int i=0;i<temp.length;i++)
    {
        l1.addElement(temp[i]);
        l2.removeElement(temp[i]);
    }}
}//GEN-LAST:event_jButton6ActionPerformed
public DefaultListModel sort (DefaultListModel dm){
    Object[] obj = dm.toArray();
    int n= obj.length;
    for(int i=0;i<n;i++)
        for(int j=0;j<n-i-1;j++)
            if(Integer.parseInt(obj[j].toString())>Integer.parseInt(obj[j+1].toString()))
            {
                Object temp = obj[j];
                obj[j]=obj[j+1];
                obj[j+1]=temp;
            }
    dm.removeAllElements();
    for(int i=0;i<n;i++)
        dm.addElement(obj[i]);
    return dm;
}
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(sw4_list.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(sw4_list.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(sw4_list.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(sw4_list.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new sw4_list().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}

Inter Process Communication using Pipe - Sorting - Systems Lab

Program:
#include<stdio.h>
main()
 {
      int i,j,n,n1;
      int pid;
      int a[5],b[5],c[5];
      int p1[2],p2[2];
       pipe(p1);
      pipe(p2);
      pid=fork();
      if(pid==0)
      {
      printf("enter the limit \n");
            fflush(stdin);
           scanf("%d",&n);
          
               close(p1[0]);
        write(p1[1],&n,sizeof(int));
    close(p2[1]);
    read(p2[0],b,5*sizeof(int));
      for(i=0;i<n;i++)
        printf("%d\t",b[i]);


        }
        else
        {
         close(p1[1]);
    read(p1[0],&n1,sizeof(int));
    printf("enter the numnbers");
             for(i=0;i<n1;i++)
               scanf("%d",&a[i]);
 
      for(i=0;i<n1;i++)
      for(j=0;j<n1-i-1;j++)
       if(b[j]>b[j+1])
       {
         int t=b[j];
         b[j]=b[j+1];
         b[j+1]=t;
        }
   
    close(p2[0]);
    write(p2[1],a,5*sizeof(int));
   }
 }
   
#include<stdio.h>
main()
 {
      int i,j,n,n1;
      int pid;
      int a[5],b[5],c[5];
      int p1[2],p2[2];
       pipe(p1);
      pipe(p2);
      pid=fork();
      if(pid==0)
      {
      printf("enter the limit \n");
            fflush(stdin);
           scanf("%d",&n);
           
               close(p1[0]);
        write(p1[1],&n,sizeof(int));
    close(p2[1]);
    read(p2[0],b,5*sizeof(int));
      for(i=0;i<n;i++)
        printf("%d\t",b[i]);


        }
        else
        {
         close(p1[1]);
    read(p1[0],&n1,sizeof(int));
    printf("enter the numnbers");
             for(i=0;i<n1;i++)
               scanf("%d",&a[i]);
 
      for(i=0;i<n1;i++)
      for(j=0;j<n1-i-1;j++)
       if(b[j]>b[j+1])
       {
         int t=b[j];
         b[j]=b[j+1];
         b[j+1]=t;
        }
   
    close(p2[0]);
    write(p2[1],a,5*sizeof(int));
   }
 }


Output:
 nn@ubuntu:~$ gcc pb1.c
nn@ubuntu:~$ ./a.out
enter the limit
5
enter the numnbers5
4
3
2
1
5    4    3    2    1
nn@ubuntu:~$ gcc pb1.c

Sorting Using Template - C++

Program:

Download Runnable Code

#include<iostream>
using namespace std;
template<class t>
void sort(t a[],int n)
{
    int i,j;t tem;
    for(i=0;i<n;i++)
        for(j=0;j<n-i-1;j++)
            if(a[j]<a[j+1])
            {
                tem=a[j];
                a[j]=a[j+1];
                a[j+1]=tem;
            }
    cout<<"\n\t--------After Sorting---------\n\n";
    for(i=0;i<n;i++) 
        cout<<" "<<a[i];
}
               
int main()
{
    int i,n,a[25],ch;float b[25];bool exit=false;   
    cout<<"\n\t------SORTING USING TEMPLATE-----------\n\nEnter the number of elements: ";
    cin>>n;

    do{
        cout<<"\n\t--------MENU---------\n\n\t1.Integer Sorting\n\t2.Floating point Sorting\n\t3.Exit\n\nEnter your choice:";
        cin>>ch;
        switch(ch)
        {
            case 1:
                cout<<"\n\t------INT TYPE-----------\n\n";
                cout<<"\nEnter elements:\n";
                for(i=0;i<n;i++)
                    cin>>a[i];
                sort(a,n);
                break;
           
            case 2:
                cout<<"\n\t------FLOAT TYPE-----------\n\n";
                cout<<"\nEnter elements:\n";
                for(i=0;i<n;i++)
                    cin>>b[i];
                sort(b,n);
                break;
            case 3:
               
            default:
                cout<<"Exiting..... \n\t\t\t\t\t\t...www.2k8618.blogspot.com\n";
                exit=true;
        }
    }while(!exit);
return 0;   
}

Output:

nn@ubuntu:~$ ./a.out

    ------SORTING USING TEMPLATE-----------

Enter the number of elements: 5

    --------MENU---------

    1.Integer Sorting
    2.Floating point Sorting
    3.Exit

Enter your choice:1

    ------INT TYPE-----------


Enter elements:
5
4
3
2
1

    --------After Sorting---------

 5 4 3 2 1
    --------MENU---------

    1.Integer Sorting
    2.Floating point Sorting
    3.Exit

Enter your choice:2

    ------FLOAT TYPE-----------


Enter elements:
1.9
1.2
1.0
1.4
1.7

    --------After Sorting---------

 1.9 1.7 1.4 1.2 1
    --------MENU---------

    1.Integer Sorting
    2.Floating point Sorting
    3.Exit

Enter your choice:3
Exiting.....
                                                                       ...www.2k8618.blogspot.com

Download

String Sort - LISP

Program:

Download Runnable Code

;;BUBBLESORT (STRING)

(defun breadn(n)
(setf arr (make-array n))
(format t "Enter the strings:~&")
(dotimes (x n t)
(setf (aref arr x) (read))))


(defun bprintn(n)
(dotimes(x n t)
    (print (aref arr x))))
(defun swap(x y)
    (setf temp (aref arr x))
    (setf (aref arr x) (aref arr y))
    (setf (aref arr y) temp)
)

(defun bsortn ( n )
    (breadn n);;(bprintn n)
    (do (( i 1 (+ i 1)))
    ((= i n))
    (setf k (- n 1))
    (do((j 0 (+ j 1    )))
    ((= j k))
    (setf m (+ j 1))
    (if (string> (aref arr j) (aref arr m))
        (swap j m))
))
    (bprintn n)
)
Output:
Break 2 [4]> (load 'bbs.lsp)
;; Loading file bbs.lsp ...
;; Loaded file bbs.lsp
T
Break 2 [4]> (bsortn 5)
Enter the strings:
orange
guava
apple
banana
mango

APPLE
BANANA
GUAVA
MANGO
ORANGE
T
Break 2 [4]>

Download

Bubblesort - LISP

;;BUBBLESORT
(defun bsortn (n)
    (format t "    <<<BUBBLESORT FOR ~D NUMBERS>>>~&" n)
    (breadn n)
    (do (( i 0 (+ i 1))) ((= i (- n 1)))
        (do ((j 0 (+ j 1))) ((= j (- (- n i) 1)))
            (if (> (aref arr j) (aref arr (+ j 1)))
                (swap j (+ j 1))
            )
        )
    )
    (bprintn n)
)
(defun breadn(n)
    (setf arr (make-array n))
    (format t "Enter the numbers:~&")
    (dotimes (x n t)
        (setf (aref arr x) (read) )
    )
)
(defun bprintn(n)
    (dotimes(x n t)
        (print (aref arr x) )
    )
)
(defun swap(x y)
    ( setf temp (aref arr x) )
    ( setf (aref arr x) (aref arr y) )
    ( setf (aref arr y) temp )
)

Output:
Break 1 [2]> (load 'b.lsp)
;; Loading file b.lsp ...
;; Loaded file b.lsp
T
Break 1 [2]> (bsortn 5)
<<<BUBBLESORT FOR 5 NUMBERS>>>
Enter the numbers:
5
4
3
2
1
1
2
3
4
5
T
Break 1 [2]>            http://www.2k8618.blogspot.com/


Sorting Algorithms - JAVA

Program:
import java.io.*;
class arraylist
{
    private int[] a;
    private int n;
    private int[] temp;
    public arraylist(int x)
    {
        a = new int[x];
        n=0;
        temp = new int[x];
    }
    public void insert(int e)
    {
        n++;
        a[n]=e;
    }
    public void display()
    {
        for(int i=1;i<=n;i++)
        System.out.print(a[i]+" ");
        System.out.println("\n");
    }
    public void bubble()    //Bubblesort
    {   
        System.out.print("(bubblesort)\n\n");
        display();
        for(int i=1;i<n;i++)
        for(int j=1;j<=n-i;j++)
        if(a[j]>a[j+1])
        {
            swap(j,j+1);
            display();
        }
    }
    public void swap(int x,int y)
    {
            int temp=a[x];
            a[x]=a[y];
            a[y]=temp;
    }
    public void selection()    //selectionsort
    {
        System.out.print("(selectionsort)\n\n");
        display();
        for(int i=1;i<n;i++)
        {
            int min=i;
            for(int j=i+1;j<=n;j++)
                if(a[j]<a[min])
                    min=j;
            swap(i,min);
            display();
        }
    }
    public void insertion()    //insertionsort
    {
        System.out.print("(insertionsort)\n\n");
        display();
        for(int j=2;j<=n;j++)
        {
            int key=a[j];int i=j-1;
            while((i>0)&&(a[i]>key))
            {
                a[i+1]=a[i];
                i--;
            }
            a[i+1]=key;
        display();
        }
    }
    public void quick(int first,int last)    //quicksort
    {
       
       
        if(last>first)
        {
            int i=first;int j=last+1;int pivot=a[first];
            do
            { 
                do{
                    i=i+1;
                  }while((a[i]<=pivot)&&(i<last));
                do{
                    j=j-1;   
                  }while((a[j]>=pivot)&&(j>first));
                   
                if(i<j)
                {   
                   
                    swap(i,j);
                    display();
                }
            }while(i<j);
            swap(first,j);
            display();
            quick(first,j-1);
            quick(j+1,last);
        }
    }
    public void merge(int low,int high)    //mergesort
    {
        if(low!=high)
        {
            int mid=(low+high)/2;
            merge(low,mid);
            merge(mid+1,high);
            rmerge(low,mid,high);
        }
    }
    public void rmerge(int low,int mid,int high)
    {
       
        int i=low;int j=mid+1;int k=low;
       
        while((i<=mid)&&(j<=high))
        {
            if(a[i]<=a[j])
                {
                    temp[k]=a[i];k++;i++;
                }
            else
                {
                    temp[k]=a[j];k++;j++;
                }
        }
        while(i<=mid)
        {
            temp[k]=a[i];k++;i++;
        }
        while(j<=high)
        {
            temp[k]=a[j];k++;j++;
        }
        for(i=1;i<=high;i++)
            a[i]=temp[i];
            display();

    }
    public void buildheap()    //heapsort
    {
        for(int i=n/2;i>=1;i--)
        maxheapify(n,i);
    }
    public void maxheapify(int n,int i)
    {
        int largest;
        int l=left(i);
        int r=right(i);
        if(l<=n&&a[l]>a[i])
            largest=l;
        else
            largest=i;
        if(r<=n&&a[r]>a[largest])
            largest=r;
        if(largest!=i)
        {
            swap(i,largest);
            maxheapify(n,largest);
        }
    }
    public void heapsort()
    {
        buildheap();
        int heapsize=n;
        for(int i=heapsize;i>=2;i--)
        {
            swap(i,1);
            display();
            heapsize--;
            maxheapify(heapsize,1);
        }
    }
    public int left(int a)
    {
        return 2*a;
    }
    public int right(int b)
    {
        return 2*b+1;
    }
    public void nidhi()
    {
        System.out.print("\t\t\t\t\t\t* www.2k8618.blogspot.com *\n");
    }
   
    public void end()
    {
        System.out.println("\n\n\t\t\t***     ***\n\t\t\t*****   ***\n\t\t\t*** *** ***\n\t\t\t***   *****\n\t\t\t***    ****\n");
        System.out.print("\n\t\t<<<<SORTING ALGORITHMS>>>>\n");
        char z=177;
        System.out.print("\t\t"+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z);
        char y=2;
        System.out.print("\n\t\t\t @ 2010 - www.2k8618.blogspot.com -\n\t\t2k8618@gmail.com,S4-CSE,GCEK\n");
        for(int x=1;x<=75;x++)
            System.out.print(""+y);
        System.out.print("\n");
    }

}

class nsort
{
    public static void main(String arg[]) throws IOException
    {
        int exit=1;arraylist ob;
        System.out.println("\n\n\t\t\t***     ***\n\t\t\t*****   ***\n\t\t\t*** *** ***\n\t\t\t***   *****\n\t\t\t***    ****\n");
        System.out.print("\n\t\t<<<<SORTING ALGORITHMS>>>>\n");
        char z=177;
        System.out.print("\t\t"+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z);
        do{
           
            DataInputStream x1= new DataInputStream(System.in);
       
            System.out.print("\nEnter the number of elements:");
            int na=Integer.parseInt(x1.readLine());
            ob=new arraylist(na+1);
            System.out.println("Enter the elements:");
            for (int k=1;k<=na;k++)
                ob.insert(Integer.parseInt(x1.readLine()));
            ob.nidhi();
            System.out.println("Before sorting:\n");
            ob.display();
            System.out.println("Enter your choice:\n\n\t1.BUBBLESORT\n\t\t2.SELECTIONSORT\n\t\t\t3.INSERTIONSORT\t\n\t4.QUICKSORT\n\t\t5.MERGESORT\n\t\t\t6.HEAPSORT\t\t*www.2k8618.blogspot.com*\n\t\t\t\t\t7.Exit>>>\n");       
            int turn=Integer.parseInt(x1.readLine());
            if(turn>=1 && turn <=6)
                System.out.println("Sorting.............");
       
            ob.nidhi();
            switch(turn)
            {
                case 1:       
                    ob.bubble();
                    System.out.println("After sorting:\n");
                    break;
                case 2:       
                    ob.selection();
                    System.out.println("After sorting:\n");
                    break;
                case 3:       
                    ob.insertion();
                    System.out.println("After sorting:\n");
                    break;
                case 4:
                    System.out.print("(QuickSort)\n\n");
                    ob.display();
                    ob.quick(1,na);
                    System.out.println("After sorting:\n");
                    break;
                case 5:
                    System.out.print("(MergeSort)\n\n");
                    ob.display();
                    ob.merge(1,na);
                    System.out.println("After sorting:\n");
                    break;
                case 6:
                    System.out.print("(HeapSort)\n\n");
                    ob.display();
                    ob.heapsort();
                    System.out.println("After sorting:\n");
                    break;
                case 7:
                    exit=0;
                    System.out.println("Not sorted:\n");
                    break;
                default:
                    System.out.println("Not sorted:\n");
                    break;
            }
       
            ob.display();ob.nidhi();
            System.out.println("\nPress 1 to continue...");
            exit=Integer.parseInt(x1.readLine());
        }while(exit==1);
        ob.end();
    }
}

Output:
nn@linuxmint ~ $ javac nsort.java
Note: nsort.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java nsort


            ***     ***
            *****   ***
            *** *** ***
            ***   *****
            ***    ****


        <<<<SORTING ALGORITHMS>>>>
        ±±±±±±±±±±±±±±±±±±±±±±±±±±
Enter the number of elements:5
Enter the elements:
5
4
3
2
1
                        * www.2k8618.blogspot.com *
Before sorting:

5 4 3 2 1 

Enter your choice:

    1.BUBBLESORT
        2.SELECTIONSORT
            3.INSERTIONSORT   
    4.QUICKSORT
        5.MERGESORT
            6.HEAPSORT        *www.2k8618.blogspot.com*
                    7.Exit>>>

1
Sorting.............
                        * www.2k8618.blogspot.com *
(bubblesort)

5 4 3 2 1 

4 5 3 2 1 

4 3 5 2 1 

4 3 2 5 1 

4 3 2 1 5 

3 4 2 1 5 

3 2 4 1 5 

3 2 1 4 5 

2 3 1 4 5 

2 1 3 4 5 

1 2 3 4 5 

After sorting:

1 2 3 4 5 

                        * www.2k8618.blogspot.com *

Press 1 to continue...
1

Enter the number of elements:5
Enter the elements:
5
4
3
2
1
                        * www.2k8618.blogspot.com *
Before sorting:

5 4 3 2 1 

Enter your choice:

    1.BUBBLESORT
        2.SELECTIONSORT
            3.INSERTIONSORT   
    4.QUICKSORT
        5.MERGESORT
            6.HEAPSORT        *www.2k8618.blogspot.com*
                    7.Exit>>>

2
Sorting.............
                        * www.2k8618.blogspot.com *
(selectionsort)

5 4 3 2 1 

1 4 3 2 5 

1 2 3 4 5 

1 2 3 4 5 

1 2 3 4 5 

After sorting:

1 2 3 4 5 

                        * www.2k8618.blogspot.com *

Press 1 to continue...
1

Enter the number of elements:5
Enter the elements:
5
4
3
2
1
                        * www.2k8618.blogspot.com *
Before sorting:

5 4 3 2 1 

Enter your choice:

    1.BUBBLESORT
        2.SELECTIONSORT
            3.INSERTIONSORT   
    4.QUICKSORT
        5.MERGESORT
            6.HEAPSORT        *www.2k8618.blogspot.com*
                    7.Exit>>>

3
Sorting.............
                        * www.2k8618.blogspot.com *
(insertionsort)

5 4 3 2 1 

4 5 3 2 1 

3 4 5 2 1 

2 3 4 5 1 

1 2 3 4 5 

After sorting:

1 2 3 4 5 

                        * www.2k8618.blogspot.com *

Press 1 to continue...
1

Enter the number of elements:5
Enter the elements:
5
4
3
2
1
                        * www.2k8618.blogspot.com *
Before sorting:

5 4 3 2 1 

Enter your choice:

    1.BUBBLESORT
        2.SELECTIONSORT
            3.INSERTIONSORT   
    4.QUICKSORT
        5.MERGESORT
            6.HEAPSORT        *www.2k8618.blogspot.com*
                    7.Exit>>>

4
Sorting.............
                        * www.2k8618.blogspot.com *
(QuickSort)

5 4 3 2 1 

1 4 3 2 5 

1 4 3 2 5 

1 2 3 4 5 

1 2 3 4 5 

After sorting:

1 2 3 4 5 

                        * www.2k8618.blogspot.com *

Press 1 to continue...
1

Enter the number of elements:5
Enter the elements:
5
4
3
2
1
                        * www.2k8618.blogspot.com *
Before sorting:

5 4 3 2 1 

Enter your choice:

    1.BUBBLESORT
        2.SELECTIONSORT
            3.INSERTIONSORT   
    4.QUICKSORT
        5.MERGESORT
            6.HEAPSORT        *www.2k8618.blogspot.com*
                    7.Exit>>>

5
Sorting.............
                        * www.2k8618.blogspot.com *
(MergeSort)

5 4 3 2 1 

4 5 3 2 1 

3 4 5 2 1 

3 4 5 1 2 

1 2 3 4 5 

After sorting:

1 2 3 4 5 

                        * www.2k8618.blogspot.com *

Press 1 to continue...
1

Enter the number of elements:5
Enter the elements:
5
4
3
2
1
                        * www.2k8618.blogspot.com *
Before sorting:

5 4 3 2 1 

Enter your choice:

    1.BUBBLESORT
        2.SELECTIONSORT
            3.INSERTIONSORT   
    4.QUICKSORT
        5.MERGESORT
            6.HEAPSORT        *www.2k8618.blogspot.com*
                    7.Exit>>>

6
Sorting.............
                        * www.2k8618.blogspot.com *
(HeapSort)

5 4 3 2 1 

1 4 3 2 5 

1 2 3 4 5 

1 2 3 4 5 

1 2 3 4 5 

After sorting:

1 2 3 4 5 

                        * www.2k8618.blogspot.com *

Press 1 to continue...
2


            ***     ***
            *****   ***
            *** *** ***
            ***   *****
            ***    ****


        <<<<SORTING ALGORITHMS>>>>
        ±±±±±±±±±±±±±±±±±±±±±±±±±±
             @ 2010 - www.2k8618.blogspot.com -
        2k8618@gmail.com,S4-CSE,GCEK

nn@linuxmint ~ $

Bubblesort - Assembly Language - MASM


BUBBLE SORT

Program:
.model small
.stack 200H
.data
  msg1 DB 10,13,'Enter the limit::$'
  msg2 DB 10,13,'Enter the numbers::$'
  msg3 DB 10,13,'The numbers in ascending order is::$'
  comma DB ',$'
 newline DB 10,13,'$'
 count db ?
 tab1 db 20 dup(0)
.code
  printstring MACRO msg
 PUSH AX
 PUSH DX
 MOV AH,09H
 MOV DX,OFFSET msg
  INT 21H
 POP DX
 POP AX
 ENDM
.startup
  printstring msg1
  CALL readnumtoAX
  MOV count,AL
  MOV CL,count
  MOV CH,00
  MOV BX,1
  printstring msg2
rdnxt:
  printstring newline
  CALL readnumtoAX
  MOV tab1[BX],AL
  INC BX
  LOOP rdnxt
  MOV CL,count
  MOV CH,00
  CMP CX,1
  JE DONE
  nextpass:
  MOV DL,00
  MOV BX,01
nextj:
  MOV AL,tab1[BX]
  MOV AH,tab1[BX + 1]
  CMP AL,AH
  JLE skip
  MOV tab1[BX],AH
  MOV tab1[BX + 1],AL
  MOV DL,1
skip:
  INC BX
  CMP BX,CX
  JL nextj
  DEC CX
  jz done
  CMP DL,01H
  JE nextpass
done:
  MOV CL,count
  MOV CH,00
  MOV BX,01
  printstring msg3
 prnnxt:
  MOV AX,00
  MOV AL,tab1[BX]
  CALL displayAX
  printstring comma
  INC BX
  LOOP prnnxt
  MOV AH,4CH
  MOV AL,00H
  INT 21H
readnumtoAX PROC NEAR
  PUSH BX
  PUSH CX
  MOV CX,10
  MOV BX,00
bac:
  MOV AH,01H
  INT 21H
  CMP AL,'0'
  JB skip1
  CMP AL,'9'
  JA skip1
  SUB AL,'0'
  PUSH AX
  MOV AX,BX
  MUL CX
  MOV BX,AX
  POP AX
  MOV AH,00
  ADD BX,AX
  JMP bac
skip1:
  MOV AX,BX
  POP CX
  POP BX
  RET
 readnumtoAX ENDP
displayAX PROC NEAR
  PUSH DX
  PUSH CX
  PUSH BX
  PUSH AX
  MOV CX,0
  MOV BX,10
 back1:
  MOV DX,0
  DIV BX
  PUSH DX
  INC CX
  OR AX,AX
  JNZ back1
back2:
  POP DX
  ADD DL,30H
  MOV AH,02H
  INT 21H
  LOOP back2
  POP AX
  POP BX
  POP CX
  POP DX
  RET
displayAX ENDP
END

Output:
F:\>sort
Enter the limit::7
Enter the numbers::
13
9
32
1
11
26
20
The numbers in ascending order is::1,9,11,13,20,26,32,

Heapsort - Sorting Algorithm - JAVA

Program:
import java.io.*;
class arraylist
{
    private int[] a;
    private int n;
    private int[] temp;
    public arraylist(int x)
    {
        a = new int[x];
        n=0;
        temp = new int[x];
    }
    public void insert(int e)
    {
        n++;
        a[n]=e;
    }
    public void display()
    {
        for(int i=1;i<=n;i++)
        System.out.print(a[i]+" ");
        System.out.println("\n");
    }
    public void swap(int x,int y)
    {
            int temp=a[x];
            a[x]=a[y];
            a[y]=temp;
    }
    public void buildheap()
    {
        for(int i=n/2;i>=1;i--)
        maxheapify(n,i);
    }
    public void maxheapify(int n,int i)
    {
        int largest;
        int l=left(i);
        int r=right(i);
        if(l<=n&&a[l]>a[i])
            largest=l;
        else
            largest=i;
        if(r<=n&&a[r]>a[largest])
            largest=r;
        if(largest!=i)
        {
            swap(i,largest);
            maxheapify(n,largest);
        }
    }
    public void heapsort()
    {
        buildheap();
        int heapsize=n;
        for(int i=heapsize;i>=2;i--)
        {
            swap(i,1);
            display();
            heapsize--;
            maxheapify(heapsize,1);
        }
    }
    public int left(int a)
    {
        return 2*a;
    }
    public int right(int b)
    {
        return 2*b+1;
    }
   
}

class heapsort
{
    public static void main(String arg[]) throws IOException
    {
        DataInputStream x1= new DataInputStream(System.in);
       
        System.out.print("\nEnter the number of elements:");
        int na=Integer.parseInt(x1.readLine());
        arraylist ob=new arraylist(na+1);
        System.out.println("Enter the elements:");
        for (int k=1;k<=na;k++)
                ob.insert(Integer.parseInt(x1.readLine()));
       
        System.out.println("Before sorting:\n");
        ob.display();
        System.out.println("Sorting.............");
        System.out.print("(HeapSort)\n\n");
            ob.display();ob.heapsort();
        System.out.println("After sorting:\n");
        ob.display();
    }
}

Output:
nn@linuxmint ~ $ javac heapsort.java
Note: heapsort.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java heapsort

Enter the number of elements:5
Enter the elements:
5
4
3
2
1
Before sorting:

5 4 3 2 1

Sorting.............
(HeapSort)

5 4 3 2 1

1 4 3 2 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

After sorting:

1 2 3 4 5

nn@linuxmint ~ $

Mergesort - Sorting Algorithm - JAVA

Program:

import java.io.*;
class arraylist
{
    private int[] a;
    private int n;
    private int[] temp;
    public arraylist(int x)
    {
        a = new int[x];
        n=0;
        temp = new int[x];
    }
    public void insert(int e)
    {
        n++;
        a[n]=e;
    }
    public void display()
    {
        for(int i=1;i<=n;i++)
        System.out.print(a[i]+" ");
        System.out.println("\n");
    }
    public void merge(int low,int high)
    {
        if(low!=high)
        {
            int mid=(low+high)/2;
            merge(low,mid);
            merge(mid+1,high);
            rmerge(low,mid,high);
        }
    }
    public void rmerge(int low,int mid,int high)
    {
       
        int i=low;int j=mid+1;int k=low;
       
        while((i<=mid)&&(j<=high))
        {
            if(a[i]<=a[j])
                {
                    temp[k]=a[i];k++;i++;
                }
            else
                {
                    temp[k]=a[j];k++;j++;
                }
        }
        while(i<=mid)
        {
            temp[k]=a[i];k++;i++;
        }
        while(j<=high)
        {
            temp[k]=a[j];k++;j++;
        }
        for(i=1;i<=high;i++)
            a[i]=temp[i];
            display();

    }
   

}

class mergesort
{
    public static void main(String arg[]) throws IOException
    {
        DataInputStream x1= new DataInputStream(System.in);
       
        System.out.print("\nEnter the number of elements:");
        int na=Integer.parseInt(x1.readLine());
        arraylist ob=new arraylist(na+1);
        System.out.println("Enter the elements:");
        for (int k=1;k<=na;k++)
                ob.insert(Integer.parseInt(x1.readLine()));
        System.out.println("Before sorting:\n");
        ob.display();
        System.out.println("Sorting.............");
        System.out.print("(MergeSort)\n\n");
        ob.display();
        ob.merge(1,na);
        System.out.println("After sorting:\n");
        ob.display();
    }
}

Output:
nn@linuxmint ~ $ javac mergesort.java
Note: mergesort.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java mergesort

Enter the number of elements:5
Enter the elements:
5
4
3
2
1
Before sorting:

5 4 3 2 1

Sorting.............
(MergeSort)

5 4 3 2 1

4 5 3 2 1

3 4 5 2 1

3 4 5 1 2

1 2 3 4 5

After sorting:

1 2 3 4 5

nn@linuxmint ~ $

Quicksort - Sorting Algorithm - JAVA

Program:

import java.io.*;
class arraylist
{
    private int[] a;
    private int n;
    private int[] temp;
    public arraylist(int x)
    {
        a = new int[x];
        n=0;
        temp = new int[x];
    }
    public void insert(int e)
    {
        n++;
        a[n]=e;
    }
    public void display()
    {
        for(int i=1;i<=n;i++)
        System.out.print(a[i]+" ");
        System.out.println("\n");
    }
    public void swap(int x,int y)
    {
            int temp=a[x];
            a[x]=a[y];
            a[y]=temp;
    }
    public void quick(int first,int last)
    {      
        if(last>first)
        {
            int i=first;int j=last+1;int pivot=a[first];
            do
            {
                do{
                    i=i+1;
                  }while((a[i]<=pivot)&&(i<last));
                do{
                    j=j-1;  
                  }while((a[j]>=pivot)&&(j>first));
                  
                if(i<j)
                {  
                  
                    swap(i,j);
                    display();
                }
            }while(i<j);
            swap(first,j);
            display();
            quick(first,j-1);
            quick(j+1,last);
        }
    }
  

}

class quicksort
{
    public static void main(String arg[]) throws IOException
    {
        DataInputStream x1= new DataInputStream(System.in);
      
        System.out.print("\nEnter the number of elements:");
        int na=Integer.parseInt(x1.readLine());
        arraylist ob=new arraylist(na+1);
        System.out.println("Enter the elements:");
        for (int k=1;k<=na;k++)
                ob.insert(Integer.parseInt(x1.readLine()));
      
        System.out.println("Before sorting:\n");
        ob.display();
        System.out.println("Sorting.............");
        System.out.print("(QuickSort)\n\n");
        ob.display();
        ob.quick(1,na);
        System.out.println("After sorting:\n");
        ob.display();
    }
}

Output:
nn@linuxmint ~ $ javac quicksort.java
Note: quicksort.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java quicksort

Enter the number of elements:5
Enter the elements:
5
4
3
2
1
Before sorting:

5 4 3 2 1

Sorting.............
(QuickSort)

5 4 3 2 1

1 4 3 2 5

1 4 3 2 5

1 2 3 4 5

1 2 3 4 5

After sorting:

1 2 3 4 5

nn@linuxmint ~ $

Insertionsort - Sorting Algorithm - JAVA

Program:
import java.io.*;
class arraylist
{
    private int[] a;
    private int n;
    private int[] temp;
    public arraylist(int x)
    {
        a = new int[x];
        n=0;
        temp = new int[x];
    }
    public void insert(int e)
    {
        n++;
        a[n]=e;
    }
    public void display()
    {
        for(int i=1;i<=n;i++)
        System.out.print(a[i]+" ");
        System.out.println("\n");
    }
    public void insertion()
    {
        System.out.print("(insertionsort)\n\n");
        display();
        for(int j=2;j<=n;j++)
        {
            int key=a[j];int i=j-1;
            while((i>0)&&(a[i]>key))
            {
                a[i+1]=a[i];
                i--;
            }
            a[i+1]=key;
        display();
        }
    }
   
}

class insertionsort
{
    public static void main(String arg[]) throws IOException
    {
        DataInputStream x1= new DataInputStream(System.in);
       
        System.out.print("\nEnter the number of elements:");
        int na=Integer.parseInt(x1.readLine());
        arraylist ob=new arraylist(na+1);
        System.out.println("Enter the elements:");
        for (int k=1;k<=na;k++)
                ob.insert(Integer.parseInt(x1.readLine()));
       
        System.out.println("Before sorting:\n");
        ob.display();
        System.out.println("Sorting.............");
       
        ob.insertion();
        System.out.println("After sorting:\n");
        ob.display();
    }
}

Output:
nn@linuxmint ~ $ javac insertionsort.java
Note: insertionsort.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java insertionsort

Enter the number of elements:5
Enter the elements:
5
4
3
2
1
Before sorting:

5 4 3 2 1

Sorting.............
(insertionsort)

5 4 3 2 1

4 5 3 2 1

3 4 5 2 1

2 3 4 5 1

1 2 3 4 5

After sorting:

1 2 3 4 5

nn@linuxmint ~ $

Selectionsort - Sorting Algorithm - JAVA

Program:
import java.io.*;
class arraylist
{
    private int[] a;
    private int n;
    private int[] temp;
    public arraylist(int x)
    {
        a = new int[x];
        n=0;
        temp = new int[x];
    }
    public void insert(int e)
    {
        n++;
        a[n]=e;
    }
    public void display()
    {
        for(int i=1;i<=n;i++)
        System.out.print(a[i]+" ");
        System.out.println("\n");
    }
   
    public void swap(int x,int y)
    {
            int temp=a[x];
            a[x]=a[y];
            a[y]=temp;
    }
    public void selection()
    {
        System.out.print("(selectionsort)\n\n");
        display();
        for(int i=1;i<n;i++)
        {
            int min=i;
            for(int j=i+1;j<=n;j++)
                if(a[j]<a[min])
                    min=j;
            swap(i,min);
            display();
        }
    }
   
}

class selection
{
    public static void main(String arg[]) throws IOException
    {
        DataInputStream x1= new DataInputStream(System.in);
        System.out.print("\nEnter the number of elements:");
        int na=Integer.parseInt(x1.readLine());
        arraylist ob=new arraylist(na+1);
        System.out.println("Enter the elements:");
        for (int k=1;k<=na;k++)
                ob.insert(Integer.parseInt(x1.readLine()));
        System.out.println("Before sorting:\n");
        ob.display();
        System.out.println("Sorting.............");
       
        ob.selection();
        System.out.println("After sorting:\n");
        ob.display();
    }
}

Output:
nn@linuxmint ~ $ javac selection.java
Note: selection.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java selection

Enter the number of elements:5
Enter the elements:
5
4
3
2
1
Before sorting:

5 4 3 2 1

Sorting.............
(selectionsort)

5 4 3 2 1

1 4 3 2 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

After sorting:

1 2 3 4 5

nn@linuxmint ~ $

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 ~ $
Related Posts Plugin for WordPress, Blogger...