File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11{
22 "java.project.sourcePaths" : [
3+ " Number Theory" ,
34 " Basic Codes" ,
4- " Number Theory "
5+ " Sorting "
56 ]
67}
Original file line number Diff line number Diff line change 1+ //Write a program to sort the array accordingly
2+ import java .util .Scanner ;
3+ class Array_semi_sort {
4+ public static void main (String args []){
5+ Scanner sc =new Scanner (System .in );
6+ System .out .println ("Enter length of array : " );
7+ int l =sc .nextInt ();
8+ int ar []=new int [l ];
9+ for (int x =0 ;x <l ;x ++){
10+ System .out .println ("Enter number at index " +x );
11+ ar [x ]=sc .nextInt ();
12+ }
13+
14+ int odd []=new int [l /2 ];
15+ int even []=new int [l -(l /2 )];
16+ int i =0 ;
17+ int j =0 ;
18+
19+ for (int x =0 ;x <l ;x ++){
20+ if (x %2 ==0 ){
21+ even [i ++]=ar [x ];
22+ }
23+ else {
24+ odd [j ++]=ar [x ];
25+ }
26+ }
27+ even =ascending (even );
28+ odd =reverse (odd );
29+ odd =ascending (odd );
30+ odd =reverse (odd );
31+ i =0 ;j =0 ;
32+
33+ for (int x =0 ;x <l ;x ++){
34+ if (x %2 ==0 ){
35+ ar [x ]=even [i ++];
36+ }
37+ else {
38+ ar [x ]=odd [j ++];
39+ }
40+ }
41+
42+ for (int x =0 ;x <ar .length ;x ++){
43+ System .out .print (ar [x ]+" " );
44+ }
45+
46+ }
47+ public static int [] ascending (int ar []){
48+ int l =ar .length ;
49+ for (int x =0 ;x <l ;x ++){
50+ for (int y =x ;y <l -1 ;y ++){
51+ if (ar [y ]>ar [y +1 ]){
52+ int t =ar [y ];
53+ ar [y ]=ar [y +1 ];
54+ ar [y +1 ]=t ;
55+ }
56+ }
57+ }
58+ return ar ;
59+ }
60+
61+ public static int [] reverse (int ar []){
62+ int l =ar .length ;
63+ int r []=new int [l ];
64+ for (int x =0 ;x <l ;x ++){
65+ r [l -1 -x ]=ar [x ];
66+ }
67+ return r ;
68+ }
69+
70+ }
You can’t perform that action at this time.
0 commit comments