Skip to content

Commit 3dcb7b6

Browse files
Create 22.1 Binary Short Method.java
1 parent c1315c1 commit 3dcb7b6

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

22.1 Binary Short Method.java

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Consider that in a plantation activity, the municipality is planting trees in an order such that the smallest tree is planted first and the biggest at the end. While arranging the trees, before planting, to position them at their positions, they are swapped in such a way that a tree can only be swapped with its adjacent trees. wAP to implemennt the same using method which contain the logic of sorting. Consider 10 trees height as input.
3+
4+
Input Format
5+
6+
72 71 70 69 68 65 78 88 61 63
7+
8+
Constraints
9+
10+
Consider that there are 10 are present in the activity and their lenghts are in integer numbers (between 60 inches to 72 inches).
11+
12+
Output Format
13+
14+
61 63 65 68 69 70 71 72 78 88
15+
16+
Sample Input 0
17+
18+
71 71 70 69 68 65 78 88 61 63
19+
Sample Output 0
20+
21+
61 63 65 68 69 70 71 71 78 88
22+
*/
23+
24+
import java.io.*;
25+
import java.util.*;
26+
import java.text.*;
27+
import java.math.*;
28+
import java.util.regex.*;
29+
30+
31+
public class Solution {
32+
static int[] binaryShort(int []array,int n)
33+
{
34+
35+
for(int i=0;i<n-1;i++)
36+
{
37+
for(int j=0;j<n-1-i;j++)
38+
{
39+
if(array[j] > array[j+1])
40+
{
41+
int temp = array[j];
42+
array[j] = array[j+1];
43+
array[j+1] = temp;
44+
}
45+
}
46+
47+
}
48+
return array;
49+
}
50+
public static void main(String args[] ) throws Exception {
51+
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
52+
Scanner sc = new Scanner(System.in);
53+
int []tree = new int [10];
54+
for(int i=0;i<10;i++)
55+
tree[i] = sc.nextInt();
56+
tree = binaryShort(tree,10);
57+
for(int i:tree)
58+
{
59+
System.out.print(i+" ");
60+
}
61+
62+
}
63+
}

0 commit comments

Comments
 (0)