Skip to content

Commit f581659

Browse files
Merge pull request #63 from Ln11211/semisort
semisort1 problem
2 parents 96fb566 + ffe3104 commit f581659

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

Sorting/semisort1.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
if(ln%2==0)
15+
{x=(ln/2)-1; }
16+
else
17+
{x=(ln/2);}
18+
19+
Arrays.sort(ar,0,(x+1)); //A method to sort array in ascending order from specified start index(inclusive)
20+
Arrays.sort(tmparr,(x+1),(ln)); // to end index(exclusive) .
21+
if(styl.equals("AD"))
22+
{
23+
int c=ln-1;
24+
for(int i=(x+1);i<ln;i++)
25+
{
26+
ar[i]=tmparr[c];
27+
c--;
28+
}
29+
System.out.println(Arrays.toString(ar));
30+
31+
}
32+
33+
else
34+
{ int c=0;
35+
for(int i=x;i>=0;i--)
36+
{
37+
tmparr[c]=ar[i];
38+
c++;
39+
}
40+
System.out.println(Arrays.toString(tmparr));
41+
}
42+
}
43+
public static void main(String[] args) throws IOException
44+
{
45+
46+
InputStreamReader inp=new InputStreamReader(System.in);
47+
BufferedReader br=new BufferedReader(inp);
48+
System.out.print("Enter Sorting style :");
49+
50+
String styl=br.readLine(); //A string to store the style of sorting AD or DA
51+
52+
System.out.print("Enter the length of the array(greater than 1) and elements of the array :");
53+
int len=Integer.parseInt(br.readLine()); //A string to store length if the array
54+
55+
System.out.println();
56+
57+
int []ar=new int[len];
58+
for(int i=0;i<len;i++) //Storing the array elements
59+
{
60+
ar[i]=Integer.parseInt(br.readLine());
61+
}
62+
semisort1 obj= new semisort1(); //Program by LN11211
63+
obj.sort(ar,len,styl);
64+
}
65+
}

0 commit comments

Comments
 (0)