File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+ Write a program to sort half of the array in ascending and second half in descending or vice versa depending on users choice.
3+ AD - Ascending then descending
4+ DA - Descending then ascending
5+ */
6+ import java .io .*;
7+ import java .util .Arrays ;
8+ class semisort1
9+ {
10+ static void sort (int [] ar ,int ln ,String styl ) //A method to sort array according to the style
11+ {
12+ int x ;
13+ int [] tmparr =ar .clone (); //A method to clone an array
14+
15+ x =(ln /2 );
16+ Arrays .sort (ar ,0 ,(x +1 )); //A method to sort array in ascending order from specified start index(inclusive)
17+ Arrays .sort (tmparr ,(x +1 ),(ln )); // to end index(exclusive) .
18+ if (styl .equals ("AD" ))
19+ {
20+ int c =ln -1 ;
21+ for (int i =(x +1 );i <ln ;i ++)
22+ {
23+ ar [i ]=tmparr [c ];
24+ c --;
25+ }
26+ System .out .println (Arrays .toString (ar ));
27+
28+ }
29+
30+ else
31+ { int c =0 ;
32+ for (int i =x ;i >=0 ;i --)
33+ {
34+ tmparr [c ]=ar [i ];
35+ c ++;
36+ }
37+ System .out .println (Arrays .toString (tmparr ));
38+ }
39+ }
40+ public static void main (String [] args ) throws IOException
41+ {
42+
43+ InputStreamReader inp =new InputStreamReader (System .in );
44+ BufferedReader br =new BufferedReader (inp );
45+ System .out .print ("Enter Sorting style :" );
46+
47+ String styl =br .readLine (); //A string to store the style of sorting AD or DA
48+
49+ System .out .print ("Enter the length of the array(greater than 1) and elements of the array :" );
50+ int len =Integer .parseInt (br .readLine ()); //A string to store length if the array
51+
52+ System .out .println ();
53+
54+ int []ar =new int [len ];
55+ for (int i =0 ;i <len ;i ++) //Storing the array elements
56+ {
57+ ar [i ]=Integer .parseInt (br .readLine ());
58+ }
59+ semisort1 obj = new semisort1 (); //Program by LN11211
60+ obj .sort (ar ,len ,styl );
61+ }
62+ }
You can’t perform that action at this time.
0 commit comments