Basic data structure implemented by PHP

xiaoxiao2021-04-05  243

// -------------------- // Basic Data Structure / / ------------------- / / Different Find (Find an element in an array) Function Bin_sch ($ ARRAY, $ Low, $ HIGH, $ K

) {IF ($ low <= $ HIGH

) {$ MID = INTVAL (($ LOW $ HIGH) / 2

); If ($ array [$ mid] == $ k

) {RETURN $ MID

Elseif ($ K <$ array [$ mid

]) {RETURN BIN_SCH ($ Array, $ Low, $ MID-1, $ K

} Else {return bin_sch ($ ARRAY, $ MID 1, $ HIGH, $ K

);}} Return -1

}

// Sequential Find (Find an element in the array) Function Seq_sch ($ Array, $ N, $ K)

) {$ ARRAY [$ N] = $ K

For For ($ I = 0; $ i <$ n; $ i

) {IF ($ Array [$ I] == $ K

) {Break;}} IF ($ i <$ n)

) {RETURN $ I

} Else {return -1

}}

// Linear Table Delete (Implementation in Array) FUNCTION DELETE_ARRAY_EEMENT ($ Array, $ I

) {$ Len = count ($ array

For ($ j = $ i; $ j <$ g; $ j

) {$ array [$ j] = $ array [$ j 1

]; Array_pop ($ Array

RETURN $ ARRAY

}

// Bubbling Sort (Array Sort) Function Bubble_Sort ($ Array

) {$ Count = count ($ array

); $ Count <= 0) Return False

For ($ I = 0; $ i <$ count; $ i

) {for ($ j = $ count-1; $ j> $ i; $ j

-) {IF ($ J]

]) {$ TMP = $ array [$ j

$ Array [$ j] = $ array [$ j-1

$ Array [$ J-1] = $ TMP

}}} Return $ array

}

// Quick Sort (Array Sort) Function Quicksort ($ Array

) {If ($ array) <= 1) Return $ array

; $ Key = $ array [0

$ Left_arr

= Array (); $ RIGHT_ARR

= Array (); for ($ I = 1; $ I

) {IF ($ ARRAY [$ I] <= $ e) $ left_arr [$] = $

]; Else $ right_arr [] = $ array [$ i

} $ L l =r = quicksort ($ Left_arr

$ Right_arr = Quicksort ($ RIGHT_ARR

); RETURN Array_Merge ($ Left_arr, Array ($ KEY), $ RIGHT_ARR

}

转载请注明原文地址:https://www.9cbs.com/read-132099.html

New Post(0)