-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortinngusingrecursion.java
More file actions
54 lines (45 loc) · 1.03 KB
/
sortinngusingrecursion.java
File metadata and controls
54 lines (45 loc) · 1.03 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
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
static Stack<Integer> st = new Stack<Integer>();
public static void sort(Stack<Integer> stack,int x)
{
if(stack.empty())
{
stack.push(x);
return;
}
if(x<stack.peek())
{
int a = stack.pop();
sort(stack,x);
stack.push(a);
}
else
stack.push(x);
}
public static void recursive(Stack<Integer> stack)
{
if(stack.empty() == false)
{
int temp = stack.pop();
recursive(stack);
sort(stack,temp);
}
}
public static void main (String[] args) throws java.lang.Exception
{
st.push(8);
st.push(5);
st.push(2);
st.push(6);
st.push(2);
st.push(1);
recursive(st);
System.out.println(st+"");
}
}