-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_sort.c
More file actions
94 lines (77 loc) · 1.26 KB
/
stack_sort.c
File metadata and controls
94 lines (77 loc) · 1.26 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#define SIZE 100
void push(int *S, int top, int item)
{
if(top == SIZE-1)
{
printf("\nStack overflow.");
return;
}
S[++top] = item;
}
void pop(int *S, int top)
{
if(top == 0)
{
printf("\nStack underflow.");
return;
}
--top;
}
int peek(int *S, int top)
{
if(top == 0)
{
printf("\nStack underflow.");
return -1;
}
return S[top];
}
void show(int *S, int top)
{
int i;
printf("\n");
for(i=0; i<=top; i++)
printf("%d ", S[i]);
printf("\n");
}
void compare(int *, int );
void sort(int *, int , int);
void sort(int *S, int cur, int TOP)
{
if(cur == 0)
return;
else
{
compare(S, TOP);
return sort(S, cur-1, TOP);
}
}
void compare(int *S, int top)
{
if(top == 0)
return;
else
{
if(S[top] < S[top-1])
{
int temp = S[top];
S[top] = S[top-1];
S[top-1] = temp;
}
return compare(S, top-1);
}
}
int main()
{
int top = -1;
int S[SIZE];
int i;
for(i= 8; i>=1; i--)
{
S[++top] = i;
}
show(S, top);
sort(S, top, top);
show(S, top);
}