Skip to content

Commit

Permalink
working on a guide for CPP09 ex02
Browse files Browse the repository at this point in the history
  • Loading branch information
loyc12 committed Jun 11, 2024
1 parent 23dcf19 commit cd56079
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 22 deletions.
50 changes: 41 additions & 9 deletions exFOO/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ DEFAULT_GOAL: all
re run rerun \
leaks releaks \
vleaks revleaks \
norm libft \
norm sub libft \
brew cmake \
glfw grind \
help man \


#------------------------------------------------------------------------------#
Expand Down Expand Up @@ -45,9 +46,6 @@ CMD = ./$(NAME) $(ARGS)
# BASE TARGETS #
#------------------------------------------------------------------------------#

# For full install (except brew)
long: cmake glfw $(NAME)

# For standard compilation
all: $(OBJDIR) $(NAME)

Expand All @@ -72,15 +70,13 @@ $(OBJS): $(OBJDIR)%.o : $(SRCDIR)%$(CX)
#------------------------------------------------------------------------------#

# Removes objects
clear: clean
clean:
clean clear:
$(HIDE) $(RM) $(OBJS)
$(HIDE) $(RM) $(NAME).dSYM
@echo "$(MAGENTA)\nDeleted object files\n $(DEFCOL)"

# Removes object dir and executable
fclear: fclean
fclean: clean
fclean fclear: clean
$(HIDE) $(RM) $(OBJDIR)
@echo "$(RED)Deleted object directory\n $(DEFCOL)"
$(HIDE) $(RM) $(NAME)
Expand Down Expand Up @@ -165,6 +161,9 @@ libft:
# BREW TARGETS #
#------------------------------------------------------------------------------#

# For full install (except brew)
long: cmake glfw $(NAME)

# Installs/Updates homebrew (WARNING : can be very slow !)
brew:
@echo "$(YELLOW)\nInstalling Brew\n $(DEFCOL)"
Expand Down Expand Up @@ -204,4 +203,37 @@ grind:
echo "$(BLUE)\nVALGRIND installed !\n $(DEFCOL)"; \
else \
echo "$(RED)\n! FAILED TO INSTALL VALGRIND !\n $(DEFCOL)"; \
fi
fi


#------------------------------------------------------------------------------#
# HELP TARGETS #
#------------------------------------------------------------------------------#

# Displays the help message
help man:
@echo "$(BLUE)Usage : make [target] $(DEFCOL)"
@echo ""
@echo "$(CYAN)Targets$(DEFCOL)"
@echo ""
@echo " $(GREEN)help/man$(DEFCOL) : Displays this message"
@echo ""
@echo " $(GREEN)all$(DEFCOL) : Compiles the program"
@echo " $(GREEN)clea[n/r]$(DEFCOL) : Removes object files"
@echo " $(GREEN)fclea[n/r]$(DEFCOL) : Removes object files and executable"
@echo ""
@echo " $(GREEN)re$(DEFCOL) : Removes object files and executable and recompiles"
@echo " $(GREEN)(re)run$(DEFCOL) : (Re)Compiles and runs the program"
@echo " $(GREEN)(re)leaks$(DEFCOL) : (Re)Compiles and runs the program with leaks"
@echo " $(GREEN)(re)vleaks$(DEFCOL) : (Re)Compiles and runs the program with valgrind"
@echo ""
@echo " $(GREEN)sub$(DEFCOL) : Updates the git submodules"
@echo " $(GREEN)libft$(DEFCOL) : Updates the libft submodule"
@echo " $(GREEN)norm$(DEFCOL) : Norminettes the project"
@echo ""
@echo " $(GREEN)long$(DEFCOL) : Installs/Updates everything except brew"
@echo " $(GREEN)brew$(DEFCOL) : Installs/Updates homebrew"
@echo " $(GREEN)cmake$(DEFCOL) : Installs/Updates cmake"
@echo " $(GREEN)glfw$(DEFCOL) : Installs/Updates glfw"
@echo " $(GREEN)grind$(DEFCOL) : Installs/Updates valgrind"
@echo ""
1 change: 0 additions & 1 deletion mod6/ex00/ScalarConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ std::string ScalarConverter::getString() const
return str;
}


