Get Answers to all your Questions

header-bg qa

Programs of bubble sorting with buffered reader

Answers (1)

best_answer
import java.io.*;

class sort

      String str;
      int size,sortArr[];
      
      publicvoid getdata()
      
             System.out.print("Enter how many data you want to enter : ");
             System.out.flush();
             try
                 BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
                 str=obj.readLine();
                 size=Integer.parseInt(str);
                 sortArr=newint[size];
                 for(int i=0;i<size;i++)
                   
                       System.out.print("Enter element at "+(i+1)+"th position  :  ");
                       System.out.flush();
                       str=obj.readLine();
                       sortArr[i]=Integer.parseInt(str);
                   
                
            catch(Exception e)  
      
                 
      publicvoid  BubSort()
      
            System.out.println("=====BUBBLE SORT=====
");
            getdata();
                for(int i=0;i<size;i++)
                    
                  for(int j=0;j<size-1;j++)
                      
                         if(sortArr[j] > sortArr[j+1])
                          
                               int temp=sortArr[j];
                               sortArr[j]=sortArr[j+1];
                               sortArr[j+1]=temp;
                          
                     
                  
            display();
            

    publicvoid display()
    
         System.out.println("
After Sorting");
         for(int i=0;i<size;i++)
              System.out.println(sortArr[i]);
    



class BubSort 

    publicstaticvoid main(String args[]) 
    
          sort ob1=new sort();
          ob1.BubSort();
    
Posted by

Deependra Verma

View full answer