{ "question_id": 7459691, "question_title": "冒泡法排序大小", "question_content": "4286\n3185\n2895\n3550\n2745\n按从小到大排序", "difficulty": "简单", "answer_id": 53444682, "answer_content": "
\n#include <stdio.h>\n \n#define ARR_LEN 255 /*数组长度上限*/\n#define elemType int /*元素类型*/\n \n/* 冒泡排序 */\n/* 1. 从当前元素起,向后依次比较每一对相邻元素,若逆序则交换 */\n/* 2. 对所有元素均重复以上步骤,直至最后一个元素 */\n/* elemType arr[]: 排序目标数组; int len: 元素个数 */\nvoid bubbleSort (elemType arr[], int len) {\n    elemType temp;\n    int i, j;\n    for (i=0; i<len-1; i++) /* 外循环为排序趟数,len个数进行len-1趟 */\n        for (j=0; j<len-1-i; j++) { /* 内循环为每趟比较的次数,第i趟比较len-i次 */\n            if (arr[j] > arr[j+1]) { /* 相邻元素比较,若逆序则交换(升序为左大于右,降序反之) */\n                temp = arr[j];\n                arr[j] = arr[j+1];\n                arr[j+1] = temp;\n            }\n        }\n}\n \nint main (void) {\n    elemType arr[ARR_LEN] = {4286,3185,2895,3550,2745};\n    int len = 5;\n    int i;\n     \n    bubbleSort (arr, len);\n    for (i=0; i<len; i++)\n        printf ("%d\\t", arr[i]);\n    putchar ('\\n');\n     \n    return 0;\n}
\n\n

 

\n", "tag_name": "c语言", "cpp": "#include \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 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