-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlternative_sorting.java
More file actions
39 lines (29 loc) · 930 Bytes
/
Alternative_sorting.java
File metadata and controls
39 lines (29 loc) · 930 Bytes
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
/**
* @author SAGAR
*
*/
import java.util.Arrays;
import java.util.Scanner;
public class Alternative_sorting {
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println("Hello world");
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of the array");
int n = sc.nextInt();
int array[] = new int[n];
for (int i = 0; i < n; i++) {
array[i] = sc.nextInt();
}
int i = 0, j = n - 1; //setting i to lower limit and j to upper limit
Arrays.sort(array); //sorting the given array
while (i < j) {
System.out.print(array[j--] + " "); //printing the maximum elements one by one
System.out.print(array[i++] + " "); //printing the minimum elements one by one
}
if (n % 2 != 0) {
System.out.print(array[i] + " ");
}
sc.close();
}
}