Showing posts with label sort. Show all posts
Showing posts with label sort. Show all posts

Saturday, August 9, 2014

Program to alphabetically sort names.

Program to alphabetically sort names. If you need help, comment below

#include<stdio.h>
#include<string.h>
void sort(int);
void disp(int);
struct student
       {char name[25];
       }a[50],*ptr;
int main()
       {ptr=a;
        int i,j,n;
        printf("\nEnter the number of students:");
        scanf("%d",&n);
        n++;
        printf("\nEnter the names:\n");
        for(i=0;i<n;i++)
               gets((ptr+i)->name);
        sort(n);
        disp(n);
        return 0;
       }
void sort(int n)
       {

        int i,j;
        char copy[25];
        for(i=0;i<n;i++)
               for(j=0;j<strlen((ptr+i)->name);j++)
                       {if((ptr+i)->name[j]<97)
                               (ptr+i)->name[j]+=32;
                       }

        for(i=0;i<n;i++)
               for(j=0;j<n-1;j++)
                       if(strcmp((ptr+j)->name,(ptr+j+1)->name)>0)
                               {strcpy(copy,(ptr+j)->name);
                                strcpy((ptr+j)->name,(ptr+j+1)->name);
                                strcpy((ptr+j+1)->name,copy);
                               }
       }







void disp(int n)
       {
        int i;
        for(i=0;i<n;i++)
               (ptr+i)->name[0]-=32;
        printf("\n\nThe sorted list is:\n");
        for(i=1;i<n;i++)
               puts((ptr+i)->name);

       }

Program for quick sort

Program to implement quick sort. If you need help, comment below

#include<stdio.h> int a[20],b[20],n,z; void quick(int x[],int first,int last) { int pivot; if(first<last) { pivot=partition(x,first,last); quick(x,first,pivot-1); quick(x,pivot+1,last); } } int partition(int x[],int first,int last) { int pivot,temp,i,j; pivot=first; i=first; j=last; while(i<j) { while((x[i]<=x[pivot])&&(i<last)) i++; while(x[j]>x[pivot]) j--; if(i<j) { temp=x[i]; x[i]=x[j]; x[j]=temp; } for(z=1;z<=n;z++) printf("%d\t ",a[z]); printf("\n"); } temp=x[pivot]; x[pivot]=x[j]; x[j]=temp; return j; } int main() { int i,j,c,ch; printf("\nQuicksort"); printf("\nEnter the no. of elements in the array:\t"); scanf("%d",&n); printf("\nEnter the elements into the array:\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); quick(a,1,n); printf("\nAfter sorting:\n"); for(i=1;i<=n;i++) printf("%d ",a[i]); }

Program for merge sort.

Program for merge sort. If you need help, feel free to ask.

#include<stdio.h> void mergesort(int x[20],int n) { int temp[20],i,j,k,l1,l2,size,u1,u2,a; size=1; while(size<n) { l1=0; k=0; while(l1+size<n) { l2=l1+size; u1=l2-1; u2=((l2+size-1<n)?l2+size-1:n-1); for(i=l1,j=l2;i<=u1&&j<=u2;k++) { if(x[i]<=x[j]) { temp[k]=x[i]; i++; } else { temp[k]=x[j]; j++; } } for(;i<=u1;k++) { temp[k]=x[i]; i++; } for(;j<=u2;k++) { temp[k]=x[j]; j++; } l1=u2+1; } for(i=l1;k<n;k++) temp[k++]=x[i]; for(i=0;i<n;i++) x[i]=temp[i]; size=size*2; printf("\n"); for(a=0;a<n;a++) printf("%d ",x[a]); printf("\n"); } } main() { int x[20],n,i; printf("\nEnter the value of the number of elements you wish to enter\n"); scanf("%d",&n); printf("\nEnter values of the elements you wish to sort"); for(i=0;i<n;i++) scanf("%d",&x[i]); printf("\nBefore Sorting this is how it looks\n:"); for(i=0;i<n;i++) printf(" %d",x[i]); mergesort(x,n); }

Program for heap sort

Program for Heap Sort. Kinda tricky program. If you need help, comment below

#include<stdio.h> int tree[20],n,ah[20],c=0; void build(int m,int item) { int ptr,par,f=0; m=m+1; ptr=m; while(ptr>1) { par=ptr/2; if(item<=tree[par]) { tree[ptr]=item; f=1; break; } tree[ptr]=tree[par]; ptr=par; } if(f==0) tree[1]=item; } void delheap(int k,int m) { int left,right,ptr,last,i; ah[k]=tree[1]; last=tree[m]; m=m-1; ptr=1; left=2; right=3; while(right<=m) { if((last>=tree[left])&&(last>=tree[right])) { tree[ptr]=last; return; } if(tree[right]<=tree[left]) { tree[ptr]=tree[left]; ptr=left; } else { tree[ptr]=tree[right]; ptr=right; } left=2*ptr; right=left+1; } if((left==m)&&(last<tree[left])) { tree[ptr]=tree[left]; ptr=left; } tree[ptr]=last; c++; printf("\n\nPass %d: ",c); for(i=1;i<=m;i++) printf("%d ",tree[i]); } main() { int i,j; printf("\nEnter total number of elements:"); scanf("%d",&n); printf("\nEnter elements to be inserted : "); for(i=1;i<=n;i++) scanf("%d",&tree[i]); for(i=1;i<n;i++) build(i,tree[i+1]); printf("\n\nHeap : "); for(i=1;i<=n;i++) printf("%d ",tree[i]); for(i=n,j=1;i>1;i--,j++) delheap(j,i); if(i==1) ah[n]=tree[1]; printf("\n\nHeap sort : "); for(i=1;i<=n;i++) printf("%d ",ah[i]); }