From ec7804223e6d841aa4d48d647124d90575629dd4 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Sat, 24 Oct 2020 00:35:41 +0000 Subject: [PATCH 1/5] Bubble sort simpple algo --- Sorting/Bubble Sort/bubble_sort.php | 51 +++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Sorting/Bubble Sort/bubble_sort.php diff --git a/Sorting/Bubble Sort/bubble_sort.php b/Sorting/Bubble Sort/bubble_sort.php new file mode 100644 index 0000000..1e5eb65 --- /dev/null +++ b/Sorting/Bubble Sort/bubble_sort.php @@ -0,0 +1,51 @@ + $val){ + + if(isset($unsorted[$idx + 1]) && $unsorted[$idx] > $unsorted[$idx + 1]){ + // swap + swap($unsorted, $idx, $idx + 1); + $swap = true; + } + + } + + }while($swap); + + return $unsorted; + +} + + +$sorted = bubble_sort($unsorted); + +// print list +echo "\n"; +foreach( $sorted as $val){ + echo $val," "; +} +echo "\n"; \ No newline at end of file From f4641cee16a53cbba7d4511474aee34b8b435c33 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Sat, 24 Oct 2020 12:44:46 +0000 Subject: [PATCH 2/5] Bidirectional bubble sort --- .../Bubble Sort/bidirectional_bubble_sort.php | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Sorting/Bubble Sort/bidirectional_bubble_sort.php diff --git a/Sorting/Bubble Sort/bidirectional_bubble_sort.php b/Sorting/Bubble Sort/bidirectional_bubble_sort.php new file mode 100644 index 0000000..dbb4ead --- /dev/null +++ b/Sorting/Bubble Sort/bidirectional_bubble_sort.php @@ -0,0 +1,75 @@ + $unsorted[$comp]){ + swap($unsorted,$idx, $comp); + $swap = true; + echo "swap left \n"; + } + + } + + // break if no sort + if($swap == false){ + break; + } + + // Sort right directional + for($idx = $array_count; $idx >= 0; $idx--){ + + $comp = $idx - 1; + if(isset($unsorted[$comp]) && $unsorted[$idx] < $unsorted[$comp]){ + swap($unsorted, $idx, $comp); + $swap = true; + echo "swap right \n"; + } + + } + + }while($swap); + + // return array + return $unsorted; + +} + +// print results +echo "\n"; +$sorted = bi_bubble_sort($unsorted); +foreach($sorted as $val){ + echo $val, " "; +} +echo "\n"; \ No newline at end of file From 8372206a270588b75191c5085cf7fe5975765871 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Sun, 1 Nov 2020 18:14:39 +0000 Subject: [PATCH 3/5] Counting Sort Algo --- Sorting/Counting Sort/countingSort_1.php | 101 +++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 Sorting/Counting Sort/countingSort_1.php diff --git a/Sorting/Counting Sort/countingSort_1.php b/Sorting/Counting Sort/countingSort_1.php new file mode 100644 index 0000000..ef3d456 --- /dev/null +++ b/Sorting/Counting Sort/countingSort_1.php @@ -0,0 +1,101 @@ + $val){ + + /** + * Increament value correponding to value + * i.e $count[2]++ + */ + $count[$val]++; + + } + + /** + * Store the cummulative count of each element + * Iterate count array and set start index position of + * counted elements. + */ + for($idx = 1; $idx <= $max_val; $idx++){ + + /** + * We are adding the previous index's count + * to the current count. This will continue + * for [k] range of elements. + */ + $count[$idx] = ($count[$idx] + $count[$idx - 1]); + + } + + // Find the index of each element + for ($idx = $array_size - 1; $idx >= 0; $idx--) { + + /** + * So we use the value at the current index pointed to. + * We then grap its cummulative value + * and adjust to correct position, by subtracting 1. + */ + $index = ($count[$rand[$idx]] - 1); + + /** + * We then store the value from the original + * array in the adjusted postion. + */ + $output[$index] = $rand[$idx]; + + /** + * We then decreament the current stored value to reflect + * the moved value. + */ + $count[$rand[$idx]]--; + + } + + + // Copy the sorted array into the original array + for($idx = 0; $idx <= $array_size - 1; $idx++){ + + // move to correct position + $rand[$idx] = $output[$idx]; + + } + + return $rand; + +} + +print_r(countingSort($randomSeq)); \ No newline at end of file From 7b64e343c879583130e4f9f12bfcff00705fe69e Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Thu, 12 Nov 2020 12:13:44 +0000 Subject: [PATCH 4/5] added trivia algos --- Sorting/trivia/sort_using_index.php | 60 +++++++++++++++++++++++++ Sorting/trivia/sort_using_last_char.php | 60 +++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 Sorting/trivia/sort_using_index.php create mode 100644 Sorting/trivia/sort_using_last_char.php diff --git a/Sorting/trivia/sort_using_index.php b/Sorting/trivia/sort_using_index.php new file mode 100644 index 0000000..f53d704 --- /dev/null +++ b/Sorting/trivia/sort_using_index.php @@ -0,0 +1,60 @@ + substr($val_2, $idx)){ + + return 1; + + }else if( substr($val_1, $idx) < substr($val_2, $idx) ){ + + return -1; + + } + + return null; + + }); + + return $sort; + + }catch(\Exception $e){ + + exit($e->getMessage()); + echo "\n\n"; + + } + +} + +$arr = ['orange', 'banana', 'apple','carrot', 'collard', 'pea']; + +$sorted = index_sort($arr, 2); + +foreach($sorted as $val){ + + echo $val . " "; + +} + +echo "\n\n"; \ No newline at end of file diff --git a/Sorting/trivia/sort_using_last_char.php b/Sorting/trivia/sort_using_last_char.php new file mode 100644 index 0000000..c6d85b9 --- /dev/null +++ b/Sorting/trivia/sort_using_last_char.php @@ -0,0 +1,60 @@ + substr($val_2,-1)){ + + return 1; + + }else if(substr($val_1,-1) < substr($val_2,-1)){ + + return -1; + + } + + return null; + + }); + + return $usort; + + }catch(\Exception $e){ + + exit($e->getMessage()); + echo "\n\n\n\n"; + + } + +} + +$arr = ['orange', 'banana', 'apple','carrot', 'collard', 'pea']; + +$sorted_arr = sort_last_char($arr); + +foreach($sorted_arr as $val){ + + echo $val . " "; + +} + +echo "\n\n\n\n"; \ No newline at end of file From 3ee897da299d69cb5c7e5bc498a4763807002199 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Fri, 13 Nov 2020 01:51:19 +0000 Subject: [PATCH 5/5] added some trivia algos --- Challenges/check_anagrams.php | 22 ++++++++++ Challenges/check_palindrome.php | 34 ++++++++++++++++ Sorting/trivia/group_by_value.php | 31 ++++++++++++++ Sorting/trivia/oddEven.php | 22 ++++++++++ Sorting/trivia/simple_uasort.php | 54 +++++++++++++++++++++++++ Sorting/trivia/sort_using_index.php | 4 +- Sorting/trivia/sort_using_last_char.php | 4 +- 7 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 Challenges/check_anagrams.php create mode 100644 Challenges/check_palindrome.php create mode 100644 Sorting/trivia/group_by_value.php create mode 100644 Sorting/trivia/oddEven.php create mode 100644 Sorting/trivia/simple_uasort.php diff --git a/Challenges/check_anagrams.php b/Challenges/check_anagrams.php new file mode 100644 index 0000000..b2f0e58 --- /dev/null +++ b/Challenges/check_anagrams.php @@ -0,0 +1,22 @@ +getMessage()); + echo "\n\n"; + + } + +} + +echo check_palindrome("DAD"); +echo "\n\n"; +echo check_palindrome("MALAYALAM"); +echo "\n\n"; +echo check_palindrome("MALAYAAM"); +echo "\n\n"; \ No newline at end of file diff --git a/Sorting/trivia/group_by_value.php b/Sorting/trivia/group_by_value.php new file mode 100644 index 0000000..77cd70c --- /dev/null +++ b/Sorting/trivia/group_by_value.php @@ -0,0 +1,31 @@ + 20, +"meeting" => 50, +"dynasty" => 20, +"chocolate" => 20, +"bananas" => 50, +"fantasy" => 30, +"football" => 20 +); + + +function group_by_value(array &$arr): array{ + + $results = []; + + foreach($arr as $key => $val){ + + $results[$val][] = $key; + + } + + return $results; + +} + +print_r(group_by_value($arr)); \ No newline at end of file diff --git a/Sorting/trivia/oddEven.php b/Sorting/trivia/oddEven.php new file mode 100644 index 0000000..81f2e57 --- /dev/null +++ b/Sorting/trivia/oddEven.php @@ -0,0 +1,22 @@ + $val_2){ + + return 1; + + }else if($val_1 < $val_2){ + + return -1; + + } + + return 0; + + }); + + return $usort; + + }catch(\Exception $e){ + + exit($e->getMessage()); + echo "\n\n"; + + } + +} + +$arr = array("a" => 4,"b" => 2,"c" => 8,"d" => 6); + +$sorted = simpple_sort($arr); + +foreach($sorted as $ky => $vl){ + + echo "key = " . $ky . " : " . " value = " . $vl ."\n"; + +} + +echo "\n\n"; \ No newline at end of file diff --git a/Sorting/trivia/sort_using_index.php b/Sorting/trivia/sort_using_index.php index f53d704..f34001b 100644 --- a/Sorting/trivia/sort_using_index.php +++ b/Sorting/trivia/sort_using_index.php @@ -18,7 +18,7 @@ function index_sort(array &$sort, int $idx): array{ if(!ctype_alpha($val_1) || !ctype_alpha($val_2)){ - return null; + return 0; } @@ -32,7 +32,7 @@ function index_sort(array &$sort, int $idx): array{ } - return null; + return 0; }); diff --git a/Sorting/trivia/sort_using_last_char.php b/Sorting/trivia/sort_using_last_char.php index c6d85b9..61b5630 100644 --- a/Sorting/trivia/sort_using_last_char.php +++ b/Sorting/trivia/sort_using_last_char.php @@ -18,7 +18,7 @@ function sort_last_char(array &$usort): array{ if(!ctype_alpha($val_1) || !ctype_alpha($val_2)){ - return null; + return 0; } @@ -32,7 +32,7 @@ function sort_last_char(array &$usort): array{ } - return null; + return 0; });