4 A. Design a class SortData that contains the method asec and desc
Program :
import java.util.Scanner;
public class ArraySortingExample {
public static void main(String[] args) {
Scanner ed = new Scanner(System.in);
int[] a = new int[5];
int i, j, temp;
System.out.println("Please Enter 5 elements in the Array");
for (i = 0; i < 5; i++) {
a[i] = ed.nextInt();
}
for (i = 0; i < 5; i++) {
for (j = i + 1; j < 5; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("Sorted Array in Increasing Order:-");
for (j = 0; j < 5; j++) {
System.out.println(a[j]);
}
for (i = 0; i < 5; i++) {
for (j = i + 1; j < 5; j++) {
if (a[i] < a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("Sorted Array in Decreasing Order:-");
for (j = 0; j < 5; j++) {
System.out.println(a[j]);
}
}
}
Comments
Post a Comment