-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathsemisort1.java
More file actions
64 lines (56 loc) · 1.98 KB
/
Copy pathsemisort1.java
File metadata and controls
64 lines (56 loc) · 1.98 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Write a program to sort half of the array in ascending and second half in descending or vice versa depending on users choice.
AD - Ascending then descending
DA - Descending then ascending
*/
import java.io.*;
import java.util.Arrays;
class semisort1
{
static void sort(int [] ar,int ln,String styl) //A method to sort array according to the style
{
int x;
int [] tmparr =ar.clone(); //A method to clone an array
if(ln%2==0)
{x=(ln/2)-1; }
else
{x=(ln/2);}
Arrays.sort(ar,0,(x+1)); //A method to sort array in ascending order from specified start index(inclusive)
Arrays.sort(tmparr,(x+1),(ln)); // to end index(exclusive) .
if(styl.equals("AD"))
{
int c=ln-1;
for(int i=(x+1);i<ln;i++)
{
ar[i]=tmparr[c];
c--;
}
System.out.println(Arrays.toString(ar));
}
else
{ int c=0;
for(int i=x;i>=0;i--)
{
tmparr[c]=ar[i];
c++;
}
System.out.println(Arrays.toString(tmparr));
}
}
public static void main(String[] args) throws IOException
{
InputStreamReader inp=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(inp);
System.out.print("Enter Sorting style :");
String styl=br.readLine(); //A string to store the style of sorting AD or DA
System.out.print("Enter the length of the array(greater than 1) and elements of the array :");
int len=Integer.parseInt(br.readLine()); //A string to store length if the array
System.out.println();
int []ar=new int[len];
for(int i=0;i<len;i++) //Storing the array elements
{
ar[i]=Integer.parseInt(br.readLine());
}
semisort1.sort(ar,len,styl);
}
}