Skip to content
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

Optimize Median method from TC O(N log N) to Linear Time Complexity #23

Conversation

mohammedelgammal
Copy link
Contributor

implementing IntroSelect algorithm which merges between quickSelect and medianOfMedians algorithms with utilities functions dt_min named after lib name to prevent conflict and swap.

…) to O(N) using IntroSort Algorithm

Median now uses introSelect algorithm merging between quickSelect and medianOfMedians Algorithm
feat[DataTomeUtils]: Adding dt_min helper function to get minimum value between two values and swap to swap two values
resolves (AlexandreHiroyuki#18)
@AlexandreHiroyuki
Copy link
Owner

The CI broke with the following error:

src/DataTomeAnalysis.h: In member function ‘TypeOfArray DataTomeAnalysis<TypeOfArray, TypeOfSum>::medianOfMedians(int, int, TypeOfArray*)’:
src/DataTomeAnalysis.h:287:31: error: there are no arguments to ‘min’ that depend on a template parameter, so a declaration of ‘min’ must be available [-fpermissive]
  287 |         int left = i, right = min(i + k, r + 1),
      |                               ^~~

You have to include some library to use the min function.
I suggest you include the C library because Cpp-exclusive libraries can break the compilation for some specific devices.

src/DataTomeUtils.h Outdated Show resolved Hide resolved
src/DataTomeAnalysis.h Outdated Show resolved Hide resolved
@AlexandreHiroyuki
Copy link
Owner

AlexandreHiroyuki commented Dec 16, 2024

New error:

Processing test_DataTomeAnalysis in desktop environment
--------------------------------------------------------------------------------
Building...
In file included from test/test_DataTomeAnalysis/DataTomeAnalysis.test.cpp:1:
In file included from src/DataTomeAnalysis.h:11:
src/DataTomeUtils.h:25:1: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
auto dt_min(const T1 &a, const T2 &b) -> decltype(a < b ? a : b) {
^
src/DataTomeUtils.h:25:1: error: 'auto' not allowed in function return type
auto dt_min(const T1 &a, const T2 &b) -> decltype(a < b ? a : b) {
^~~~
src/DataTomeUtils.h:25:38: error: expected ';' at end of declaration
auto dt_min(const T1 &a, const T2 &b) -> decltype(a < b ? a : b) {
                                     ^
                                     ;
src/DataTomeUtils.h:25:39: error: cannot use arrow operator on a type
auto dt_min(const T1 &a, const T2 &b) -> decltype(a < b ? a : b) {
                                      ^
In file included from test/test_DataTomeAnalysis/DataTomeAnalysis.test.cpp:1:
src/DataTomeAnalysis.h:287:31: error: no matching function for call to 'dt_min'
        int left = i, right = dt_min(i + k, r + 1),
                              ^~~~~~
1 warning and 4 errors generated.
*** [.pio/build/desktop/test/test_DataTomeAnalysis/DataTomeAnalysis.test.o] Error 1
Building stage has failed, see errors above. Use `pio test -vvv` option to enable verbose output.
---------- desktop:test_DataTomeAnalysis [ERRORED] Took 0.50 seconds ----------

It seems like the auto keyword is not supported on the version we need.

@mohammedelgammal
Copy link
Contributor Author

Refactored dt_min and CI tests ran successfully

Copy link
Owner

@AlexandreHiroyuki AlexandreHiroyuki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@AlexandreHiroyuki AlexandreHiroyuki merged commit 3f601cb into AlexandreHiroyuki:main Dec 16, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants