Quick actions

cmd+k|ctrl+k

Navigation

Languages

ข้อ 4

Snippet info

Language

Php

Visibility

public

Author

64160267

Created

2023-09-28T08:02:26.917215Z

Updated

2023-09-28T09:04:56.788416Z

<?php 
//64160267 Kanokpol Promtha
// Insertion Sort
function exam04($arry){
    $count = 0;
    for($i = 0; $i < count($arry); $i++) {
        $tmp = $arry[$i];
        $j = $i - 1;
        while($j >= 0 && $arry[$j] > $tmp) {
            $arry[$j + 1] = $arry[$j];
            $j = $j - 1;
            // count sum
            $count++;
        }
        $arry[$j + 1] = $tmp;
    }
    return $count;
}

function test_01(){
    if(exam04([2, 1, 3, 1, 2]) == 4){
        echo "pass";
    } else {
        echo "fail";
    }
}

test_01();
INFO