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/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 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 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 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 new file mode 100644 index 0000000..f34001b --- /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 0; + + }); + + 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..61b5630 --- /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 0; + + }); + + 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