-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAscend_Descend_Order_Array.java
More file actions
66 lines (61 loc) · 2.12 KB
/
Ascend_Descend_Order_Array.java
File metadata and controls
66 lines (61 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.Scanner;
public class Ascend_Descend_Order_Array {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the lenght of array to be made:");
int length = input.nextInt();
int temp;
int arr[] = new int[length];
System.out.println("Enter the elements: ");
// Entering the elements in the array:
for (int i = 0; i < length; i++) arr[i] = input.nextInt();
// Printing the Original Array:
System.out.println("Original array:");
System.out.print("{");
for (int i = 0; i < length; i++){
System.out.print(arr[i]);
if (i < length-1) System.out.print(",");
else break;
}
System.out.println("}");
System.out.println();
// For arranging the elements in array in Ascending order:
for (int j = 0; j < length; j++){
for (int k= j+1; k < length; k++){
if (arr[j] > arr[k]){
temp= arr[j];
arr[j] = arr[k];
arr[k] = temp;
}
}
}
// Printing the elements:
System.out.println("Ascending Order: ");
System.out.print("{");
for (int i=0; i<arr.length; i++) {
System.out.print(arr[i]);
if (i < arr.length-1) System.out.print(",");
else break;
}
System.out.println("}");
System.out.println();
// For arranging the elements in array in Descending order:
for (int j = 0; j < length; j++){
for (int k= j+1; k < length; k++){
if (arr[j] < arr[k]){
temp= arr[k];
arr[k] = arr[j];
arr[j] = temp;
}
}
}
System.out.println("Descending Order: ");
System.out.print("{");
for (int i=0; i<arr.length; i++) {
System.out.print(arr[i]);
if (i < arr.length-1) System.out.print(",");
else break;
}
System.out.print("}");
}
}