Insert a new number reorder
Analysis: Compare the new number with the number in the sorted array until you find the insertion point, then move the number after the insertion point back one unit (A[i+1]=a[i]), and then insert the data.
Code:
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main () {
int a[12];//defines an array for storing numbers
int n;//The new number entered
int i=0,j=0,k=0;//sort of variables to use
cout<< "Please input ten integers:" <<endl;
for (i=1;i<=10;i++) {
cin>>a[i];
}//Input data
for (i=1;i<10;i++) {
K=i;
for (j=i+1;j<=10;j++) {
if (A[j]<a[k]) {
K=j;
}
}
A[0]=a[i];
A[I]=A[K];
A[K]=A[0];
}
cout<< "Sorting order:" <<endl;
for (i=1;i<=10;i++) {
cout<<a[i]<< "";
}//Output data
cout<<endl;
cout<< "Please input a new one:";
cin>>n;
for (i=1;i<=10;i++) {
if (a[i]>n) {
K=n;
for (j=10;j>=i;j--) {
A[J+1]=A[J];
}
A[i]=n;
Break
}
}
cout<< "New sorting order:" <<endl;
for (i=1;i<=11;i++) {
cout<<a[i]<< "";
}//Output data
cout<<endl;
return 0;
}
To invert the data in an array that is already sorted:
Analysis: The implementation of the order of the inverse, you need to find an array of the middle of the array, the number as the middle point, corresponding to the two sides of the digital exchange can be.
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main () {
int a[11];//defines an array for storing numbers
int i=0,j=0,k=0;//sort of variables to use
cout<< "Please input ten integers:" <<endl;
for (i=1;i<=10;i++) {
cin>>a[i];
}//Input data
for (i=1;i<10;i++) {
K=i;
for (j=i+1;j<=10;j++) {
if (A[j]<a[k]) {
K=j;
}
}
A[0]=a[i];
A[I]=A[K];
A[K]=A[0];
}
cout<< "Sorting order:" <<endl;
for (i=1;i<=10;i++) {
cout<<a[i]<< "";
}//Output data
cout<<endl;
cout<< "Reverse Order:" <<endl;
i=10;
For (j=1;j<= (I/2); j + +) {
A[0]=A[J];
A[J]=A[I+1-J];
A[I+1-J]=A[0];
}
for (i=1;i<=10;i++) {
cout<<a[i]<< "";
}//Output data
cout<<endl;
return 0;
}
Another method is to create an array, directly in the assignment process to complete the inversion, namely:
Copy Code code as follows:
<pre class=cpp name= "code" > #include <iostream>
using namespace Std;
int main () {
The int a[11];//defines an array to store the numbers </pre><pre class=cpp name= "code" > int b[11];
int i=0,j=0,k=0;//sort of variables to use
cout<< "Please input ten integers:" <<endl;
for (i=1;i<=10;i++) {
cin>>a[i];
}//Input data
for (i=1;i<10;i++) {
K=i;
for (j=i+1;j<=10;j++) {
if (A[j]<a[k]) {
K=j;
}
}
A[0]=a[i];
A[I]=A[K];
A[K]=A[0];
}
cout<< "Sorting order:" <<endl;
for (i=1;i<=10;i++) {
cout<<a[i]<< "";
}//Output data
cout<<endl;
cout<< "Reverse Order:" <<endl;
i=10;
for (j=1;j<=i;j++) {</pre><pre class=cpp name= "code" > B[j]=a[i+1-j];
}
for (i=1;i<=10;i++) {
cout<<b[i]<< "";
}//Output data
cout<<endl;
return 0;
}
</PRE><BR>
Compared with the above method, the computational steps of the data are reduced, but the overhead of the system space is increased.
<div></DIV>
<PRE></PRE>