PHP implementation of ordered array printing or sorting "with Python, C and Go language implementation code" _php tips

Source: Internet
Author: User
Tags exception handling php code php error php introduction

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 &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 a array
  int j=0;//callout b array
  int m=0;//Mark 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++];
    }
  Place the remaining digits behind the new array (the remaining number is definitely the front of the 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] = {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;
}

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 &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")
}

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.

Related Article

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.