php實現有序數組列印或排序的方法【附Python、C及Go語言實現代碼】_php技巧

來源:互聯網
上載者:User

本文執行個體講述了php實現有序數組列印或排序的方法。分享給大家供大家參考,具體如下:

有序的數組列印或排序對於php來講非常的簡單了這裡整理了幾個不同語言的做法的實現代碼,具體的我們一起來看這篇php中有序的數組列印或排序的例子吧.

最近有個面試題挺火的——把2個有序的數組列印或排序,剛看到這個題的時候也有點蒙,最優的演算法肯定要用到有序的特性.

思考了一會發現也不是很難,假如數組是正序排列的,可以同時遍曆2個數組,將小的值進行排序,最後會遍曆完一個數組,留下一個非空數組,而且剩下的值肯定大於等於已經排好序的最大值.

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

運行得到的新數組為:

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)

其他語言實現代碼:

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+1a = [1,2,3,4,5]b = [2,3,4,5,6,7,8,9]fib(a,b)

C代碼:

#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; //標註a數組  int j=0; //標註b數組  int m=0; //標註新數組  while (i&lt;a_len&amp;&amp;j&lt;b_len) { //重新排序 if(a[i]&lt;b[j]) {      temp[m++] = b[j++];    } else {      temp[m++] = a[i++];    }  }  //將剩餘的數字放在新數組後面(剩餘的肯定是前面的數字大)  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代碼:

複製代碼 代碼如下:
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 //標註a數組
    var k = 0 //標註b數組
    var h = 0 //標註新數組
    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")
}

更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP數組(Array)操作技巧大全》、《php字串(string)用法總結》、《PHP針對XML檔案操作技巧總結》、《PHP錯誤與異常處理方法總結》、《PHP運算與運算子用法總結》、《PHP網路編程技巧總結》、《PHP基本文法入門教程》、《php物件導向程式設計入門教程》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家PHP程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.