There's been a very hot question recently--print or sort the two ordered arrays. Just see this problem when also a bit of Mongolia, the best algorithm must use the orderly characteristics.
Think of a will find it is not very difficult, if the array is a positive sequence, you can traverse the two arrays, the small value of the order, 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.
The 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 (!empty ($a)) {$temp = Array_merge ($temp, $a);
if (!empty ($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 Python code
Def fib (a,b):
len_a = Len (a)
c = []
while Len (a) a nd 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
& nbsp; 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 of
#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; Callout Array A
int j=0; Callout B Array
int m=0; Callout new Array
while (I&lt;a_len&amp;&amp;j&lt;b_len) {//Reorder if (A[i]&lt;b[j]) {
temp[m++] = b[j++];
} else {
temp[m++] = a[i++];
}
}
Put the remaining digits behind the new array (the remaining number must be the largest in the front)
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] = {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&lt;a_len+b_len; y++) {
printf ("%d", c[y]);
}
return 0;
}
The Go Code
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 &lt; Len_a &amp;&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")
}