C語言:控制流
阿新 • • 發佈:2020-12-20
#include <QCoreApplication> #include <iostream> using namespace std; //選擇排序,大迴圈每次選擇一個數與後面元素逐個比較 void mysort(char* src,int len){ int tem =0; int exchanged = 1;//交換標誌位 for(int i = 0; i < len && exchanged; i++){ //每輪比較之前重置交換標誌位 exchanged = 0; for(int j= i + 1; j < len; j++){ //從小到大> //從大到小< //src[i]依次與它後面元素比較 if(src[i] > src[j]){ //需要交換,置位標誌位 exchanged = 1; tem = src[i]; src[i] = src[j]; src[j] = tem; } } } } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); char b2[]={11,5,4,7,3,0,9,10,22}; mysort(b2, 9); cout<<b2<<endl; for (auto j : b2) { printf("從大到小 mysort out buff2: %d\n", j); } return a.exec(); }