Tuesday, March 23, 2010

Bubble Sort

import java.io.*;

public class sortbubble
{ public static void main(String args[]) throws IOException
  { InputStreamReader read = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(read);
  int arr[] = new int[10];
  System.out.println("Enter 10 numbers");
  for(int i=0;i<10;i++)
  { arr[i]=Integer.parseInt(br.readLine());
  }
  System.out.println("They will now be sorted IN ASCENDING ORDER using bubb;e sort");
  int temp=0;
  for(int i=5;i<15;i++)
  { for(int j=0;j<9;j++)
  { if(arr[j]>arr[j+1])
  { temp=arr[j];
  arr[j]=arr[j+1];
  arr[j+1]=temp;
  }
  }
  }
  for(int i=0;i<10;i++)
  { System.out.println(arr[i]);
  }
   
  System.out.println("Now they will be sorted in DESCENDING ORDER");
  for(int i=0;i<10;i++)
  { for(int j=0;j<9;j++)
  { if(arr[j]  { temp=arr[j];
  arr[j]=arr[j+1];
  arr[j+1]=temp;
  }
  }
  }
  for(int i=0;i<10;i++)
  { System.out.println(arr[i]);
  }
  }
}
   
   
 

No comments:

Post a Comment