-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_79.cpp
More file actions
46 lines (39 loc) · 739 Bytes
/
Day_79.cpp
File metadata and controls
46 lines (39 loc) · 739 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
40
41
42
43
44
45
46
/*
DAY 79: Sort a stack.
https://www.geeksforgeeks.org/sort-a-stack-using-recursion/
QUESTION : Given a stack, the task is to sort it such that the top of the stack has the greatest element.
Example 1:
Input:
Stack: 3 2 1
Output: 3 2 1
Example 2:
Input:
Stack: 11 2 32 3 41
Output: 41 32 11 3 2
Expected Time Complexity : O(N*N)
Expected Auixilliary Space : O(N) recursive
Constraints:
1<=N<=100
*/
void sortedInsert(stack<int> &s, int x)
{
if(s.empty() or x>s.top())
{
s.push(x);
return;
}
int temp = s.top();
s.pop();
sortedInsert(s,x);
s.push(temp);
}
void SortedStack :: sort()
{
if(!s.empty())
{
int x = s.top();
s.pop();
sort();
sortedInsert(s,x);
}
}