-
喵喔喔
#include <stdio.h>#include <conio.h> #define LEN 100 /*数组长度上限*/#define elemType int /*元素类型*/ /*升序冒泡排序*//*参数说明:*//*int arr[]:排序目标数组*//*int len:元素个数*/void bubbleSortAsc (elemType arr[], int len) { elemType temp; int i, j; for (i=0; i<len-1; i++) /*外循环控制排序趟数,len-1个数进行len-1趟*/ for (j=0;j<len-1-i; j++) { /*内循环每趟比较的次数,第j趟比较len-j次*/ if (arr[j] > arr[j+1]) { /*相邻元素比较,逆序则交换*/ temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } }} /*打印数组*/void printArr (elemType arr[], int len) { int i; for (i=0; i<len; i++) printf ("%d\t",arr[i]); putchar ('\n');} int main (void) { elemType arr[LEN] = {3,5,1,7,4,9,6,8,10,4}; int len; len = 10; puts ("初始数组:"); printArr (arr,len); putchar ('\n'); bubbleSortAsc (arr, len); /*升序冒泡排序*/ printArr (arr,len); putchar ('\n'); getch (); /*屏幕暂留*/ return 0;}运行结果
-
当年话下
12345678910111213141516171819202122232425262728293031323334353637383940//希望我的回答对你的学习有帮助#include <stdio.h> #define N 3 //定义数组的大小,表示数组中可以放的个数 void Sort(int IntArray[N]) //冒泡算法{ for (int i = 0; i < N - 1; i++) { for (int j = 0; j < N - i - 1; j++) { int temp; if (IntArray[j] > IntArray[j + 1]) { temp = IntArray[j]; IntArray[j] = IntArray[j + 1]; IntArray[j + 1] = temp; } } }} int main(){ int IntArray_T[N] = {}; for (int i = 0; i < N; i++) //输入 { scanf("%d", &IntArray_T[i]); } Sort(IntArray_T); for (int i = 0; i < N; i++) //输出 { printf("%4d", IntArray_T[i]); } return 0;}
-
蝴蝶刀刀
12345678910111213141516171819202122232425262728293031#include <stdio.h> void sort(int a[], int n) {//选择排序 int i,j,k,t; for(i = 0; i < n - 1; ++i) { k = i; for(j = k + 1; j < n; ++j) { if(a[k] > a[j]) k = j; } if(k != i) { t = a[i]; a[i] = a[k]; a[k] = t; } }} int main() { int a[] = {21,16,30,21,8,19,33,26,28,27,24,50,13,12}; int i,n = sizeof(a)/sizeof(a[0]); printf("排序前:\n"); for(i = 0; i < n; ++i) printf("%d ",a[i]); printf("\n"); sort(a,n); printf("排序后:\n"); for(i = 0; i < n; ++i) printf("%d ",a[i]); printf("\n"); return 0;}
-
哔哔one
#include <stdio.h>void sort(int a[],int n)//选择排序{int i,j,t,temp;for(i=0;i<n-1;i++){t=i;for(j=i+1;j<n;j++){if(a[t]>a[j]){t=j;}}if(t!=i){temp = a[i];a[i] = a[t];a[t] = temp;}}}void main(){int a[4];int i;for(i=0;i<4;i++){scanf("%d",&a[i]);}sort(a,4);for(i=0;i<4;i++){printf("%d ",a[i]);}}