Showing posts with label sorting in c. Show all posts
Showing posts with label sorting in c. Show all posts

Saturday, June 16, 2012

64. PROGRAM TO SORT LIST OF NO. WITHOUT CHANGING THE ORIGINAL ARRAY


/*WAP TO SORT LIST OF NO. WITHOUT CHANGING THE ORIGINAL ARRAY.
 **************not workng***************/
 #include<conio.h>
 #include<stdio.h>
 void main()
 {
 int a[20],i,*ptr,j,temp,n;
 ptr=a;

 clrscr();
 printf("\n\nENTER THE SIZE OF THE ARRAY=");
 scanf("%d",&n);
 printf("\n\nENTER THE ELEMENTS=\n");
 for(i=0;i<n;i++)
 {
 scanf("%d",&a[i]);
 ptr[i]=&a[i];
 }
 for(j=0;j<n-1;j++)
 {
 for(i=0;i<n-1;i++)
  {
   if(*ptr[i]>*ptr[i+1])
   {
    temp=ptr[i];
    ptr[i]=ptr[i+1];
    ptr[i+1]=temp;
   }
  }}
 printf("\n\nORIGINAL ARRAY IS=");
 for(i=0;i<n;i++)
 printf("\n%d",a[i]);
 printf("\n\nAFTER SORTING ARRAY IS=");
 for(i=0;i<n;i++)
 printf("%d",*ptr[i]);
 getch();
 }

Saturday, August 6, 2011

WAP to show Insertion Sort

//WAP to show Insertion Sort
#include<stdio.h>
#include<conio.h>
main()
{ int i,j,a[30],temp,last;
clrscr();
printf("\n\nEnter no. of elements:");
scanf("%d",&last);

printf("\n\nEnter %d elements:",last);
for(i=0 ; i<last ; i++)
{ scanf("%d",&a[i]);
}
printf("\n\nEntered elements are:");
for(i=0 ; i<last ; i++)
{ printf("%d",a[i]);
}

for(i=0 ; i<last ; i++)
{ temp = a[i];
j = i - 1;
while((temp<a[j]) && (j>=0))
{ a[j+1] = a[j];
j = j - 1;
}
a[j+1] = temp;
}


printf("\n\nElements of array after aorting is:");

for(i=0 ; i<last ; i++)
{ printf("%d",a[i]);
}
getch();
}

Programming the Whole World!