/*A program of data structure to represent a random array into sorted or decending order form.& put it into another array*/
//SORTING OF ARRAY MEANS...place the elements of array in serial wise...EITHER IN 1)ASCENDING ORDER..OR IN 2)DECENDING ORDER//
#include<stdio.h>
int main()
{
int arr[5]={2,1,3,5,4};//this is old array
int arr1[5]={0};//new array in which sorted element means elements in decending form are going to be put...
int i,j,x;
for(i=0;i<5;i++)//i will represent no. of elements in old array....& compare one by one to those elements to each & every element of new array....upto value of i....means upto no. of elements are already being put into new array...
//NO. OF ELEMENTS OF OLD ARRAY WILL BE SAME AS NO. OF ELEMENTS OF NEW ARRAY...
//WE ARE GOING TO CONVERT THESE MANY NO. OF ELEMENTS INTO DECENDING ORDER
{
for(j=0;j<=i;j++)//if at last time arr[6]={0},then then arr1[6]={0}always if arr1[j]={0},so j<=i will work completely,go inside loop,and by shifting value to next position ,it will place most small value to last pos. of arr1.for this array should be declared one more as required,so last pos of arr1 always be 1.
{
if(arr1[j]<arr[i])//1ST OF ALL IN THIS CONDITION..CHECK WEITHER EITHER ELEMENT IN NEW ARRAY IS SMALL THAN OLD ARRAY OR NOT...IF SMALL IN NEW ARRAY..MEANS ELEMENT OF OLD ARRAY IS GREATER ...THEN SHIFT ALL OTHER ELEMENTS OF NEW ARRAY & PUT THIS OLD ARRAY ELEMENT WHICH IS GREATER INTO NEW ARRAY AT ONE OF THE STARTING POSITIONS WHERE OLD ARRAY ELEMENT SEEMS GREATER BY ITSELF.....SO IN THIS WAY...AT 1ST POINTS..GREATER ELEMENTS OF OLD ARRAY TAKES PLACE BY SHIFTING SMALL ELEMENTS INTO RIGHT BY ONE...MAKING THIS NEW ARRAY INTO ASCENDING ORDER
{
x=i;//THIS IS HOW SHIFTING TAKES PLACE...PLACE i into x..which is no. of old elemENTS
x--;//decrement x by 1 ...bcz elements in array starts from 0 ..not 1..
for(;j<=x;x--)//j<=x means ..j representing elements of new array..means at the point where old array element is grater than all new array elements,,then shift all those new array elements to right 1 by1
{
arr1[x+1]=arr1[x];//using this loop..place element 5 to 6th position..4 to 5th..3 to 4th..2 to 3rd..1 to 2nd..move the position of all the elements that are smaller than that element which is to be inserted ...to place elements in new array in decending order...
}
arr1[j]=arr[i]; //as all the smaller elements in new array are shifted to right ...put element which is greater than all of those elements that are shifted....into its exact position..which is j..where next element is new array is found to be smaller than i element which is compared to all elements of j...
printf("\narr1[%d]=%d",j,arr1[j]);
break;//whenever assignment takes place..break from this loop..into other loop..loop of i..