- #include <stdio.h>
- #include <stdlib.h>
- int main() {
- char ch = ' ';
- int b = 0, count = 0, temp = 0;
- int a[10000];
- int flag = 0;
- printf("请输入一串数字,以空格分隔以回车结束\n");
- //数组的录入
- for (int i = 0; ; i++) {
- scanf("%d",&b);
- a[i] = b;
- scanf("%c",&ch);
- count++;
- if (ch == '\n') {
- break;
- }
- }
- for (int i = 0; i < count - 1; ++i) {
- for (int j = 0; j < count -1 - i; ++j) {
- //大数沉底
- if (a[j] > a[j + 1]) {
- temp = a[j];
- a[j] = a[j + 1];
- a[j + 1] = temp;
- //如果发生交换则记录位请0
- flag = 0;
- }
- //如果没有发生交换则记录位+1
- else{
- ++flag;
- }
- //如果在这趟中一次交换都没有发生,说明数组是排好顺序的,跳出内层for循环
- if (flag == (count - 1)) {
- break;
- }
- }
- //如果数组是排好的则跳出外层for循环
- if (flag == (count - 1)) {
- break;
- }
-
- }
-
- printf("排序后为:\n");
- for (int i = 0; i < count; i++) {
- printf("%d\t",a[i]);
- }
- return 0;
- }
复制代码 |
|