{
  "question_id": 7459691,
  "question_title": "冒泡法排序大小",
  "question_content": "4286\n3185\n2895\n3550\n2745\n按从小到大排序",
  "difficulty": "简单",
  "answer_id": 53444682,
  "answer_content": "<pre>\n<code>#include &lt;stdio.h&gt;\n \n#define ARR_LEN 255 /*数组长度上限*/\n#define elemType int /*元素类型*/\n \n/* 冒泡排序 */\n/* 1. 从当前元素起&#xff0c;向后依次比较每一对相邻元素&#xff0c;若逆序则交换 */\n/* 2. 对所有元素均重复以上步骤&#xff0c;直至最后一个元素 */\n/* elemType arr[]: 排序目标数组; int len: 元素个数 */\nvoid bubbleSort (elemType arr[], int len) {\n    elemType temp;\n    int i, j;\n    for (i&#61;0; i&lt;len-1; i&#43;&#43;) /* 外循环为排序趟数&#xff0c;len个数进行len-1趟 */\n        for (j&#61;0; j&lt;len-1-i; j&#43;&#43;) { /* 内循环为每趟比较的次数&#xff0c;第i趟比较len-i次 */\n            if (arr[j] &gt; arr[j&#43;1]) { /* 相邻元素比较&#xff0c;若逆序则交换&#xff08;升序为左大于右&#xff0c;降序反之&#xff09; */\n                temp &#61; arr[j];\n                arr[j] &#61; arr[j&#43;1];\n                arr[j&#43;1] &#61; temp;\n            }\n        }\n}\n \nint main (void) {\n    elemType arr[ARR_LEN] &#61; {4286,3185,2895,3550,2745};\n    int len &#61; 5;\n    int i;\n     \n    bubbleSort (arr, len);\n    for (i&#61;0; i&lt;len; i&#43;&#43;)\n        printf (&#34;%d\\t&#34;, arr[i]);\n    putchar (&#39;\\n&#39;);\n     \n    return 0;\n}</code></pre>\n\n<p> </p>\n",
  "tag_name": "c语言",
  "cpp": "#include <stdio.h>\n#define ARR_LEN 255 \n#define elemType int \nvoid bubbleSort (elemType arr[], int len) {\n\telemType temp;\n\tint i, j;\n\tfor (i=0; i<len-1; i++) \n\t\tfor (j=0; j<len-1-i; j++) { \n\t\t\tif (arr[j] > arr[j+1]) { \n\t\t\t\ttemp = arr[j];\n\t\t\t\tarr[j] = arr[j+1];\n\t\t\t\tarr[j+1] = temp;\n\t\t\t}\n\t\t}\n}\nint main (void) {\n\telemType arr[ARR_LEN] = {4286,3185,2895,3550,2745};\n\tint len = 5;\n\tint i;\n\tbubbleSort (arr, len);\n\tfor (i=0; i<len; i++)\n\t\tprintf (\"%d\\t\", arr[i]);\n\tputchar ('\\n');\n\treturn 0;\n}",
  "topic_link": "https://bbs.csdn.net/topics/600470243",
  "status": 1,
  "keywords": "桶排序,算法初阶,线性时间排序,排序和顺序统计量",
  "license": "csdn.net",
  "notebook": {
    "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/4.ipynb?type=file"
  },
  "notebook_enable": 1,
  "author": "gechenyilie"
}