int main() { int n, m; scanf("%d", &n); int arr[n]; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } scanf("%d", &m); int end = n; int i = 0, j = 0; for (i = 0; i < n; i++) { if (m >= arr[i] && m < arr[i + 1]) { for (j = end; j > i; j--) { arr[j + 1] = arr[j]; } arr[i + 1] = m; break; } else if (m > arr[n - 1]) { arr[end] = m; } else if (m < arr[0]) { for (j = end; j >= 0; j--) { arr[j + 1] = arr[j]; } arr[0] = m; } } for (int i = 0; i < n + 1; i++) { printf("%d ", arr[i]); } return 0; }
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <malloc.h> int main() { int n = 0; int same_n = 0; int* p = NULL; int tem = 0; int i = 0; scanf("%d", &n); //开辟空间,用来存放数组 p = (int*)malloc(sizeof(int) * (n + 1)); if (!p) { perror("malloc"); return 1; } //录入有序数组 for (i = 0; i < n; i++) { scanf("%d", p + i); } //录入待插入的整数 scanf("%d", &tem); //寻找待插入的位置 for (i = 0; i < n; i++) { if (tem < *(p + i)) { //此时的i,就是tem应该在数组里的位置(下标) break; } } //进行插入操作 same_n = n; while (same_n != i) { //数组从后往前拉 *(p + same_n) = *(p + same_n - 1); same_n--; } *(p + i) = tem; //输出结果 for (i = 0; i <= n; i++) { printf("%d ", *(p + i)); } //释放空间 free(p); p = NULL; return 0; }
#include <stdio.h> int main() { int n = 0; int arr[60] = {0}; while (~scanf("%d", &n))//输入有序数的个数 { int i = 0; for (i = 0; i < n; i++) { scanf("%d", &arr[i]);//输入有序数 } int x = 0; scanf("%d", &x);//要插入的数 for (i = 0; i < n; i++) { if (x < arr[0]) //当小于此组所有的有序数 { for (i = 0; i < n; i++) { arr[n - i] = arr[n - 1 - i]; } arr[0] = x; } else if (x > arr[i] && x < arr[i + 1]) { for (int j = n - 1; j > i; j--) { arr[j + 1] = arr[j]; } arr[i + 1] = x; } else if (x > arr[n - 1]) //当大于此组有序数的所有数 { arr[n] = x; } } for (int k = 0; k <= n; k++) { printf("%d ", arr[k]); } } return 0; }
#include <stdio.h> int main() { int n = 0, i = 0, temp, ret; int arr[52] = {0}; while (scanf("%d", &n) != EOF) { arr[i++] = n; //将所有的数都读入到数组中 //arr[0] - 数组的个数,arr[arr[0]+1] - 待插入的数 } //将arr[count]的数插入到1~count-1的数组中 for (i = 1; i <= arr[0]; i++) { if (arr[arr[0] + 1] < arr[i]) break; //此时i的值为待插入的下标 } ret = arr[arr[0] + 1]; //保存待插入的数 for (int j = arr[0] + 1; j > i; j--) { arr[j] = arr[j - 1]; } arr[i] = ret; for (int i = 1; i <= arr[0] + 1; i++) { printf("%d ", arr[i]); } return 0; }
#include <stdio.h> int main() { int n = 0; scanf("%d", &n); int arr[n]; int i = 0; for (i = 0; i < n; i++) { scanf("%d ", &arr[i]); } int count = 0; int flag = 0; scanf("%d", &count); for (i = 0; i < n; i++) { if (count < arr[i]) { flag++; printf("%d ", count); count = arr[n - 1] + 1; //被赋予最大值 } printf("%d ", arr[i]); } if (flag == 0) //此时 升序数组只有一个,在打印count; { printf("%d ", count); } return 0; }
int main() { int n = 0; int arr[50] = { 0 }; scanf("%d", &n); int i = 0; int a = 0; for ( i = 0; i < n; i++) { scanf("%d", arr + i); } scanf("%d", &a); arr[n] = a; for (i = n; i > 0; i--) { if (arr[i]< arr[i-1]) { int temp = arr[i]; arr[i] = arr[i - 1]; arr[i - 1] = temp; } else { break; } } for (i = 0; i < n + 1; i++) { printf("%d ", arr[i]); } return 0; }
#include <stdio.h> void bubble_sort(int arr[], int sz) { int i = 0; int j = 0; int tmp = 0; for (i = 0; i < sz; i++) { for (j = 0; j < sz - i - 1; j++) { if (arr[j] > arr[j + 1]) { tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } } } } int main() { int N = 0; scanf("%d", &N); int i = 0; int arr[51] = {0}; for (i = 0; i < N; i++) scanf("%d", &arr[i]); scanf("%d", &arr[N]); bubble_sort(arr, N + 1); for (i = 0; i < N + 1; i++) printf("%d ", arr[i]); printf("\n"); return 0; }
#include<stdio.h> int main() { int n = 0; scanf("%d",&n); int arr[50]; int i = 0; for(i = 0;i<n;i++) { scanf("%d ",&arr[i]); } int x = 0; scanf("%d ",&x); int ret = 0;//放被插入位置的原元素 int j = 0; for(i = 0;i<n+1;i++) { if(x>=arr[i]&&x<=arr[i+1])//找到插入的位置i+1 { for(j = n;j>i;j--)//接下来把这个元素后面的元素都向后挪一个 { arr[j] = arr[j-1];//从最后一个开始挪 } arr[i+1] = x; i++; } } if(x<arr[0]) { int j = 0; ret = arr[0]; arr[0] = x;//arr[1]需要空出来 for(j = n;j>1;j--)//接下来把这个元素后面的元素都向后挪一个 { arr[j] = arr[j-1];//从最后一个开始挪 } arr[1] = ret;//把被插入位置的原元素放进去 } if(x>arr[n-1]) { arr[n] = x; } for(i = 0;i<=n;i++) { printf("%d ",arr[i]); } return 0; }
#include <stdio.h> int main(void) { int num, n, index; scanf("%d", &n); int arr[n + 1]; for (int i = 0; i < n; i++) scanf("%d", &arr[i]); scanf("%d", &num); n++; for (int i = 0; i < n; i++) { if (arr[i] > num) { index = i; break; } } for (int i = n; i > index; i--) arr[i] = arr[i - 1]; arr[index] = num; for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; }
#include<stdio.h> int main() { int b = 0; int c[100] = { 0 }; scanf("%d", &b);//输入一个数,表示一共有b个数; for (int i = 0; i < b; i++) { scanf("%d", &c[i]);//输入一个有序数组 } int a = 0; scanf("%d", &a); for (int i = b; i >=0; i--)//从最后一个开始比较 { if (a>c[i-1]) { c[i] = a;//如果大于最后一个数,直接赋值,程序循环结束,打印数组 break; } else {//如果小,那就交换 c[i] = c[i - 1]; c[i - 1] = a; } } for (int i = 0; i < b + 1; i++)//打印最后的数组 { printf("%d ", c[i]); } return 0; }