Repository navigation
Expand file tree
/
Copy pathSortedArray.java
More file actions
69 lines (65 loc) · 1.69 KB
/
Copy pathSortedArray.java
File metadata and controls
69 lines (65 loc) · 1.69 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
67
68
69
// Program to return given array into ascending order and dsceding order
import java.util.Scanner;
public class SortedArray
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println(" Enter the size of the array:");
int n=scanner.nextInt();
int a[]=new int[n];
System.out.println(" Enter the elements of the array:");
for(int i=0; i<n; i++)
{
a[i]=scanner.nextInt();
}
int b[]=new int[n];
b=ascendingOrderArray(a);
System.out.println(" Elements in ascending order:");
for(int i=0; i<n; i++)
{
System.out.print(b[i] + " ");
}
b=descendingOrderArray(a);
System.out.println(" Elements in descending order:");
for(int i=0; i<n; i++)
{
System.out.print(b[i] + " ");
}
scanner.close();
}
public static int[] ascendingOrderArray(int a[])
{
int temp;
for(int i=0; i<a.length; i++)
{
for(int j=i+1; j<a.length; j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
return a;
}
public static int[] descendingOrderArray(int a[])
{
int temp;
for(int i=0; i<a.length; i++)
{
for(int j=i+1; j<a.length; j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
return a;
}
}