abhas Active member
| Subject: Bubble sorting program [java] Sat Jun 27, 2009 6:13 pm | |
| import java.io.*; class bubble { public static void main()throws IOException {
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a number of elements"); int n=Integer.parseInt(obj.readLine()); int arr[]=new int[n]; for(int a=0;a<n;a++) { System.out.println("Enter a number"); arr[a]=Integer.parseInt(obj.readLine()); }
for(int i=0;i<n;i++) { for(int j=0;j<n-1;j++) { if(arr[j]<arr[j+1]) { arr[j]=arr[j]+arr[j+1]; arr[j+1]=arr[j]-arr[j+1]; arr[j]=arr[j]-arr[j+1]; } } } for(int i=0;i<n;i++) { System.out.println(arr[i]); }
} } |
|