#include<iostream>#include<cstdio>usingnamespace std;intmain(void){/*
1. 定义数组
2. 初始化数组
3. 循环读入数值
4. 判断打印
*/int arr[11], num, value;for(int i =0; i <11;++ i){
arr[i]=0;// 初始化为0 }
cout <<"排序的数的个数:";
cin >> num;for(int i =0; i < num;++ i){
cin >> value;// value >= 0 && value <= 10
arr[value]++;// NOTICE}for(int i =0; i <11;++ i){for(int j =1; j <= arr[i];++ j){
cout << i <<" ";}}return0;}
2. 冒泡排序
#include<cstdio>#include<iostream>usingnamespace std;intmain(void){int arr[10];// input numfor(int i =0; i <10; i ++){
cin >> arr[i];}// Bubble Sortfor(int i =0; i <10;++ i){// NOTICE for(int j =0; j <9-i;++ j){if(arr[j]< arr[j+1]){// from big to smallint temp = arr[j];
arr[j]= arr[j+1];
arr[j+1]= temp;}}}// outputfor(int i =0; i <10; i ++){
cout << arr[i]<<" ";}return0;}
3. 结构体冒泡排序
#include<iostream>usingnamespace std;// create struct arraystruct student{int score;char name[21];};intmain(void){struct student arr[10], t;// inputfor(int i =0; i <10; i ++){
cin >> arr[i].name >> arr[i].score;}// bubble sort based on scorefor(int i =0; i <10;++ i){for(int j = i; j <9;++ j){// from small to bigif(arr[j].score > arr[j+1].score){
t = arr[j];
arr[j]= arr[j+1];
arr[j+1]= t;}}}for(int i =0; i <10;++ i){
cout << arr[i].name <<" ";}return0;}
4. 快速排序
#include<iostream>usingnamespace std;int arr[100], n;// from small to bigvoidQuickSort(int left,int right);intmain(void){// num of sort
cin >> n;for(int i =0; i < n;++ i){
cin >> arr[i];}QuickSort(0, n-1);for(int i =0; i < n;++ i){
cout << arr[i]<<" ";}return0;}voidQuickSort(int left,int right){int temp, i, j;// 排序合法性判断if(left > right)return;
temp = arr[left];// based num
i = left;
j = right;while(i != j){while(arr[j]>= temp && i < j){-- j;}while(arr[i]<= temp && i < j){++ i;}if(i < j){int t = arr[j];
arr[j]= arr[i];
arr[i]= t;}}
arr[left]= arr[i];
arr[i]= temp;QuickSort(left, i-1);QuickSort(i+1, right);return;}