Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更好的理解建议 #67

Open
webflying opened this issue Sep 25, 2019 · 1 comment
Open

更好的理解建议 #67

webflying opened this issue Sep 25, 2019 · 1 comment

Comments

@webflying
Copy link

/**

  • BubbleSort
  • 冒泡排序,依次比较相邻的两个元素的大小,如果前面的大于后面的,那么交换两者位置
  • @param array $container
  • @return array
    */
    function BubbleSort(array $container)
    {
    $count = count($container);
    for ($j = 1; $j < $count; $j++) {
    for ($i = 0; $i < $count - $j; $i++) {
    if ($container[$i] > $container[$i + 1]) {
    $temp = $container[$i];
    $container[$i] = $container[$i + 1];
    $container[$i + 1] = $temp;
    }
    }
    $str = "第" . $j . "步排序结果";
    $res = $str . implode(',', $container);
    printf("%s\n",$res);
    }
    return $container;
    }

BubbleSort([4, 21, 41, 2, 53, 1, 213, 31, 21, 423]);

/*
第1步排序结果4,21,2,41,1,53,31,21,213,423
第2步排序结果4,2,21,1,41,31,21,53,213,423
第3步排序结果2,4,1,21,31,21,41,53,213,423
第4步排序结果2,1,4,21,21,31,41,53,213,423
第5步排序结果1,2,4,21,21,31,41,53,213,423
第6步排序结果1,2,4,21,21,31,41,53,213,423
第7步排序结果1,2,4,21,21,31,41,53,213,423
第8步排序结果1,2,4,21,21,31,41,53,213,423
第9步排序结果1,2,4,21,21,31,41,53,213,423
*/

@m9rco
Copy link
Owner

m9rco commented Sep 25, 2019

欢迎提交Pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants