Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //napisać algorytm o złożoności obliczeniowej n sortujący rosnąco n-elementową tablicę
- //https://en.wikipedia.org/wiki/Sorting_algorithm
- //Simple pancake sort
- <?php
- function pancakeSort(&$arr)
- {
- $currentSize = count($arr);
- for ($currentSize; $currentSize > 1; --$currentSize) {
- $maxIndex = findMaxIndex($arr, $currentSize);
- if ($maxIndex != $currentSize - 1) {
- flip($arr, $maxIndex);
- flip($arr, $currentSize - 1);
- }
- }
- }
- function findMaxIndex($arr, $currentSize)
- {
- $maxIndex = 0;
- for ($i = 0; $i < $currentSize; ++$i) {
- if ($arr[$i] > $arr[$maxIndex]) {
- $maxIndex = $i;
- }
- }
- return $maxIndex;
- }
- function flip(&$arr, $i)
- {
- $start = 0;
- while ($start < $i) {
- $temp = $arr[$start];
- $arr[$start] = $arr[$i];
- $arr[$i] = $temp;
- $start++;
- $i--;
- }
- }
- $exampleArr = array(77, 6.5, 20, 11, 4.4, 21, 7);
- pancakeSort($exampleArr);
- var_dump($exampleArr);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement