Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
PHP - BubbleSort
<?php //php 7.0.8 $data = [1, 8, 5, 3, 1, 4, 2]; $n = sizeof($data); for ($i = $n-1; $i > 1; $i--) { // note: $i > 1 and $i = $n - 1 $flag = 0; for ($j = 0; $j < $i; $j++) { // note : $j < $i if ($data[$j] > $data[$j+1]) { $temp = $data[$j+1]; $data[$j+1] = $data[$j]; $data[$j] = $temp; $flag = 1; } } if ($flag == 0) break; // assume cases like already sorted, we no need to iterate again and again. the clue is if there is no swap happens, it means the array is sorted. } print_r($data); ?>
run
|
edit
|
history
|
help
2
net connector
mmd
Golobal keyword by rakib
PHP - Stack Implementation
daus
Tugas 1
PHP - BinarySearch (How-many-times-sorted-array-rotated)
basha
bobon
Compare value pairs