Introduction 

An integer array can be sorted with various algorithms. This algorithm helps to sort an array while the elements are being entered.

The algorithm is based on the fact that in a sorted array when a new element is entered the position of the element can be found out and the other elemnts are shifted to make a place for the new variable. 

Ascending order runtime sorting: 
It shows how the ascending order runtime sorting is being done while the elements are being entered 

 


Descending order runtime sorting:     

It shows how the descending order runtime sorting is being done while the elements are being entered   


The above images give an idea of the runtime sorting algorithms. The next section gives the algorithms used.
 

The Algorithm 

The algorithms are given bellow
The algorithm is based on the fact that the initial value is already sorted and whenever a new element is added it has its own place in the array and it compares with the previous value and accordingly gets swapped until it reaches its position. 

1.The algorithm for ascending order sorting: 

/********ALGORITHM FOR SORTING A INTEGER ARRAY WHILE INSERTING IN ASCENDING ORDER********/

for(i=0;i<n;i++)           //n is the array size and ar[] is the array
 {
  ptr=i;              //asigning d=value of i
  cin>>ar[i];

/*The sorting is done as if i=0 the following loop is not                                 run as the first element is intially sorted.As the ar[1] is entered the following loop    comapres it with the previous and swaps the values accordingly.*/

  for(j=i-1;j>=0;j--)
   {
    if(ar[ptr]<ar[j])  //Checks if the previous element is larger or smaller
     {                 //If it is larger then they are swapped
      temp=ar[ptr];
      ar[ptr]=ar[j];   //It is continued until the previous value is smaller
      ar[j]=temp;
     }
    else break;

/*AR[ptr] is the new user input so after every swap with previous value 
ptr becomes ptr-1*/

    ptr=ptr-1;         //code reaches here only when ar[ptr]<ar[j]
   }
 } 

2.The algorithm for descending order sorting:  

/********ALGORITHM FOR SORTING A INTEGER ARRAY WHILE INSERTING IN DESCENDING ORDER*******/

for(i=0;i<n;i++)           //n is the array size and ar[] is the array
 {
  ptr=i;              //asigning d=value of i
  cin>>ar[i];

/*The sorting is done as if i=0 the following loop is not                                 run as the first element is intially sorted.As the ar[1] is entered the following loop    comapres it with the previous and swaps the values accordingly.*/

  for(j=i-1;j>=0;j--)
   {
    if(ar[ptr]>ar[j])  //Checks if the previous element is larger or smaller
     {                 //If it is smaller then they are swapped
      temp=ar[ptr];
      ar[ptr]=ar[j];   //It is continued until the previous value is larger
      ar[j]=temp;
     }
    else break;

/*AR[ptr] is the new user input so after every swap with previous value 
ptr becomes ptr-1*/

    ptr=ptr-1;         //code reaches here only when ar[ptr]>ar[j]
   }
 }  

After understanding the above algorithms download code.zip

Itwill contain two files:
RNSA.cpp and RNSD.cpp 

The RNSA.cpp has the code for ascending order sorting and the RNSD has the code for descending order sorting. The code has the following additions for showing the ourputs:

for(int h=0;h<=i;h++)
 cout<<val[h]<<" ";

More Informations: 

While using the algorithm it must be kept in mind that the array size should not be very large as the array elements are being inputted and the sorting is also being carried out.
The code that is given here downloading has an array size of 30. 

 

Thank You for reading the article

Pijush 

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"