An example of an ordered array print or sort in PHP

Source: Internet
Author: User


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 &amp;lt;stdio.h&amp;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&amp;lt;a_len&amp;amp;&amp;amp;j&amp;lt;b_len) {//Reorder if (A[i]&amp;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&amp;lt;a_len) {
for (; i&amp;lt;a_len; i++) {
temp[m++] = A[i];
}
}
if (J&amp;lt;b_len) {
for (; j&amp;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&amp;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 &amp;lt; Len_a &amp;amp;&amp;amp; K &amp;lt; Len_b {
If A[J] &amp;gt; B[k] {
C[H] = B[k]
h++
k++
} else {
C[H] = A[j]
h++
J + +
}
}

If J &amp;lt; len_a {
For i: = j; I &amp;lt; len_a; i++ {
C[H] = A[i]
h++
}
}

If K &amp;lt; Len_b {
For i: = k; I &amp;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")
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.