Tuesday, March 19, 2013

C Language Example # 39 Array element sorting without temporary array or variable.

C Language Example # 39 Array element sorting without temporary array or variable..

* Program will Sort array element without using any temporary variable or temporary array
.

//Written by : Latest Technology Guide
//Title : C Language Example # 39 Array element sorting without temporary array or variable..


#include <stdio.h>
#include <conio.h>
void main()
{
    clrscr();
    int arr[70],i,n,j,temp;

    printf("Enter the number of elements :");
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
        printf("Enter Element arr[%d] =",i);
        scanf("%d",&arr[i]);
    }

    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(arr[i]>arr[j])     //change < sign to descending
            {
             //    temp=arr[i];
                arr[i]=arr[i]+arr[j];
              //    arr[i]=arr[j];
                arr[j]=arr[i]-arr[j];
              //    arr[j]=temp;
                arr[i]=arr[i]-arr[j];
            }
        }
    }

        for(i=0;i<n;i++)
            printf("%3d",arr[i]);

    getch();
}


Output:




--------------------------------------------------------------------------------
Explanation of C Programming Language Example # 39 Array element sorting without temporary array or variable..

* You need to enter upper bound value of array for which you are going to enter value and want to sort this array.
* You need to enter array values up to array upper bound.
* This program will sort array elements.


Note: All programs are developed and tested using Turbo C++ 3.0 under Windows XP. We just want to provide guidelines to the users. If you are using any other Compiler or other operating system they you need to modify this program as per your requirements.

2 comments: