Search This Blog

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

Wednesday, 13 January 2016

Quick Sort

/* Quick Sort */

    #include<stdio.h>
    #include<conio.h>
    void quicksort(int [],int,int);
    void main()
    {

    int a[10],n,i;
    printf("\nEnter size of array");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
         printf("\nEnter a[%d]",i);
         scanf("%d",&a[i]);
    }
    quicksort(a,0,n-1);
    printf("\nSorted array is:\n");
    for(i=0;i<n;i++)
    {
         printf("%d ",a[i]);
    }

    }
    void quicksort(int x[10],int first,int last)
    {

    int pivot,i,j,temp;
    if(first<last)
    {
         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;
         }
    }
    temp=x[pivot];
    x[pivot]=x[j];
    x[j]=temp;
    quicksort(x,first,j-1);
    quicksort(x,j+1,last);
    }

    }

Merge Sort


    #include<stdio.h>
    #include<conio.h>
    void partition(int [],int,int);
    void merge_sort(int [],int,int,int);
    void main()
    {

    int a[10],i,n;
    clrscr();
    printf("\nEnter no of elements:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
         printf("\nEnter a[%d]",i);
         scanf("%d",&a[i]);
    }
    partition(a,0,n-1);
    for(i=0;i<n;i++)
    {
         printf("%d ",a[i]);
    }
    getch();

    }

    void partition(int a[],int low,int high)
    {

    int mid;
    if(low<high)
    {
         mid=(low+high)/2;
         partition(a,low,mid);
         partition(a,mid+1,high);
         merge_sort(a,low,mid,high);
    }

    }

    void merge_sort(int a[],int low,int mid,int high)
    {

    int temp[10];
    int i,j,k,m;
    m=mid+1;

    for(i=low;j<=mid && m<=high;i++)
    {
         if(a[j]<=a[m])
         {
              temp[i]=a[j];
              j++;
         }
         else
         {
              temp[i]=a[m];
              m++;
         }
    }
    if(j>mid)
    {
         for(k=m;k<=high;k++)
         {
              temp[i]=a[k];
              i++;
         }
    }
    else
    {
         for(k=j;k<=mid;k++)
         {
               temp[i]=a[k];
               i++;
         }
    }
    for(k=low;k<=high;k++)
    {
         a[k]=temp[k];
    }

    }

Insertion Sort


    #include<stdio.h>
    #include<conio.h>

    void main()
    {

    int i,j,temp,arr[10],n;
    clrscr();
    printf("\nEnter size of ayyar");
    scanf("%d",&n);

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

    for (i = 1; i < n; i++)
    {
         int tmp = arr[i];
         int j;
         for (j = i; j > 0; j--)
         {
              if (arr[j - 1] < tmp)
                   break;
              arr[j] = arr[j - 1];
         }
         arr[j] = tmp;
    }

    printf("\nSorted array is \n");
    for(i=0;i<n;i++)
    {
         printf("%d\n",arr[i]);
    }
    getch();

    }

Selection Sort



    #include<stdio.h>
    #include<conio.h>

    void main()
    {

    int i,j,min,n,a[6],temp;
    clrscr();
    printf("Enter number");
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
         scanf("%d",&a[i]);
    }
    printf("\nSorted List is:\n");

    for(i=0;i<n;i++)
    {
         min = i;
         for(j=i+1;j<n;j++)
         {
              if(a[j]<a[min])
              min = j;
         }
         temp=a[i];
         a[i]=a[min];
         a[min]=temp;
    }

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

    }

Bubble Sort



   #include<stdio.h>

    void main()
    {

    int i,j,n,temp,a[10];
    printf("\nEnter n\n");
    scanf("%d",&n);

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

    for (i = 1; i < n; i++)
    {
    for (j = 0; j < n - 1; j++)
    {
    if (a[j] > a[j + 1])
    {
    temp = a[j];
    a[j] = a[j + 1];
    a[j + 1] = temp;
    }
    }
    }

    printf("\nSorted ayyay using bubble sort is\n");
    for (i = 0; i < n; i++)
    {
    printf("\n%d", a[i]);
    }

    }