{"id":211924,"date":"2021-02-28T09:26:23","date_gmt":"2021-02-28T01:26:23","guid":{"rendered":"https:\/\/gulass.cn\/?p=211924"},"modified":"2021-02-23T16:28:54","modified_gmt":"2021-02-23T08:28:54","slug":"introduction-to-bubble","status":"publish","type":"post","link":"https:\/\/gulass.cn\/introduction-to-bubble.html","title":{"rendered":"\u5192\u6ce1\u6392\u5e8f\u7b80\u4ecb\u53ca\u5176\u5e94\u7528"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5192\u6ce1\u6392\u5e8f\uff08Bubble Sort\uff09\u4e5f\u662f\u4e00\u79cd\u7b80\u5355\u76f4\u89c2\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u5b83\u91cd\u590d\u5730\u8d70\u8bbf\u8fc7\u8981\u6392\u5e8f\u7684\u6570\u5217\uff0c\u4e00\u6b21\u6bd4\u8f83\u4e24\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u4ed6\u4eec\u7684\u987a\u5e8f\u9519\u8bef\u5c31\u628a\u4ed6\u4eec\u4ea4\u6362\u8fc7\u6765\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u8d70\u8bbf\u6570\u5217\u7684\u5de5\u4f5c\u662f\u91cd\u590d\u5730\u8fdb\u884c\u76f4\u5230\u6ca1\u6709\u518d\u9700\u8981\u4ea4\u6362\uff0c\u4e5f\u5c31\u662f\u8bf4\u8be5\u6570\u5217\u5df2\u7ecf\u6392\u5e8f\u5b8c\u6210\u3002\u8fd9\u4e2a\u7b97\u6cd5\u7684\u540d\u5b57\u7531\u6765\u662f\u56e0\u4e3a\u8d8a\u5c0f\u7684\u5143\u7d20\u4f1a\u7ecf\u7531\u4ea4\u6362\u6162\u6162\"\u6d6e\"\u5230\u6570\u5217\u7684\u9876\u7aef\u3002<\/p>\n

\u4f5c\u4e3a\u6700\u7b80\u5355\u7684\u6392\u5e8f\u7b97\u6cd5\u4e4b\u4e00\uff0c\u5192\u6ce1\u6392\u5e8f\u7ed9\u6211\u7684\u611f\u89c9\u5c31\u50cf Abandon \u5728\u5355\u8bcd\u4e66\u91cc\u51fa\u73b0\u7684\u611f\u89c9\u4e00\u6837\uff0c\u6bcf\u6b21\u90fd\u5728\u7b2c\u4e00\u9875\u7b2c\u4e00\u4f4d\uff0c\u6240\u4ee5\u6700\u719f\u6089\u3002\u5192\u6ce1\u6392\u5e8f\u8fd8\u6709\u4e00\u79cd\u4f18\u5316\u7b97\u6cd5\uff0c\u5c31\u662f\u7acb\u4e00\u4e2a flag\uff0c\u5f53\u5728\u4e00\u8d9f\u5e8f\u5217\u904d\u5386\u4e2d\u5143\u7d20\u6ca1\u6709\u53d1\u751f\u4ea4\u6362\uff0c\u5219\u8bc1\u660e\u8be5\u5e8f\u5217\u5df2\u7ecf\u6709\u5e8f\u3002\u4f46\u8fd9\u79cd\u6539\u8fdb\u5bf9\u4e8e\u63d0\u5347\u6027\u80fd\u6765\u8bf4\u5e76\u6ca1\u6709\u4ec0\u4e48\u592a\u5927\u4f5c\u7528\u3002<\/p>\n

\u7b97\u6cd5\u6b65\u9aa4<\/strong><\/div>\n

\u6bd4\u8f83\u76f8\u90bb\u7684\u5143\u7d20\u3002\u5982\u679c\u7b2c\u4e00\u4e2a\u6bd4\u7b2c\u4e8c\u4e2a\u5927\uff0c\u5c31\u4ea4\u6362\u4ed6\u4eec\u4e24\u4e2a\u3002<\/p>\n

\u5bf9\u6bcf\u4e00\u5bf9\u76f8\u90bb\u5143\u7d20\u4f5c\u540c\u6837\u7684\u5de5\u4f5c\uff0c\u4ece\u5f00\u59cb\u7b2c\u4e00\u5bf9\u5230\u7ed3\u5c3e\u7684\u6700\u540e\u4e00\u5bf9\u3002\u8fd9\u6b65\u505a\u5b8c\u540e\uff0c\u6700\u540e\u7684\u5143\u7d20\u4f1a\u662f\u6700\u5927\u7684\u6570\u3002<\/p>\n

\u9488\u5bf9\u6240\u6709\u7684\u5143\u7d20\u91cd\u590d\u4ee5\u4e0a\u7684\u6b65\u9aa4\uff0c\u9664\u4e86\u6700\u540e\u4e00\u4e2a\u3002<\/p>\n

\u6301\u7eed\u6bcf\u6b21\u5bf9\u8d8a\u6765\u8d8a\u5c11\u7684\u5143\u7d20\u91cd\u590d\u4e0a\u9762\u7684\u6b65\u9aa4\uff0c\u76f4\u5230\u6ca1\u6709\u4efb\u4f55\u4e00\u5bf9\u6570\u5b57\u9700\u8981\u6bd4\u8f83\u3002<\/p>\n

\u52a8\u56fe\u6f14\u793a<\/strong><\/div>\n

\"\"<\/p>\n

\u4ec0\u4e48\u65f6\u5019\u6700\u5feb<\/strong><\/div>\n

\u5f53\u8f93\u5165\u7684\u6570\u636e\u5df2\u7ecf\u662f\u6b63\u5e8f\u65f6\uff08\u90fd\u5df2\u7ecf\u662f\u6b63\u5e8f\u4e86\uff0c\u6211\u8fd8\u8981\u4f60\u5192\u6ce1\u6392\u5e8f\u6709\u4f55\u7528\u554a\uff09\u3002<\/p>\n

\u4ec0\u4e48\u65f6\u5019\u6700\u6162<\/strong><\/div>\n

\u5f53\u8f93\u5165\u7684\u6570\u636e\u662f\u53cd\u5e8f\u65f6\uff08\u5199\u4e00\u4e2a for \u5faa\u73af\u53cd\u5e8f\u8f93\u51fa\u6570\u636e\u4e0d\u5c31\u884c\u4e86\uff0c\u5e72\u561b\u8981\u7528\u4f60\u5192\u6ce1\u6392\u5e8f\u5462\uff0c\u6211\u662f\u95f2\u7684\u5417\uff09\u3002<\/p>\n

JavaScript \u4ee3\u7801\u5b9e\u73b0<\/strong><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

function bubbleSort(arr) {\r\n    var len = arr.length;\r\n    for (var i = 0; i < len - 1; i++) {\r\n        for (var j = 0; j < len - 1 - i; j++) {\r\n            if (arr[j] > arr[j+1]) {        \/\/ \u76f8\u90bb\u5143\u7d20\u4e24\u4e24\u5bf9\u6bd4\r\n                var temp = arr[j+1];        \/\/ \u5143\u7d20\u4ea4\u6362\r\n                arr[j+1] = arr[j];\r\n                arr[j] = temp;\r\n            }\r\n        }\r\n    }\r\n    return arr;\r\n}<\/pre>\n
Python \u4ee3\u7801\u5b9e\u73b0<\/strong><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

def bubbleSort(arr):\r\n    for i in range(1, len(arr)):\r\n        for j in range(0, len(arr)-i):\r\n            if arr[j] > arr[j+1]:\r\n                arr[j], arr[j + 1] = arr[j + 1], arr[j]\r\n    return arr<\/pre>\n
Go \u4ee3\u7801\u5b9e\u73b0<\/strong><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

func bubbleSort(arr []int) []int {\r\n        length := len(arr)\r\n        for i := 0; i < length; i++ {\r\n                for j := 0; j < length-1-i; j++ {\r\n                        if arr[j] > arr[j+1] {\r\n                                arr[j], arr[j+1] = arr[j+1], arr[j]\r\n                        }\r\n                }\r\n        }\r\n        return arr\r\n}<\/pre>\n
Java \u4ee3\u7801\u5b9e\u73b0<\/strong><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

public class BubbleSort implements IArraySort {\r\n\r\n    @Override\r\n    public int[] sort(int[] sourceArray) throws Exception {\r\n        \/\/ \u5bf9 arr \u8fdb\u884c\u62f7\u8d1d\uff0c\u4e0d\u6539\u53d8\u53c2\u6570\u5185\u5bb9\r\n        int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);\r\n\r\n        for (int i = 1; i < arr.length; i++) {\r\n            \/\/ \u8bbe\u5b9a\u4e00\u4e2a\u6807\u8bb0\uff0c\u82e5\u4e3atrue\uff0c\u5219\u8868\u793a\u6b64\u6b21\u5faa\u73af\u6ca1\u6709\u8fdb\u884c\u4ea4\u6362\uff0c\u4e5f\u5c31\u662f\u5f85\u6392\u5e8f\u5217\u5df2\u7ecf\u6709\u5e8f\uff0c\u6392\u5e8f\u5df2\u7ecf\u5b8c\u6210\u3002\r\n            boolean flag = true;\r\n\r\n            for (int j = 0; j < arr.length - i; j++) {\r\n                if (arr[j] > arr[j + 1]) {\r\n                    int tmp = arr[j];\r\n                    arr[j] = arr[j + 1];\r\n                    arr[j + 1] = tmp;\r\n\r\n                    flag = false;\r\n                }\r\n            }\r\n\r\n            if (flag) {\r\n                break;\r\n            }\r\n        }\r\n        return arr;\r\n    }\r\n}<\/pre>\n
PHP \u4ee3\u7801\u5b9e\u73b0<\/strong><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

function bubbleSort($arr)\r\n{\r\n    $len = count($arr);\r\n    for ($i = 0; $i < $len - 1; $i++) {\r\n        for ($j = 0; $j < $len - 1 - $i; $j++) {\r\n            if ($arr[$j] > $arr[$j+1]) {\r\n                $tmp = $arr[$j];\r\n                $arr[$j] = $arr[$j+1];\r\n                $arr[$j+1] = $tmp;\r\n            }\r\n        }\r\n    }\r\n    return $arr;\r\n}<\/pre>\n
C \u8bed\u8a00<\/strong><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

#include \r\nvoid bubble_sort(int arr[], int len) {\r\n        int i, j, temp;\r\n        for (i = 0; i < len - 1; i++)\r\n                for (j = 0; j < len - 1 - i; j++)\r\n                        if (arr[j] > arr[j + 1]) {\r\n                                temp = arr[j];\r\n                                arr[j] = arr[j + 1];\r\n                                arr[j + 1] = temp;\r\n                        }\r\n}\r\nint main() {\r\n        int arr[] = { 22, 34, 3, 32, 82, 55, 89, 50, 37, 5, 64, 35, 9, 70 };\r\n        int len = (int) sizeof(arr) \/ sizeof(*arr);\r\n        bubble_sort(arr, len);\r\n        int i;\r\n        for (i = 0; i < len; i++)\r\n                printf(\"%d \", arr[i]);\r\n        return 0;\r\n}<\/pre>\n
C++ \u8bed\u8a00<\/strong><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

#include \r\nusing namespace std;\r\ntemplate \/\/\u6574\u6570\u6216\u6d6e\u70b9\u6570\u7686\u53ef\u4f7f\u7528,\u82e5\u8981\u4f7f\u7528\u7c7b(class)\u6216\u7ed3\u6784\u4f53(struct)\u65f6\u5fc5\u987b\u91cd\u8f7d\u5927\u4e8e(>)\u8fd0\u7b97\u7b26\r\nvoid bubble_sort(T arr[], int len) {\r\n        int i, j;\r\n        for (i = 0; i < len - 1; i++)\r\n                for (j = 0; j < len - 1 - i; j++)\r\n                        if (arr[j] > arr[j + 1])\r\n                                swap(arr[j], arr[j + 1]);\r\n}\r\nint main() {\r\n        int arr[] = { 61, 17, 29, 22, 34, 60, 72, 21, 50, 1, 62 };\r\n        int len = (int) sizeof(arr) \/ sizeof(*arr);\r\n        bubble_sort(arr, len);\r\n        for (int i = 0; i < len; i++)\r\n                cout << arr[i] << ' ';\r\n        cout << endl;\r\n        float arrf[] = { 17.5, 19.1, 0.6, 1.9, 10.5, 12.4, 3.8, 19.7, 1.5, 25.4, 28.6, 4.4, 23.8, 5.4 };\r\n        len = (float) sizeof(arrf) \/ sizeof(*arrf);\r\n        bubble_sort(arrf, len);\r\n        for (int i = 0; i < len; i++)\r\n                cout << arrf[i] << ' '<<\/iostream><\/pre>\n
C#<\/strong><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

static void BubbleSort(int[] intArray) {\r\n    int temp = 0;\r\n    bool swapped;\r\n    for (int i = 0; i < intArray.Length; i++)\r\n    {\r\n        swapped = false;\r\n        for (int j = 0; j < intArray.Length - 1 - i; j++)\r\n            if (intArray[j] > intArray[j + 1])\r\n            {\r\n                temp = intArray[j];\r\n                intArray[j] = intArray[j + 1];\r\n                intArray[j + 1] = temp;\r\n                if (!swapped)\r\n                    swapped = true;\r\n            }\r\n        if (!swapped)\r\n            return;\r\n    }\r\n}<\/pre>\n
Ruby<\/strong><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

class Array\r\n  def bubble_sort!\r\n    for i in 0...(size - 1)\r\n      for j in 0...(size - i - 1)\r\n        self[j], self[j + 1] = self[j + 1], self[j] if self[j] > self[j + 1]\r\n      end\r\n    end\r\n    self\r\n  end\r\nend\r\n\r\nputs [22, 34, 3, 32, 82, 55, 89, 50, 37, 5, 64, 35, 9, 70].bubble_sort!<\/pre>\n
Swift<\/strong><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

import Foundation\r\n\r\nfunc bubbleSort (arr: inout [Int]) {\r\n    for i in 0.. arr[j+1] {\r\n                arr.swapAt(j, j+1)\r\n            }\r\n        }\r\n    }\r\n}\r\n\r\n\/\/ \u6d4b\u8bd5\u8c03\u7528\r\n\r\nfunc testSort () {\r\n    \/\/ \u751f\u6210\u968f\u673a\u6570\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u64cd\u4f5c\r\n    var list:[Int] = []\r\n    for _ in 0...99 {\r\n        list.append(Int(arc4random_uniform(100)))\r\n    }\r\n    print(\"\\(list)\")\r\n    bubbleSort(arr:&list)\r\n    print(\"\\(list)\")\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u8d70\u8bbf\u6570\u5217\u7684\u5de5\u4f5c\u662f\u91cd\u590d\u5730\u8fdb\u884c\u76f4\u5230\u6ca1\u6709\u518d\u9700\u8981\u4ea4\u6362\uff0c\u4e5f\u5c31\u662f\u8bf4\u8be5\u6570\u5217\u5df2\u7ecf\u6392\u5e8f\u5b8c\u6210\u3002\u8fd9\u4e2a\u7b97\u6cd5\u7684\u540d\u5b57\u7531\u6765\u662f\u56e0\u4e3a\u8d8a\u5c0f\u7684\u5143\u7d20\u4f1a […]<\/p>\n","protected":false},"author":1481,"featured_media":211927,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-211924","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/211924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=211924"}],"version-history":[{"count":4,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/211924\/revisions"}],"predecessor-version":[{"id":211995,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/211924\/revisions\/211995"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/211927"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=211924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=211924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=211924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}