-
Notifications
You must be signed in to change notification settings - Fork 317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: insert sort and heap sort in sort.c #467
Draft
Skyseaee
wants to merge
10
commits into
bytedance:main
Choose a base branch
from
Skyseaee:feat/insert_sort_and_heap_sort
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2e80f45
feat: add insert_sort and heap_sort in sort.c
Skyseaee 193f83e
Merge branch 'main' into feat/insert_sort_and_heap_sort
Skyseaee 84bad44
Merge branch 'main' of github.com:Skyseaee/sonic into feat/insert_sor…
Skyseaee cf82f3c
feat: modify _strcmp function in sort.c
Skyseaee 9de1243
Merge branch 'feat/insert_sort_and_heap_sort' of github.com:Skyseaee/…
Skyseaee b4fe75e
feat: add insert sort and heap sort in sort.c
Skyseaee b4c8825
Merge branch 'main' into feat/insert_sort_and_heap_sort
liuq19 8217c2e
Merge branch 'feat/insert_sort_and_heap_sort' of github.com:Skyseaee/…
Skyseaee 538f374
fix: fix boundary crossing and Chinese character sorting issues.
Skyseaee 04e3e30
fix: add boundary handling.
Skyseaee File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include <stdio.h> | ||
|
||
#include "native.h" | ||
|
||
typedef struct MapPair{ | ||
GoString key; | ||
void* value; | ||
} MapPair; | ||
|
||
// swap elems in MapPair | ||
void swap(MapPair* lhs, MapPair* rhs) { | ||
MapPair temp; | ||
temp = *lhs; | ||
*lhs = *rhs; | ||
*rhs = temp; | ||
} | ||
|
||
static always_inline size_t min(size_t x, size_t y) { | ||
return x > y ? y : x; | ||
} | ||
|
||
static always_inline bool less(MapPair lhs, MapPair rhs) { | ||
size_t index = 0; | ||
size_t size = min(lhs.key.len, rhs.key.len); | ||
while(index < size && *lhs.key.buf && *rhs.key.buf && *lhs.key.buf == *rhs.key.buf) { | ||
lhs.key.buf++, rhs.key.buf++; | ||
index++; | ||
} | ||
|
||
return *(unsigned char *)lhs.key.buf - *(unsigned char *)rhs.key.buf < 0; | ||
liuq19 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
void heap_adjust(MapPair* kvs, size_t root, size_t end) { | ||
for(size_t child=2*root + 1; child<=end; child = child*2 + 1) { | ||
if(child < end && less(kvs[child], kvs[child+1])) { | ||
child++; | ||
} | ||
|
||
if(!less(kvs[root], kvs[child])) { | ||
break; | ||
} else { | ||
swap(&kvs[root], &kvs[child]); | ||
root = child; | ||
} | ||
} | ||
} | ||
|
||
void heap_sort(MapPair* kvs, size_t n) { | ||
if(n <= 1) { | ||
return; | ||
} | ||
|
||
for(int i=(n-1)/2; i>=0; i--) { | ||
heap_adjust(kvs, i, n-1); | ||
} | ||
|
||
for(int i=n-1; i>0; i--) { | ||
swap(&kvs[i], &kvs[0]); | ||
heap_adjust(kvs, 0, i-1); | ||
} | ||
} | ||
|
||
void insert_sort(MapPair* kvs, size_t n) { | ||
if(n <= 1) { | ||
return; | ||
} | ||
|
||
for(size_t i=1; i<n; i++) { | ||
MapPair temp = kvs[i]; | ||
int j=i-1; | ||
while(j>=0 && less(temp, kvs[j])) { | ||
kvs[j + 1] = kvs[j]; | ||
j--; | ||
} | ||
kvs[j+1] = temp; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#include <assert.h> | ||
#include <string.h> | ||
#include "../sort.c" | ||
|
||
void test_heap_sort(MapPair* input, MapPair* expect, int n) { | ||
heap_sort(input, n); | ||
for(int i=0; i<n; i++) { | ||
assert(strcmp(input[i].key.buf, expect[i].key.buf) == 0); | ||
} | ||
} | ||
|
||
void test_insert_sort(MapPair* input, MapPair* expect, int n) { | ||
insert_sort(input, n); | ||
for(int i=0; i<n; i++) { | ||
assert(strcmp(input[i].key.buf, expect[i].key.buf) == 0); | ||
} | ||
} | ||
|
||
int main() { | ||
MapPair mpr[5]; | ||
char temp[6] = "abcdf"; | ||
mpr[0].key.buf = "abcd"; | ||
mpr[0].key.len = 5; | ||
|
||
mpr[1].key.buf = "abcdf"; | ||
mpr[1].key.len = 6; | ||
|
||
mpr[2].key.buf = "测试字段"; | ||
mpr[2].key.len = 13; | ||
|
||
mpr[3].key.buf = ""; | ||
mpr[3].key.len = 1; | ||
|
||
mpr[4].key.buf = temp; | ||
mpr[4].key.len = 5; | ||
|
||
MapPair expect[5]; | ||
|
||
expect[0].key.buf = ""; | ||
expect[0].key.len = 0; | ||
|
||
expect[1].key.buf = "abcd"; | ||
expect[1].key.len = 5; | ||
|
||
expect[2].key.buf = temp; | ||
expect[2].key.len = 5; | ||
|
||
expect[3].key.buf = "abcdf"; | ||
expect[3].key.len = 6; | ||
|
||
expect[4].key.buf = "测试字段"; | ||
expect[4].key.len = 13; | ||
|
||
MapPair mpr_2[5]; | ||
memcpy(mpr_2, mpr, 5 * sizeof(MapPair)); | ||
|
||
test_heap_sort(mpr, expect, 5); | ||
test_insert_sort(mpr_2, expect, 5); | ||
|
||
// test when n is a large number | ||
MapPair mpr_n_heap[4096]; | ||
MapPair mpr_n_insert[4096]; | ||
MapPair expect_n[4096]; | ||
for(int i=0; i<4096; i++) { | ||
mpr_n_heap[i].key.buf = "abcda"; | ||
mpr_n_heap[i].key.len = 6; | ||
|
||
expect_n[i].key.buf = "abcda"; | ||
expect_n[i].key.len = 6; | ||
} | ||
mpr_n_heap[2000].key.buf = ""; | ||
mpr_n_heap[2000].key.len = 1; | ||
mpr_n_heap[1000].key.buf = "aabb"; | ||
mpr_n_heap[1000].key.len = 5; | ||
mpr_n_heap[3000].key.buf = "ss"; | ||
mpr_n_heap[3000].key.len = 3; | ||
mpr_n_heap[4000].key.buf = "中文字符"; | ||
mpr_n_heap[4000].key.len = 13; | ||
mpr_n_heap[4095].key.buf = ""; | ||
mpr_n_heap[4095].key.len = 1; | ||
|
||
expect_n[0].key.buf = ""; | ||
expect_n[0].key.len = 1; | ||
expect_n[1].key.buf = ""; | ||
expect_n[1].key.len = 1; | ||
expect_n[2].key.buf = "aabb"; | ||
expect_n[2].key.len = 5; | ||
expect_n[2000].key.buf = "abcda"; | ||
expect_n[2000].key.len = 6; | ||
expect_n[1000].key.buf = "abcda"; | ||
expect_n[1000].key.len = 6; | ||
expect_n[3000].key.buf = "abcda"; | ||
expect_n[3000].key.len = 6; | ||
expect_n[4000].key.buf = "abcda"; | ||
expect_n[4000].key.len = 6; | ||
expect_n[4094].key.buf = "ss"; | ||
expect_n[4094].key.len = 3; | ||
expect_n[4095].key.buf = "中文字符"; | ||
expect_n[4095].key.len = 13; | ||
|
||
memcpy(mpr_n_insert, mpr_n_heap, 4096 * sizeof(MapPair)); | ||
test_heap_sort(mpr_n_heap, expect_n, 4096); | ||
test_insert_sort(mpr_n_insert, expect_n, 4096); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里比较长度之后,应该不用确认 *lhs.key.buf && *rhs.key.buf了,另外这里可以使用局部变量表示两个lhs.key.buf 和 rhs.key.buf。