This article illustrates the way in which PHP implements ordered array printing or sorting. Share to everyone for your reference, specific as follows:
Ordered array printing or sorting is very simple for PHP. Here is a few different languages of the implementation code, we look at this PHP in an orderly array of print or sorting examples.
Recently have a face test is very hot--to 2 ordered array printing or sorting, just see this problem is also a bit of a mask, the optimal algorithm must use the orderly characteristics.
Think for a while it is not difficult to find, if the array is a positive sequence, you can traverse 2 of the array, the small values are sorted, the end will go through an array, leaving a non-empty array, and the rest of the value is definitely greater than the order of the maximum value is already sorted.
PHP Code:
<?php
function Sort_arr ($a, $b) {
$temp = array ();
while ($a && $b) {
if ($a [' 0 ']< $b [' 0 ']) {
$temp [] = Array_shift ($a);
} else {
$temp [] = Array_ Shift ($b);
}
}
if (!emptyempty ($a)) {$temp = Array_merge ($temp, $a);
if (!emptyempty ($b)) {$temp = Array_merge ($temp, $b);
return $temp;
}
$a = array (1,2,3,4,5,6);
$b = Array (2,3,4,10,10,10,10);
Sort_arr ($a, $b);
>
The new array to run is:
Array
(
[0] => 1
[1] => 2
[2] => 2
[3] => 3
[4] => 3
[5] => 4
[6] => 4
[7] => 5
[8] => 6
[9] =>
[a] => [one
] =>
Other language Implementation code:
Python Code:
def fib (a,b):
len_a = Len (a)
C = [] While
Len (a) and Len (b):
if a[0] > b[0]:
c.append (B.pop (0 )
Else:
c.append (a.pop (0))
if Len (a):
c = C+a
If Len (b):
C = c+b i=0 while
i <len (c):
print (c[i])
i = i+1
a = [1,2,3,4,5]
b = [2,3,4,5,6,7,8,9]
fib (a,b)
C Code:
#include <stdio.h>;
int *sort (int a[], int b[], int a_len, int b_len) {
int *temp = malloc (A_len+b_len);
int i=0; Callout a array
int j=0;//callout b array
int m=0;//Mark new array while
(I<a_len&&j<b_len) {//Reorder if (A[i]<b[j]) {
temp[m++] = b[j++];
} else {
temp[m++] = a[i++];
}
Place the remaining digits behind the new array (the remaining number is definitely the front of the Large)
if (I<a_len) {for
(; i<a_len; i++) {
temp[m++] = A[i];
}
}
if (J<b_len) {for
(; j<b_len; j) {
temp[m++] = b[j];
}
return temp;
}
int main (int argc, const char * argv[]) {
int a[4] = {2,3,11,89};
int b[6] = {4,6,9,10,22,55};
int a_len = sizeof (a)/sizeof (a[0));
int b_len = sizeof (b)/sizeof (b[0));
int *c = sort (A, B, A_len, B_len);
int y = 0;
for (; y<a_len+b_len; y++) {
printf ("%d", C[y]);
return 0;
}
Go code:
Copy Code code as follows:
Package Main
Import "FMT"
Func Main () {
var a = [5]int{1, 2, 3, 4, 5}
var b = [8]int{4, 5, 6, 7, 89, 100, 111, 112}
var len_a = Len (a)
var len_b = len (b)
var c = Make ([]int, Len_a+len_b)
var j = 0//callout Array
var k = 0//callout b Array
var h = 0//Mark new array
for J < Len_a && K < Len_b {
If A[J] > B[k] {
C[H] = B[k]
h++
k++
} else {
C[H] = A[j]
h++
J + +
}
}
If J < len_a {
For i: = j; I < len_a; i++ {
C[H] = A[i]
h++
}
}
If K < Len_b {
For i: = k; I < Len_b; i++ {
C[H] = B[i]
h++
}
}
Print (C, "C")
}
/**
* [Print Array]
* @param {[type]} o []int [array]
* @param {[type]} name string [array name]
*/
Func Print (o []int, name string) {
Fmt. Printf ("%s:", name)
For _, V: = range O {
Fmt. Printf ("%d", V)
}
Fmt. Printf ("\ n")
}
More interested in PHP related content readers can view the site topics: "PHP array" Operation Tips Encyclopedia, "PHP string (String) Usage summary", "PHP for XML file Operation skills Summary", "PHP error and Exception handling method summary", " Summary of PHP operation and operator usage, "Summary of PHP Network programming skills", "Introduction to PHP Basic Grammar", "PHP Introduction to Object-oriented Programming", "Php+mysql Database Operation Introduction" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design.