Skip to content

Commit c8e589d

Browse files
committed
semisort1 problem
Code for semisort1 problem.
1 parent 4d34882 commit c8e589d

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

Sorting/semisort1.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

0 commit comments

Comments
 (0)