php实现有序数组打印或排序的方法【附Python、C及Go语言实现代码】

2025-05-29 0 101

本文实例讲述了php实现有序数组打印排序的方法。分享给大家供大家参考,具体如下:

有序的数组打印排序对于php来讲非常的简单了这里整理了几个不同语言的做法的实现代码,具体的我们一起来看这篇php中有序的数组打印排序的例子吧.

最近有个面试题挺火的——把2个有序的数组打印排序,刚看到这个题的时候也有点蒙,最优的算法肯定要用到有序的特性.

思考了一会发现也不是很难,假如数组是正序排列的,可以同时遍历2个数组,将小的值进行排序,最后会遍历完一个数组,留下一个非空数组,而且剩下的值肯定大于等于已经排好序的最大值.

PHP代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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

?>

运行得到的新数组为:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
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 代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19
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代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37
#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; //标注a数组

int j=0; //标注b数组

int m=0; //标注新数组

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++];

}

}

//将剩余的数字放在新数组后面(剩余的肯定是前面的数字大)

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

复制代码 代码如下:

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

希望本文所述对大家PHP程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 php实现有序数组打印或排序的方法【附Python、C及Go语言实现代码】 https://www.kuaiidc.com/96295.html

相关文章

发表评论
暂无评论