Skip to content

Commit

Permalink
Merge pull request #317 from Kgosavi-dev/hacktoberfest_2024
Browse files Browse the repository at this point in the history
commited for added bubble sort program
  • Loading branch information
sudhanshu-77 authored Oct 30, 2024
2 parents ca1a9c4 + e448b5e commit 15bee2d
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions bubble_sort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

function bubbleSort($arr) {
$n = count($arr);

// Traverse through all array elements
for ($i = 0; $i < $n - 1; $i++) {
for ($j = 0; $j < $n - $i - 1; $j++) {

// Swap if the element found is
// greater than the next element
if ($arr[$j] > $arr[$j + 1]) {
$temp = $arr[$j];
$arr[$j] = $arr[$j + 1];
$arr[$j + 1] = $temp;
}
}
}

return $arr;
}

// Driver code
$arr = [61, 33, 22, 14, 27, 15, 92];
$sortedArray = bubbleSort($arr);

// Print the sorted array
echo "Sorted array: "
. implode(", ", $sortedArray);

?>

0 comments on commit 15bee2d

Please sign in to comment.