今天学习了冒泡排序,选择排序
冒泡排序
#include <stdio.h>
int main()
{//1.定义数组int arr[] = { 3,5,2,1,4 };int len = sizeof(arr) / sizeof(int);//2.先分开写每次循环,找到规律后再合并for (int i = 0; i < len; i++){for (int j = 0; j < len - 1 - i; j++){//交换if (arr[j] >= arr[j + 1]){int temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}//遍历for (int i = 0; i < len; i++){printf("%d ", arr[i]);}return 0;
}
选择排序
#include <stdio.h>
int main()
{//定义数组int arr[] = { 3,5,2,1,4 };int len = sizeof(arr) / sizeof(int);/*arr[0] arr[1] arr[1] arr[2]arr[0] arr[2] arr[1] arr[3]arr[0] arr[3] arr[1] arr[4]arr[0] arr[4] */for (int i = 0; i < len - 1; i++){for (int j = i + 1; j < len ; j++){if(arr[i]>arr[j]){int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}}}//遍历for (int i = 0; i < len; i++){printf("%d ", arr[i]);}return 0;
}
冲啊!!!