forked from Mugdha-Hazra/PrepBytes-questions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSort Stack.c
50 lines (47 loc) · 940 Bytes
/
Sort Stack.c
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
#include <stdio.h>
#define ll long long
void sortStack(int input[],int size)
{
int tmpStack[size];
int top=-1;
while (size>=0)
{
int tmp = input[size];
size--;
// while temporary stack is not empty and top
// of stack is greater than temp
while (top>=0 && tmpStack[top] < tmp)
{
// pop from temporary stack and push
// it to the input stack
input[++size]=tmpStack[top];
top--;
}
// push temp in tempory of stack
tmpStack[++top]=(tmp);
}
while (top>=0)
{
printf("%d ",tmpStack[top]);
top--;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,x;
scanf("%d",&n);
int input[n];int top=-1;
for(int i=0;i<n;i++){
scanf("%d",&x);
input[++top]=x;
}
// This is the temporary stack
sortStack(input,top);
printf("\n");
}
return 0;
}