博约8486 • 15小时前
using namespace std; int main() {
int n ;
cin >> n;
int nums[n ] = {};
for(int i = 0; i < n ; i++) {
cin >> nums[i];
}
int sortedIndex = 0;
while(sortedIndex < n ) {
cout << "Insert element[" << sortedIndex + 1 << "]:" << endl;
cout << "Init:";
for(int i = 0; i <= sortedIndex; i++){
cout << nums[i] << " ";
}
cout << endl;
for(int i = sortedIndex; i > 0; i--) {
if(nums[i] < nums[i - 1]) {
int temp = nums[i];
nums[i] = nums[i - 1];
cout << "Move back:";
for(int j = 0; j <= sortedIndex ; j++) {
cout << nums[j] << " ";
}
cout << endl;
nums[i - 1] = temp;
}else break;
}
cout << "Final:";
for(int i = 0; i <= sortedIndex ; i++) {
cout << nums[i] << " ";
}
cout << endl;
sortedIndex++;
}
}
评论:
请先登录,才能进行评论