Php: how to print or sort ordered arrays [Python, C, and Go code] And pythongo
This article describes how to print or sort ordered arrays in php. We will share this with you for your reference. The details are as follows:
Ordered array printing or sorting is very simple for php. Here we have compiled the implementation code of several different languages, let's take a look at the example of printing or sorting ordered arrays in php.
Recently, an interview question was very popular. I printed or sorted two ordered arrays. I was a little confused when I first saw this question. The optimal algorithm must use the ordered feature.
After thinking about it for a while, it is not very difficult to find out. If the array is in the forward order, we can traverse two arrays at the same time, sort small values, and finally traverse an array, leave a non-empty array, and the remaining value must be greater than or equal to the maximum value 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 is as follows:
Array( [0] => 1 [1] => 2 [2] => 2 [3] => 3 [4] => 3 [5] => 4 [6] => 4 [7] => 5 [8] => 6 [9] => 10 [10] => 10 [11] => 10 [12] => 10)
Implementation Code in other languages:
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+1a = [1,2,3,4,5]b = [2,3,4,5,6,7,8,9]fib(a,b)
C code:
# Include & lt; stdio. h & gt; int * sort (int a [], int B [], int a_len, int B _len) {int * temp = malloc (a_len + B _len ); int I = 0; // annotation a array int j = 0; // annotation B array int m = 0; // annotation new array while (I & lt; a_len & amp; & amp; j & lt; B _len) {// re-order if (a [I] & lt; B [j]) {temp [m ++] = B [j ++];} else {temp [m ++] = a [I ++] ;}} // place the remaining number behind the new array (the remaining number must be large) if (I & lt; a_len) {for (; I & lt; a_len; I ++) {temp [m ++] = a [I] ;}} if (j & lt; B _len) {for (; j & lt; B _len; j ++) {temp [m ++] = B [j] ;}} return temp;} int main (int argc, const char * argv []) {int a [4] = {,}; int B [6] = {, 9, 10,}; int a_len = sizeof () /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 & lt; a_len + B _len; y ++) {printf ("% d", c [y]) ;}return 0 ;}
GO code:
Copy codeThe Code is 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 ()
Var len_ B = len (B)
Var c = make ([] int, len_a + len_ B)
Var j = 0 // annotate the Array
Var k = 0 // annotate the B Array
Var h = 0 // annotate the new array
For j & lt; len_a & amp; k & lt; len_ B {
If a [j] & gt; B [k] {
C [h] = B [k]
H ++
K ++
} Else {
C [h] = a [j]
H ++
J ++
}
}
If j & lt; len_a {
For I: = j; I & lt; len_a; I ++ {
C [h] = a [I]
H ++
}
}
If k & lt; len_ B {
For I: = k; I & lt; 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 ")
}