1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int n; 7 cin>>n; 8 int a[1000]={0}; 9 for(int i=1;i<=n;i++){ 10 cin>>a[i]; 11 } 12 int key; 13 for(int j=2;j<=n;j++){ 14 key=a[j]; 15 int i=j-1; 16 while(i>0&&a[i]>key){ 17 a[i+1]=a[i]; 18 i--; 19 } 20 a[i+1]=key; 21 } 22 for(int i=1;i<=n;i++){ 23 cout<<a[i]<<" "; 24 } 25 return 0; 26 }
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int n; 7 cin>>n; 8 int a[1000]={0}; 9 for(int i=1;i<=n;i++){ 10 cin>>a[i]; 11 } 12 int key; 13 for(int j=2;j<=n;j++){ 14 key=a[j]; 15 int i=j-1; 16 while(i>0&&a[i]<key){ 17 a[i+1]=a[i]; 18 i--; 19 } 20 a[i+1]=key; 21 } 22 for(int i=1;i<=n;i++){ 23 cout<<a[i]<<" "; 24 } 25 return 0; 26 }