欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php實(shí)現(xiàn)有序數(shù)組打印或排序的方法【附Python、C及Go語言實(shí)現(xiàn)代碼】

 更新時(shí)間:2016年11月10日 14:56:38   作者:smiling  
這篇文章主要介紹了php實(shí)現(xiàn)有序數(shù)組打印或排序的方法,涉及php針對(duì)數(shù)組的遍歷、判斷、構(gòu)造與合并等常用操作技巧,并附帶了Python、C及Go語言的實(shí)現(xiàn)代碼,需要的朋友可以參考下

本文實(shí)例講述了php實(shí)現(xiàn)有序數(shù)組打印或排序的方法。分享給大家供大家參考,具體如下:

有序的數(shù)組打印或排序?qū)τ趐hp來講非常的簡(jiǎn)單了這里整理了幾個(gè)不同語言的做法的實(shí)現(xiàn)代碼,具體的我們一起來看這篇php中有序的數(shù)組打印或排序的例子吧.

最近有個(gè)面試題挺火的——把2個(gè)有序的數(shù)組打印或排序,剛看到這個(gè)題的時(shí)候也有點(diǎn)蒙,最優(yōu)的算法肯定要用到有序的特性.

思考了一會(huì)發(fā)現(xiàn)也不是很難,假如數(shù)組是正序排列的,可以同時(shí)遍歷2個(gè)數(shù)組,將小的值進(jìn)行排序,最后會(huì)遍歷完一個(gè)數(shù)組,留下一個(gè)非空數(shù)組,而且剩下的值肯定大于等于已經(jīng)排好序的最大值.

PHP代碼:

<?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);
?>

運(yùn)行得到的新數(shù)組為:

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
)

其他語言實(shí)現(xiàn)代碼:

Python 代碼:

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代碼:

#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; //標(biāo)注a數(shù)組
  int j=0; //標(biāo)注b數(shù)組
  int m=0; //標(biāo)注新數(shù)組
  while (i&amp;lt;a_len&amp;amp;&amp;amp;j&amp;lt;b_len) { //重新排序 if(a[i]&amp;lt;b[j]) {
      temp[m++] = b[j++];
    } else {
      temp[m++] = a[i++];
    }
  }
  //將剩余的數(shù)字放在新數(shù)組后面(剩余的肯定是前面的數(shù)字大)
  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;
}

GO代碼:

復(fù)制代碼 代碼如下:
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 //標(biāo)注a數(shù)組
    var k = 0 //標(biāo)注b數(shù)組
    var h = 0 //標(biāo)注新數(shù)組
    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")
}

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP針對(duì)XML文件操作技巧總結(jié)》、《PHP錯(cuò)誤與異常處理方法總結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論