The ascending order was displaying the same as descending order. how do i get the ascending order to be different from descending order?
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n =10;
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++){
a[i] = in.nextInt();
}
for (int i = 0; i < n; i++){
for (int j = i + 1; j < n; j++){
if (a[i] < a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("Descending Order:");
for (int i = 0; i < n; i++){
System.out.print(a[i]+",");
}
for (int i = 0; i < n; i++){
for (int j = i + 1; j < n; j++){
if (a[i] > a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("\nAscending Order:");
for (int i = 0; i < n; i++){
System.out.print(a[i]+",");
}
}
2 answers
Which one is wrong and which one is right? This will help us focus on only the relevant code.
the ascending order is the same as descending order.