-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackSortRec.java
More file actions
39 lines (35 loc) · 765 Bytes
/
Copy pathStackSortRec.java
File metadata and controls
39 lines (35 loc) · 765 Bytes
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
import java.util.Stack;
public class StackSortRec{
public static void main(String a[]){
Stack<Integer> numList = new Stack<Integer>();
numList.push(10);
numList.push(5);
numList.push(6);
numList.push(2);
numList.push(1);
numList.push(3);
System.out.println(numList);
sort(numList);
System.out.println(numList);
}
public static void sort(Stack<Integer> numList) {
if(numList.size()==0) {
return;
}else {
int temp = numList.pop();
sort(numList);
insert(numList,temp);
}
}
public static void insert(Stack<Integer> numList, int temp) {
if(numList.size()==0||numList.peek()<=temp) {
numList.push(temp);
return;
}else {
int val = numList.pop();
insert(numList,temp);
numList.add(val);
return;
}
}
}