//SORT THE LIST IN DESCENDING ORDER USING BUBBLE SORT
//Developed by Ahlam Ansari
class Sort
{
public static void main(String ahlam[ ])
{
int a[ ]={7,3,9,1,2,43,23,10};
int i,j,l,temp;
l=a.length;
System.out.println("Elements in the unsorted array are: ");
for(i=0;i<l;i++)
{
System.out.println(a[i]+"\t"); //PRINTING UNSORTED ELEMENTS
}
for(i=0;i<l;i++) //SORTING THE ELEMENTS IN ARRAY
{
for(j=0;j<l-i-1;j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("Sorted array elements are: ");
for(i=0;i<l;i++)
{
System.out.println(a[i]+"\t"); ////PRINTING SORTED ELEMENTS
}
}
}
/*
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac Sort.java
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java Sort
Elements in the unsorted array are:
7
3
9
1
2
43
23
10
Sorted array elements are:
43
23
10
9
7
3
2
1
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>
*/
//Developed by Ahlam Ansari
class Sort
{
public static void main(String ahlam[ ])
{
int a[ ]={7,3,9,1,2,43,23,10};
int i,j,l,temp;
l=a.length;
System.out.println("Elements in the unsorted array are: ");
for(i=0;i<l;i++)
{
System.out.println(a[i]+"\t"); //PRINTING UNSORTED ELEMENTS
}
for(i=0;i<l;i++) //SORTING THE ELEMENTS IN ARRAY
{
for(j=0;j<l-i-1;j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("Sorted array elements are: ");
for(i=0;i<l;i++)
{
System.out.println(a[i]+"\t"); ////PRINTING SORTED ELEMENTS
}
}
}
/*
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac Sort.java
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java Sort
Elements in the unsorted array are:
7
3
9
1
2
43
23
10
Sorted array elements are:
43
23
10
9
7
3
2
1
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>
*/
No comments:
Post a Comment