How to sort an array in a single loop?

后端 未结 22 2957
面向向阳花
面向向阳花 2020-12-19 09:14

So I was going through different sorting algorithms. But almost all the sorting algorithms require 2 loops to sort the array. The time complexity of Bubble sort & Insert

22条回答
  •  情书的邮戳
    2020-12-19 09:39

    The following code is in PHP to sort an array best case possible. https://paiza.io/projects/r22X0VuHvPQ236jgkataxg

     $f){
                $gt [] = $a[$i];
            }else{
                $lt [] = $a[$i];
            }
        }
    
        return array_merge(quicksort($lt),array($f),quicksort($gt));
    }
    
    
    $ar = [7,4,3,6,5,1,2];
    echo "Input array => ".implode(' , ',$ar).'
    '; $a = quicksort($ar); echo "Output array => ".implode(' , ',$a);; ?>

提交回复
热议问题