Skip to content

Commit ed3f57b

Browse files
committed
Add first quicksort
1 parent 095eec4 commit ed3f57b

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ SET ( cppcore_common_src
7777
include/cppcore/Common/Hash.h
7878
include/cppcore/Common/TStringBase.h
7979
include/cppcore/Common/Variant.h
80+
include/cppcore/Common/Sort.h
8081
include/cppcore/Common/TBitField.h
8182
include/cppcore/Common/TOptional.h
8283
)
@@ -133,6 +134,7 @@ IF( CPPCORE_BUILD_UNITTESTS )
133134
SET( cppcore_common_test_src
134135
test/common/HashTest.cpp
135136
test/common/VariantTest.cpp
137+
test/common/SortTest.cpp
136138
test/common/TBitFieldTest.cpp
137139
test/common/TOptionalTest.cpp
138140
)

include/cppcore/Common/Sort.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#pragma once
2+
3+
#include <cppcore/CPPCoreCommon.h>
4+
5+
namespace cppcore {
6+
7+
typedef int32_t (*ComparisonFn)(const void* _lhs, const void* _rhs);
8+
9+
int32_t int_comp(const void *lhs, const void *rhs) {
10+
int32_t _lhs=0, _rhs=0;
11+
memcpy(&_lhs, lhs, sizeof(int32_t));
12+
memcpy(&_rhs, rhs, sizeof(int32_t));
13+
if (_lhs > _rhs) {
14+
return 1;
15+
} else if (lhs == rhs) {
16+
return 0;
17+
}
18+
return -1;
19+
}
20+
21+
template<class T>
22+
void swap(T *v1, T *v2) {
23+
T tmp = *v1;
24+
*v1 = *v2;
25+
*v2 = *tmp;
26+
}
27+
28+
inline void quicksort(void *pivot, void *_data, size_t num, ComparisonFn func=int_comp) {
29+
if (num < 2) {
30+
return;
31+
}
32+
33+
if (_data == nullptr) {
34+
return;
35+
}
36+
37+
size_t l = 0;
38+
size_t g = 1;
39+
uint8_t *data = (uint8_t*) _data;
40+
for (size_t i=1; i<num;) {
41+
int32_t result = func(&data[i], pivot);
42+
if (result > 0) {
43+
swap(&data[l], &data[i]);
44+
++l;
45+
} else if (result == 0) {
46+
swap(&data[g], &data[i]);
47+
++g;
48+
++i;
49+
} else {
50+
++i;
51+
}
52+
}
53+
54+
quicksort(pivot, &data[0], l, func);
55+
quicksort(pivot, &data[g], num-g, func);
56+
}
57+
58+
bool isSorted(void *data, size_t num, ComparisonFn func) {
59+
if (num < 2) {
60+
return true;
61+
}
62+
63+
for (size_t i=0; i<num-1; ++i) {
64+
const int32_t result = func(&data[i], &data[i+1]);
65+
if (result == -1) {
66+
return false;
67+
}
68+
}
69+
70+
return true;
71+
}
72+
73+
} // namespace cppcore

test/common/SortTest.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
-------------------------------------------------------------------------------------------------
3+
The MIT License (MIT)
4+
5+
Copyright (c) 2014-2024 Kim Kulling
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy of
8+
this software and associated documentation files (the "Software"), to deal in
9+
the Software without restriction, including without limitation the rights to
10+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11+
the Software, and to permit persons to whom the Software is furnished to do so,
12+
subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
-------------------------------------------------------------------------------------------------
24+
*/
25+
#include <cppcore/Common/Sort.h>
26+
27+
#include "gtest/gtest.h"
28+
29+
using namespace cppcore;
30+
31+
class SortTest : public testing::Test {
32+
public:
33+
34+
protected:
35+
// empty
36+
};
37+
38+
TEST_F(SortTest, isSortedTest ) {
39+
int32_t arr[] = {1,2,3,4,5};
40+
bool sorted = isSorted(arr, 5, int_comp);
41+
EXPECT_TRUE(sorted);
42+
}
43+

0 commit comments

Comments
 (0)