diff --git a/Sorting/BubbleSort.php b/Sorting/BubbleSort.php index 9bc66795..9431d774 100644 --- a/Sorting/BubbleSort.php +++ b/Sorting/BubbleSort.php @@ -11,18 +11,18 @@ function bubbleSort($array) $length = count($array); for ($i = $length; $i > 0; $i--) { - $swapped = true; + $swapped = false; for ($j = 0; $j < $i - 1; $j++) { if ($array[$j] > $array[$j + 1]) { $temp = $array[$j]; $array[$j] = $array[$j + 1]; $array[$j + 1] = $temp; - $swapped = false; + $swapped = true; } } - if ($swapped) { + if (!$swapped) { break; } } diff --git a/Strings/CheckAnagram.php b/Strings/CheckAnagram.php index 93ec37d3..151ec008 100644 --- a/Strings/CheckAnagram.php +++ b/Strings/CheckAnagram.php @@ -11,11 +11,13 @@ function isAnagram(string $originalString, string $testString, bool $caseInsensitive = true) { if ($caseInsensitive) { - $originalString = strtolower($originalString); // Converting string to lowercase for case-insensitive check + $originalString = strtolower($originalString); $testString = strtolower($testString); } - // count_chars(string, mode = 1) returns key-value pairs with character as key, frequency as value - // We can directly compare the arrays in this case + if (strlen($originalString) !== strlen($testString)) { + return false; + } + return count_chars($originalString, 1) === count_chars($testString, 1); }