C++ implementation of array sort and insert reorder and inverse operation details

  • 2020-04-02 01:56:52
  • OfStack

Insert new Numbers to reorder
Analysis:
Compare the new Numbers with the Numbers in the sorted array one by one until the insertion point is found, then move the Numbers after the insertion point back one unit (a[I +1]=a[I]), and then insert the data.

Code:


#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;//The variables used for sorting
 cout<<"please input ten integers:"<<endl;
 for(i=1;i<=10;i++){
  cin>>a[i];
 } //The 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]<<"   ";
 } //The 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]<<"   ";
 } //The output data
 cout<<endl; 
 return 0;
} 

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/201310210857486.jpg ">

Invert the data in the sorted array:

Analysis: the implementation of the order of the inverse, need to find the array in the middle of the array, with the number as the middle point, the corresponding two sides of the number exchange.

#include<iostream>
using namespace std;
int main(){
 int a[11];//Defines an array for storing Numbers
 int i=0,j=0,k=0;//The variables used for sorting
 cout<<"please input ten integers:"<<endl;
 for(i=1;i<=10;i++){
  cin>>a[i];
 } //The 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]<<"   ";
 } //The 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]<<"   ";
 } //The output data
 cout<<endl; 
 return 0;
} 

Another method is to set up an array and directly complete the inverse during the assignment process, that is:

<PRE class=cpp name="code">#include<iostream>
using namespace std;
int main(){
 int a[11];//Defines an array & NBSP; used to store Numbers. </ PRE> <The PRE class = CPP name = "code"> & have spent & have spent & have spent & have spent & have spent & have spent & have spent Int b [11];
 int i=0,j=0,k=0;//The variables used for sorting
 cout<<"please input ten integers:"<<endl;
 for(i=1;i<=10;i++){
  cin>>a[i];
 } //The 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]<<"   ";
 } //The 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]<<"   ";
 } //The output data
 cout<<endl; 
 return 0;
} 
</PRE><BR>
 Compared with the above method, the calculation steps of data are reduced, but the overhead of system space is increased 
<DIV><IMG alt="" src="<img src="//files.jb51.net/file_images/article/201310/201310210857497.jpg" alt="" />"></DIV>
<PRE></PRE>

Related articles: