Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
progseq-stack_sort
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
dario.genga
progseq-stack_sort
Commits
a0dc7c37
Commit
a0dc7c37
authored
3 years ago
by
dario.genga
Browse files
Options
Downloads
Patches
Plain Diff
Add array sort by two stacks
The program can now sort an array using the two stacks methods.
parent
f6fc779c
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
stack.c
+63
-2
63 additions, 2 deletions
stack.c
stack.h
+1
-0
1 addition, 0 deletions
stack.h
test.c
+18
-0
18 additions, 0 deletions
test.c
with
82 additions
and
2 deletions
stack.c
+
63
−
2
View file @
a0dc7c37
...
@@ -22,7 +22,6 @@ bool stack_is_empty(stack s) {
...
@@ -22,7 +22,6 @@ bool stack_is_empty(stack s) {
return
is_empty
;
return
is_empty
;
}
}
void
stack_push
(
stack
*
s
,
int
val
)
{
void
stack_push
(
stack
*
s
,
int
val
)
{
int
next_top
=
s
->
top
+
1
;
int
next_top
=
s
->
top
+
1
;
...
@@ -38,7 +37,6 @@ void stack_pop(stack *s, int *val) {
...
@@ -38,7 +37,6 @@ void stack_pop(stack *s, int *val) {
*
val
=
s
->
data
[
s
->
top
];
*
val
=
s
->
data
[
s
->
top
];
s
->
data
[
s
->
top
]
=
-
1
;
s
->
data
[
s
->
top
]
=
-
1
;
s
->
top
-=
1
;
s
->
top
-=
1
;
s
->
size
-=
1
;
}
}
}
}
...
@@ -54,3 +52,66 @@ void stack_destroy(stack *s) {
...
@@ -54,3 +52,66 @@ void stack_destroy(stack *s) {
s
->
top
=
-
1
;
s
->
top
=
-
1
;
s
->
size
=
-
1
;
s
->
size
=
-
1
;
}
}
int
*
sort
(
int
unsorted_array
[],
int
array_size
)
{
int
*
sorted_array
=
malloc
(
array_size
*
sizeof
(
int
));
int
*
left_peak
=
malloc
(
sizeof
(
int
));
int
*
right_peak
=
malloc
(
sizeof
(
int
));
stack
left_stack
,
right_stack
;
stack_init
(
&
left_stack
,
array_size
);
stack_init
(
&
right_stack
,
array_size
);
for
(
int
i
=
0
;
i
<
array_size
;
i
++
)
{
int
value
=
unsorted_array
[
i
];
// First stack push
if
(
stack_is_empty
(
left_stack
))
{
stack_push
(
&
left_stack
,
value
);
}
// Add to left stack if the value is lower than the left peek and higher than the right peak
else
{
stack_peek
(
left_stack
,
left_peak
);
if
(
*
left_peak
>
value
&&
(
right_stack
.
top
<
0
||
*
right_peak
<
value
))
{
stack_push
(
&
left_stack
,
value
);
}
// Else arrange the stacks
else
{
while
(
left_stack
.
top
>=
0
&&
*
left_peak
<
value
)
{
stack_pop
(
&
left_stack
,
left_peak
);
stack_push
(
&
right_stack
,
*
left_peak
);
stack_peek
(
left_stack
,
left_peak
);
stack_peek
(
right_stack
,
right_peak
);
}
while
(
right_stack
.
top
>=
0
&&
*
right_peak
>
value
)
{
stack_pop
(
&
right_stack
,
right_peak
);
stack_push
(
&
left_stack
,
*
right_peak
);
stack_peek
(
right_stack
,
right_peak
);
}
// Then push the value to the left stack
stack_push
(
&
left_stack
,
value
);
}
}
}
// Push to right stack to the left stack
while
(
right_stack
.
top
>=
0
)
{
stack_peek
(
right_stack
,
right_peak
);
stack_pop
(
&
right_stack
,
right_peak
);
stack_push
(
&
left_stack
,
*
right_peak
);
}
// Add the stack to the array
for
(
int
i
=
0
;
i
<
array_size
;
i
++
)
{
stack_pop
(
&
left_stack
,
left_peak
);
sorted_array
[
i
]
=
*
left_peak
;
}
// Free the memory
free
(
left_peak
);
free
(
right_peak
);
stack_destroy
(
&
left_stack
);
stack_destroy
(
&
right_stack
);
return
sorted_array
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
stack.h
+
1
−
0
View file @
a0dc7c37
...
@@ -20,5 +20,6 @@ void stack_push(stack *s, int val);
...
@@ -20,5 +20,6 @@ void stack_push(stack *s, int val);
void
stack_pop
(
stack
*
s
,
int
*
val
);
void
stack_pop
(
stack
*
s
,
int
*
val
);
void
stack_peek
(
stack
s
,
int
*
val
);
void
stack_peek
(
stack
s
,
int
*
val
);
void
stack_destroy
(
stack
*
s
);
void
stack_destroy
(
stack
*
s
);
int
*
sort
(
int
unsorted_array
[],
int
array_size
);
#endif
#endif
\ No newline at end of file
This diff is collapsed.
Click to expand it.
test.c
+
18
−
0
View file @
a0dc7c37
...
@@ -6,6 +6,7 @@
...
@@ -6,6 +6,7 @@
#include
<assert.h>
#include
<assert.h>
#include
<stdbool.h>
#include
<stdbool.h>
#include
<stdio.h>
#include
<stdio.h>
#define ARRAY_SIZE 5
void
test_stack_init
(
stack
*
s
,
int
size
)
{
void
test_stack_init
(
stack
*
s
,
int
size
)
{
printf
(
"Testing stack_init()...
\n
"
);
printf
(
"Testing stack_init()...
\n
"
);
...
@@ -56,6 +57,7 @@ void test_stack_push(stack *s, int val) {
...
@@ -56,6 +57,7 @@ void test_stack_push(stack *s, int val) {
stack_push
(
&
stack_full
,
val
);
stack_push
(
&
stack_full
,
val
);
assert
(
stack_full
.
data
[
stack_full
.
top
]
==
value_top_full_stack_value
);
assert
(
stack_full
.
data
[
stack_full
.
top
]
==
value_top_full_stack_value
);
printf
(
"OK.
\n
"
);
printf
(
"OK.
\n
"
);
stack_destroy
(
&
stack_full
);
}
}
void
test_stack_pop
(
stack
*
s
,
int
*
val
)
{
void
test_stack_pop
(
stack
*
s
,
int
*
val
)
{
...
@@ -82,10 +84,25 @@ void test_stack_pop(stack *s, int *val) {
...
@@ -82,10 +84,25 @@ void test_stack_pop(stack *s, int *val) {
printf
(
"OK.
\n
"
);
printf
(
"OK.
\n
"
);
}
}
void
test_sort
(
int
unsorted_array
[],
int
expected_array
[],
int
array_size
)
{
printf
(
"Testing sort()...
\n
"
);
int
*
sorted_array
=
sort
(
unsorted_array
,
array_size
);
for
(
int
i
=
0
;
i
<
array_size
;
i
++
)
{
assert
(
sorted_array
[
i
]
==
expected_array
[
i
]);
}
printf
(
"OK.
\n
"
);
free
(
sorted_array
);
}
int
main
()
{
int
main
()
{
stack
s
,
non_empty_stack
;
stack
s
,
non_empty_stack
;
int
size
=
10
,
value_to_add
=
7
;
int
size
=
10
,
value_to_add
=
7
;
int
val
;
int
val
;
int
unsorted_array
[
ARRAY_SIZE
]
=
{
0
,
-
1
,
2
,
7
,
4
};
int
expected_array
[
ARRAY_SIZE
]
=
{
-
1
,
0
,
2
,
4
,
7
};
printf
(
"Starting the tests...
\n
"
);
printf
(
"Starting the tests...
\n
"
);
// Test init and destroy
// Test init and destroy
test_stack_init
(
&
s
,
size
);
test_stack_init
(
&
s
,
size
);
...
@@ -103,6 +120,7 @@ int main() {
...
@@ -103,6 +120,7 @@ int main() {
test_stack_peek
(
s
,
&
val
);
test_stack_peek
(
s
,
&
val
);
test_stack_push
(
&
s
,
value_to_add
);
test_stack_push
(
&
s
,
value_to_add
);
test_stack_pop
(
&
s
,
&
val
);
test_stack_pop
(
&
s
,
&
val
);
test_sort
(
unsorted_array
,
expected_array
,
ARRAY_SIZE
);
// Free the memory
// Free the memory
stack_destroy
(
&
s
);
stack_destroy
(
&
s
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment