Example of sequential array printing or sorting in php-PHP source code

Source: Internet
Author: User
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. 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.

Script ec (2); script


A recent interview question is very popular-print or sort 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 the two arrays at the same time, sort the small values, and finally traverse an array, leave a non-empty array, and the remaining values must be greater than or equal to the maximum value in the sorted order.

PHP code

Function sort_arr ($ a, $ B ){
$ Temp = array ();
While ($ a & $ B ){
If ($ a ['0'] <$ B ['0']) {
$ Temp [] = array_shift ($ );
} Else {
$ Temp [] = array_shift ($ B );
}
}
If (! Empty ($ a) {$ temp = array_merge ($ temp, $ );}
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 );
Sort_arr ($ a, $ B );
?>

Python code

Def fib (a, B ):
Len_a = len ()
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 ():
C = c +

If len (B ):
C = c + B

I = 0
While I
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; // mark the Array
Int j = 0; // mark the B Array
Int m = 0; // mark the new array

While (I & lt; a_len & 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] = {2, 3, 11, 89 };
Int B [6] = };

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


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

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.