Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
alg-1-lab-assignments
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
Show more breadcrumbs
20041679 .
alg-1-lab-assignments
Commits
23cc6f4c
Commit
23cc6f4c
authored
1 year ago
by
20041679
Browse files
Options
Downloads
Patches
Plain Diff
merge sort
parent
f082c847
No related branches found
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
alglib/src/sort.c
+79
-4
79 additions, 4 deletions
alglib/src/sort.c
alglib/src/sort_private.c
+0
-60
0 additions, 60 deletions
alglib/src/sort_private.c
alglib/src/sort_private.h
+16
-2
16 additions, 2 deletions
alglib/src/sort_private.h
with
95 additions
and
66 deletions
alglib/src/sort.c
+
79
−
4
View file @
23cc6f4c
...
...
@@ -25,6 +25,7 @@
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<upo/utility.h>
void
upo_insertion_sort
(
void
*
base
,
size_t
n
,
size_t
size
,
upo_sort_comparator_t
cmp
)
{
...
...
@@ -39,6 +40,22 @@ void upo_insertion_sort(void *base, size_t n, size_t size, upo_sort_comparator_t
free
(
last_copy
);
}
static
void
*
upo_insertion_sort_insert_pos
(
const
void
*
base
,
const
void
*
elem
,
size_t
n
,
size_t
size
,
upo_sort_comparator_t
cmp
)
{
for
(
size_t
i
=
0
;
i
<
n
;
++
i
)
{
if
(
cmp
((
unsigned
char
*
)
base
+
i
*
size
,
elem
)
>=
0
)
{
return
(
unsigned
char
*
)
base
+
i
*
size
;
}
}
return
NULL
;
}
void
upo_merge_sort
(
void
*
base
,
size_t
n
,
size_t
size
,
upo_sort_comparator_t
cmp
)
{
if
(
n
>
1
)
{
...
...
@@ -49,10 +66,68 @@ void upo_merge_sort(void *base, size_t n, size_t size, upo_sort_comparator_t cmp
}
}
static
void
upo_merge_sort_merge
(
void
*
base1
,
size_t
n1
,
void
*
base2
,
size_t
n2
,
size_t
size
,
upo_sort_comparator_t
cmp
)
{
unsigned
char
*
tmp
=
malloc
((
n1
+
n2
)
*
size
);
size_t
i1
=
0
;
size_t
i2
=
0
;
while
(
i1
<
n1
&&
i2
<
n2
)
{
unsigned
char
*
curr1
=
(
unsigned
char
*
)
base1
+
i1
*
size
;
unsigned
char
*
curr2
=
(
unsigned
char
*
)
base2
+
i2
*
size
;
unsigned
char
*
currtmp
=
tmp
+
(
i1
+
i2
)
*
size
;
if
(
cmp
(
curr1
,
curr2
)
<
0
)
{
memcpy
(
currtmp
,
curr1
,
size
);
++
i1
;
}
else
{
memcpy
(
currtmp
,
curr2
,
size
);
++
i2
;
}
}
while
(
i1
<
n1
)
{
unsigned
char
*
curr
=
(
unsigned
char
*
)
base1
+
i1
*
size
;
unsigned
char
*
currtmp
=
tmp
+
(
i1
+
i2
)
*
size
;
memcpy
(
currtmp
,
curr
,
size
);
++
i1
;
}
while
(
i2
<
n2
)
{
unsigned
char
*
curr
=
(
unsigned
char
*
)
base2
+
i2
*
size
;
unsigned
char
*
currtmp
=
tmp
+
(
i1
+
i2
)
*
size
;
memcpy
(
currtmp
,
curr
,
size
);
++
i2
;
}
memcpy
(
base1
,
tmp
,
n1
*
size
);
memcpy
(
base2
,
tmp
+
n1
*
size
,
n2
*
size
);
free
(
tmp
);
}
void
upo_quick_sort
(
void
*
base
,
size_t
n
,
size_t
size
,
upo_sort_comparator_t
cmp
)
{
/* TO STUDENTS:
* Remove the following two lines and put here your implementation. */
fprintf
(
stderr
,
"To be implemented!
\n
"
);
abort
();
if
(
n
>
1
)
{
unsigned
char
*
p
=
upo_quick_sort_partition
(
base
,
(
unsigned
char
*
)
base
+
n
*
size
,
size
,
cmp
);
upo_quick_sort
(
base
,
p
,
upo_quick_sort
(
p
+
size
,
(
unsigned
char
*
)
base
+
n
*
size
-
p
,
size
,
cmp
);
}
}
// using hoare's scheme
static
void
*
upo_quick_sort_partition
(
void
*
lo
,
void
*
hi
,
size_t
size
,
upo_sort_comparator_t
cmp
)
{
}
This diff is collapsed.
Click to expand it.
alglib/src/sort_private.c
deleted
100644 → 0
+
0
−
60
View file @
f082c847
#include
<assert.h>
#include
"sort_private.h"
#include
<stdlib.h>
#include
<string.h>
void
*
upo_insertion_sort_insert_pos
(
const
void
*
base
,
const
void
*
elem
,
size_t
n
,
size_t
size
,
upo_sort_comparator_t
cmp
)
{
for
(
size_t
i
=
0
;
i
<
n
;
++
i
)
{
if
(
cmp
((
unsigned
char
*
)
base
+
i
*
size
,
elem
)
>=
0
)
{
return
(
unsigned
char
*
)
base
+
i
*
size
;
}
}
// unreachable
assert
(
0
);
}
void
upo_merge_sort_merge
(
void
*
base1
,
size_t
n1
,
void
*
base2
,
size_t
n2
,
size_t
size
,
upo_sort_comparator_t
cmp
)
{
unsigned
char
*
tmp
=
malloc
((
n1
+
n2
)
*
size
);
size_t
i1
=
0
;
size_t
i2
=
0
;
while
(
i1
<
n1
&&
i2
<
n2
)
{
unsigned
char
*
curr1
=
(
unsigned
char
*
)
base1
+
i1
*
size
;
unsigned
char
*
curr2
=
(
unsigned
char
*
)
base2
+
i2
*
size
;
unsigned
char
*
currtmp
=
tmp
+
(
i1
+
i2
)
*
size
;
if
(
cmp
(
curr1
,
curr2
)
<
0
)
{
memcpy
(
currtmp
,
curr1
,
size
);
++
i1
;
}
else
{
memcpy
(
currtmp
,
curr2
,
size
);
++
i2
;
}
}
while
(
i1
<
n1
)
{
unsigned
char
*
curr
=
(
unsigned
char
*
)
base1
+
i1
*
size
;
unsigned
char
*
currtmp
=
tmp
+
(
i1
+
i2
)
*
size
;
memcpy
(
currtmp
,
curr
,
size
);
++
i1
;
}
while
(
i2
<
n2
)
{
unsigned
char
*
curr
=
(
unsigned
char
*
)
base2
+
i2
*
size
;
unsigned
char
*
currtmp
=
tmp
+
(
i1
+
i2
)
*
size
;
memcpy
(
currtmp
,
curr
,
size
);
++
i2
;
}
memcpy
(
base1
,
tmp
,
n1
*
size
);
memcpy
(
base2
,
tmp
+
n1
*
size
,
n2
*
size
);
free
(
tmp
);
}
This diff is collapsed.
Click to expand it.
alglib/src/sort_private.h
+
16
−
2
View file @
23cc6f4c
...
...
@@ -31,7 +31,7 @@
#include
<stddef.h>
#include
<upo/sort.h>
void
*
upo_insertion_sort_insert_pos
(
static
void
*
upo_insertion_sort_insert_pos
(
const
void
*
base
,
const
void
*
elem
,
size_t
n
,
...
...
@@ -39,8 +39,22 @@ void *upo_insertion_sort_insert_pos(
upo_sort_comparator_t
cmp
);
void
upo_merge_sort_merge
(
void
*
base1
,
size_t
n1
,
void
*
base2
,
size_t
n2
,
size_t
size
,
upo_sort_comparator_t
cmp
);
static
void
upo_merge_sort_merge
(
void
*
base1
,
size_t
n1
,
void
*
base2
,
size_t
n2
,
size_t
size
,
upo_sort_comparator_t
cmp
);
static
void
*
upo_quick_sort_partition
(
}
void
*
base
,
size_t
n
,
size_t
size
,
upo_sort_comparator_t
cmp
);
/* TO STUDENTS:
*
...
...
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