Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print_r($unsorted);
- $sorted = bubbleSorting1($unsorted);
- print_r($sorted);
- function bubbleSorting($data)
- {
- $array_length = count($data);
- $iterations_count = $array_length - 1;
- //обходим массив столько раз, сколько в нём элементов
- $i = 0;
- while($i < $array_length) {
- $sorted = FALSE;
- $y = 0;
- while ($y < $iterations_count) {
- //сравниваем соседние элементы массива
- if ($data[$y] > $data[$y + 1]) {
- $sorted = TRUE;
- //меняем их местами
- list($data[$y], $data[$y + 1]) = [
- $data[$y + 1],
- $data[$y]
- ];
- }
- $y++;
- }
- $iterations_count--;
- if (!$sorted) {
- //элементы не менялись местами, значит массив отсортирован
- return $data;
- }
- $i++;
- }
- return $data;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement