Write a Program in java to sort a list of items in ascending order.
class sortList{
public static void main(String[] args) {
int A[]={1,2,6,4,8,7,9};
System.out.println("Original List : ");
printArray(A);
System.out.println();
assendingOrder(A);
System.out.println("List in ascending order : ");
printArray(A);
}
public static void printArray(int A[]){
for(int i=0;i<A.length;i++){
System.out.print(A[i]+"\t");
}
}
public static void assendingOrder(int A[]){
int a;
for(int i=0;i<A.length;i++){
for(int j=i;j<A.length;j++){
if(A[i]>A[j]){
a=A[i];
A[i]=A[j];
A[j]=a;
}
}
}
}
}
OUTPUT:
Original List :
1 2 6 4 8 7 9
List in ascending order :
1 2 4 6 7 8 9
Comments
Post a Comment
Please do not enter any spam link in the comment box.