Question:
ICSE 2005 Question 7
Write a program to bubble sort the following set of values in ascending order:-
5,3,8,4,9,2,1,12,98,16
output:
1
2
3
4
5
8
9
12
16
98
Source Code:
public class ICSE2005_7
{
public static void main() throws Exception
{
int a[]={5,3,8,4,9,2,1,12,98,16};
int l=a.length;
for(int i=0; i<l; i++)
for(int j=0; j<(l-i-1); j++)
if(a[j]>a[j+1])
{
int c=a[j];
a[j]=a[j+1];
a[j+1]=c;
}
System.out.println("The sorted array is:");
for(int i=0; i<l; i++)
System.out.println(a[i]);
}
}
Result:
OUTPUT:
The sorted array is:
1
2
3
4
5
8
9
12
16
98
No comments:
Post a Comment