C + + implementation of array sorting/inserting reordering/And reverse operation detailed _c language

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.