Here is an example implementation of the Bubble sort algorithm in PHP:
main.php437 chars19 lines
The bubbleSort
function takes an array as an input and sorts it in ascending order using the Bubble sort algorithm. The function first gets the length of the array and then uses two nested loops to compare adjacent elements and swap them if they are in the wrong order. After each complete iteration through the array, the highest unsorted element "bubbles up" to its correct position in the sorted portion of the array. The function then returns the sorted array.
Note: Bubble sort is not an efficient algorithm when compared to other sorting algorithms like quicksort or mergesort, but it is simple and useful for small inputs or as a teaching tool.
gistlibby LogSnag