Guest User

Untitled

a guest
Jun 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. import java.io.*;
  2. class sort
  3. {
  4.     int x[];
  5.     public sort(int a[])
  6.     {
  7.         for(int i=0; i<a.length; i++)
  8.             x=a;
  9.     }
  10.     public void bubble()
  11.     {
  12.         int temp;
  13.         for(int i=0; i<x.length; i++)
  14.         {
  15.             for(int j=0; j<x.length-i-1; j++)
  16.                 if(x[j]>x[j+1])
  17.                 {
  18.                     temp=x[j];
  19.                     x[j]=x[j+1];
  20.                     x[j+1]=temp;
  21.                 }
  22.         }
  23.         for(int i=0; i<x.length; i++)
  24.             System.out.println(x[i]);
  25.     }
  26.     public void selection()
  27.     {
  28.         int pos, small;
  29.         for(int i=0; i<x.length; i++)
  30.         {
  31.             small=x[i]; pos=i;
  32.             for(int j=i+1; j<x.length; j++)
  33.             {
  34.                 if(x[j]<small)
  35.                 { small=x[j]; pos=j;}
  36.             }
  37.             int temp=x[i];
  38.             x[i]=x[pos];
  39.             x[pos]=temp;
  40.         }
  41.         display(x);
  42.     }
  43.     public void display(int a[])
  44.     {
  45.         for(int i=0; i<a.length; i++)
  46.             System.out.println(a[i]);
  47.     }
  48. }
  49.  
  50. class allsort
  51. {
  52.     public static void main(String args[]) throws IOException
  53.     {
  54.         System.out.println("Enter size of array: "); int size;
  55.         String in;
  56.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  57.         in = stdin.readLine();
  58.         size = Integer.parseInt(in);
  59.         int arr[] = new int[size];
  60.         System.out.println("Start entering elements..");
  61.         for (int i=0; i<size; i++)
  62.         {
  63.             in = stdin.readLine();
  64.             arr[i]=Integer.parseInt(in);
  65.         }
  66.         System.out.println("Your array: ");
  67.         for(int i=0; i<size; i++)
  68.             System.out.println(arr[i]);
  69.         sort obj=new sort(arr); //array passed to class.
  70.         System.out.println("Which sorting do you want to perform?\n1. Selection\n2. Bubble\n\tEnter choice(1-2): ");
  71.         in=stdin.readLine(); int choice=Integer.parseInt(in);
  72.         switch(choice)
  73.         {
  74.             case 1:System.out.println("Selection Sorted array: ");
  75.                    obj.selection();
  76.                    break;
  77.             case 2: System.out.println("Bubble Sorted array: ");
  78.                 obj.bubble();
  79.                 break;
  80.         }
  81.     }
  82. }
Add Comment
Please, Sign In to add comment