题解 | #C++选择排序#
C++选择排序
https://www.nowcoder.com/practice/3b6175426e704c0b9461628b2278631b
#include <iostream> using namespace std; int main() { int arr[6] = { 0 }; int len = sizeof(arr) / sizeof(int); for (int i = 0; i < len; i++) { cin >> arr[i]; } // write your code here...... int i,j,k=0; for(j=0;j<len-1;j++) {int k=j; for(i=j+1;i<len;i++) if(arr[k]>arr[i]) k=i; arr[k]=arr[k]+arr[j]-(arr[j]=arr[k]); } for (int i = 0; i < len; i++) { cout<<arr[i]<<" "; } return 0; }