Wednesday, 16 May 2012

Write a C program to sort a list of elements (use dynamic memeory allocation to create array).

Underlying concept: Passing Array by reference.


#include<stdio.h>
#include<stdlib.h>
#include "array.h"
void sort(int *s,int n)
{
    int i=0,j,temp;
   
    for(i=0;i<n;i++)
        {
            for(j=0;j<n-i-1;j++)
            {
                if(s[j+1]<s[j])
                    {
                        temp=s[j];
                        s[j]=s[j+1];
                        s[j+1]=temp;
                    }
            }
        }
}




int main()
{
    unsigned n;
    int*p;
    printf("\nEnter the length of array:");
    scanf("%u",&n);
    p=(int *)malloc(n*sizeof(int));
    input(p,n);
    system("clear");
    display(p,n);
    sort(p,n);
    printf("\nArray after sorting :\n");
    display(p,n);
    return 0;
}

0 comments

 
© 2011-2012 ProgrammingBlue
Posts RSS Comments RSS
Back to top