std::ostream &operator<< ( std::ostream &out, const ScalarConverter &rhs )
{
out << rhs.getString();
Expand Down
12 changes: 0 additions & 12 deletions mod9/ex02/PmergeMe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,6 @@ IVEC PmergeMe::getVect( void ) const { return this->_V; }
ILST PmergeMe::getList( void ) const { return this->_L; }
time_t PmergeMe::getSortTime( ) const { return this->_sortTime; }

// checkers
/*
// throw and error if there is duplicate values
void checkVect()
{
return;
}
void checkList()
{
return;
}
*/

// Sorters

Expand Down
67 changes: 67 additions & 0 deletions mod9/ex02/PsortMe.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,72 @@
#include "PmergeMe.hpp"

// tl:dr of how ford johnson works :

/*
0 : INTRO : the Ford-Johnson algorithm ( or merge-insertion sort) is an sorting algorithm that optimizes the number of comparisons by maximizing \
| comparisons within ranges of 2n - 1 elements ( aka one less than 2^n ), which are optimal for binary searches, as they required as many \
| comparisons as any other ranges between 2^(n-1) and (2^n) - 1 ( inclusively )
0.1 : NOTE : thus, all insertions and sorts are done via binary searches, which is optimized for ranges of size 2^n - 1, aka one less than a factor of 2
0.3 : NOTE : it is possible work with duplicates, but it neccesitates significant tweaks to the algorithm ( it should really be a bonus ngl )
1 : STEP : input an initial, unsorted array of values
1.2 : STEP : if the lenght of said array is uneven, store one of the numbers away ( we will insert it back at the end )
1.3 : STEP : pair elements together and store the pairs into a new pair array ( doesn't matter how they get paired )
2 : STEP : sort the values inside the pairs ( biggest one first is standard )
2.1 : NOTE : we will call the biggest element of a given pair Pn; Xn, and the smallest; Yn
2.2 : STEP : sort the pair array based on each pair X's value ( smallest to biggest )
2.3 : NOTE : this means Pn = [ Xn, Yn ], where Xn > Yn and Xn > X(n-1)
2.4 : NOTE : this, of course, doesn't not garantee that Yn > Y(n-1), hence the following complex sorting algorithm
3 : STEP : create a final array and store every Xn in it ( from biggest to smallest )
3.1 : STEP : put Y0 ( smallest value of P0 ) at the beginning of said final array
3.2 : EXAMPLE : the array should now look like; [ Y0, X0, X1, X2, X3, X4, X5, ... ]
==== USING FACTORS OF 2 ====
4 : STEP : create a variable to store the current range factor ( f ), and initiate it to 2
4.1 : NOTE : this means that, if the range factor is n, the range size will be n^2
4.2 : NOTE : I will call each successive range Jn, where n is the range factor
5 : STEP : iter over each yet-uninserted Yn, where n <= f
5.1 : IMPORTANT : we need to iterate backwards ( starting from Yf and decresing towards Y0 )
5.2 : IMPORTANT : we need to avoid inserting the same value twice, and thus need to stop at Y(F-1)
5.1 : IMPORTANT
==== USING THE JACKOBSTHAL SEQUENCE ====
4 : STEP : load or build the jacobsthal sequence ( 1, 1, 3, 5, 11, 21, 43, 85, 171, 341, 683, 1365, ... ) ( sequence where Jn = (Jn-1) + 2(Jn-2) )
4.1 : NOTE : we will call each jacobsthal element Jn. we will use j when a Jn is used as an index ( ex : Xj instead of XJn )
4.2 : NOTE : we will use these Jn to find each successive insertion range size ( from index 0 to Xj, where j = Jn )
4.3 : NOTE : the initial Jn value is that of J2 ( value of 3 ), as the insertion of Y0 is trivial and already done ( J0 / J1 are thus skipped )
4.4 : NOTE : this means the first insertion range is; [ Y0, X0, X1 ]
5 : STEP : iter over each yet-uninserted Yn, and insert them into the final array
5.1 : IMPORTANT : you need to insert them in reverse order ( from Yj to Y0, stopping at the previous Yj )
5.2 : EXAMPLE : inserting Y2 ( largest Yn in range J3 ); [ Y0, X0, X1 ] -> [ Y0, X0, X1, Y2 ]
5.3 : NOTE : as the iteration range is a fixed size, the final value will be kicked out of the range after each insertion
5.4 : EXAMPLE : [ Y0, X0, X1, Y2 ] -> [ Y0, X0, X1 ]
5.5 : NOTE : this is by design, since all remaining insertion in this range are garanteed to be smaller than the kicked value
6 : STEP : repeat step 5 until all yet uninserted Yn bellow Y(j+1) have been inserted
6.1 : EXAMPLE : on the first loop ( range of J2 = 3 ), we insert Y2, then Y1. we then finish, as Y0 is already inserted
7 : STEP : once you are done inserting in this range, set the new range to J(n+1), and go back to step 5 ( again... nested loop are so fun after all )
7.2 : EXAMPLE : on the second loop ( range of J3 = 5 ), we insert Y4, then Y3, then finish, since Y3 is already inserted
7.3 : EXAMPLE : on the third loop ( range of J4 = 11 ), we insert Y10 to Y5, then finish, since Y5 is already inserted
7.4 : NOTE : on each succesive loop, the range increases by a factor of 2, thus making sure it is always optimized for binary search
7.5 : IMPORTANT : make sure to cap the insertion range to the final array's size ( which is often not equal to 2^n - 1 )
8 : IMPORTANT : what I call range ( ex : a range of 5 ), is actually the index of the Yn to restart the insertion from
8.1 : EXAMPLE : this means that range 5 will actually look like [ Y0, X0, X1, Y2, X2, X3, X4 ]
8.2 : NOTE : the actual "lenght" of each ranges is simply 2n - 1, where n starts a 2 and increase by 1 after each loop
9 : STEP : once all Yn have been inserted, insert the last remaining Yn ( if any ) into the array via a simple binary search
*/

// sorts a vector of ints using the Ford-Johnson algorithm
int PmergeMe::sortVect( void )
{
Expand Down

0 comments on commit cd56079

Please sign in to comment.