Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

sortings

/*Note:-There may be some errors*/
------------merge---------------------
#include<stdio.h>
#include<conio.h>

#define MAX_SIZE 5

void merge_sort(int, int);
void merge_array(int, int, int, int);

int arr_sort[MAX_SIZE];

int main() {
  int i;

  printf("Simple Merge Sort Example - Functions and Array\n");
  printf("\nEnter %d Elements for Sorting\n", MAX_SIZE);
  for (i = 0; i < MAX_SIZE; i++)
    scanf("%d", &arr_sort[i]);

  printf("\nYour Data   :");
  for (i = 0; i < MAX_SIZE; i++) {
    printf("\t%d", arr_sort[i]);
  }

  merge_sort(0, MAX_SIZE - 1);

  printf("\n\nSorted Data :");
  for (i = 0; i < MAX_SIZE; i++) {
    printf("\t%d", arr_sort[i]);
  }
  getch();

}

void merge_sort(int i, int j) 
{
  int m;

  if (i < j)
   {
    m = (i + j) / 2;
    merge_sort(i, m);
    merge_sort(m + 1, j);
    merge_array(i, m, m + 1, j);
  }
}

void merge_array(int a, int b, int c, int d) 
{
  int t[50];
  int i = a, j = c, k = 0;

  while (i <= b && j <= d) 
  {
    if (arr_sort[i] < arr_sort[j])
      t[k++] = arr_sort[i++];
    else
      t[k++] = arr_sort[j++];
  }
  while (i <= b)
    t[k++] = arr_sort[i++];

  while (j <= d)
    t[k++] = arr_sort[j++];

  for (i = a, j = 0; i <= d; i++, j++)
    arr_sort[i] = t[j];
}
----------------------quick------------------------------
#include <stdio.h>
 
void quicksort (int [], int, int);
 
int main()
{
    int list[50];
    int size, i;
 
    printf("Enter the number of elements: ");
    scanf("%d", &size); 
    printf("Enter the elements to be sorted:\n");
    for (i = 0; i < size; i++)
    {
        scanf("%d", &list[i]);
    } 
    quicksort(list, 0, size - 1);
    printf("After applying quick sort\n");
    for (i = 0; i < size; i++)
    {
        printf("%d ", list[i]);
    }
    printf("\n");
 
    return 0;
}
void quicksort(int list[], int low, int high)
{
    int pivot, i, j, temp;
    if (low < high)
    {
        pivot = low;
        i = low;
        j = high;
        while (i < j) 
        {
            while (list[i] <= list[pivot] && i <= high)
            {
                i++;
            }
            while (list[j] > list[pivot] && j >= low)
            {
                j--;
            }
            if (i < j)
            {
                temp = list[i];
                list[i] = list[j];
                list[j] = temp;
            }
        }
        temp = list[j];
        list[j] = list[pivot];
        list[pivot] = temp;
        quicksort(list, low, j - 1);
        quicksort(list, j + 1, high);
    }
}
--------------------------heap-----------------------------------
#include <stdio.h>
int main()
{
    int heap[10], no, i, j, c, root, temp;
    clrscr();
    printf("\n Enter no of elements :");
    scanf("%d", &no);
    printf("\n Enter the nos : ");
    for (i = 0; i < no; i++)
       scanf("%d", &heap[i]);
    for (i = 1; i < no; i++)
    {
	c = i;
	do
	{
	    root = (c - 1) / 2;
	 if (heap[root] < heap[c])   /* to create MAX heap array */

	    {

		temp = heap[root];

		heap[root] = heap[c];

		heap[c] = temp;

	    }

	    c = root;

	} while (c != 0);

    }
    printf("Heap array : ");

    for (i = 0; i < no; i++)

	printf("%d\t ", heap[i]);

    for (j = no - 1; j >= 0; j--)

    {

	temp = heap[0];

	heap[0] = heap[j];    /* swap max element with rightmost leaf element */

	heap[j] = temp;

	root = 0;

	do

	{

	    c = 2 * root + 1;    /* left node of root element */

	    if ((heap[c] < heap[c + 1]) && c < j-1)

		c++;

       if (heap[root]<heap[c] && c<j)    /* again rearrange to max heap array */

	    {

		temp = heap[root];

		heap[root] = heap[c];

		heap[c] = temp;

	    }

	    root = c;

	} while (c < j);

    }

    printf("\n The sorted array is : ");

    for (i = 0; i < no; i++)

       printf("\%3d", heap[i]);

    printf("\n Complexity : \n Best case = Avg case = Worst case = O(n logn) \n");
 return 0;

}
---------------------------------selection------------------------------
#include <stdio.h>
 
int main()
{
   int array[100], n, c, d, position, swap;
 
   printf("Enter number of elements\n");
   scanf("%d", &n);
 
   printf("Enter %d integers\n", n);
 
   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);
 
   for ( c = 0 ; c < ( n - 1 ) ; c++ )
   {
      position = c;
 
      for ( d = c + 1 ; d < n ; d++ )
      {
         if ( array[position] > array[d] )
            position = d;
      }
      if ( position != c )
      {
         swap = array[c];
         array[c] = array[position];
         array[position] = swap;
      }
   }
 
   printf("Sorted list in ascending order:\n");
 
   for ( c = 0 ; c < n ; c++ )
      printf("%d\n", array[c]);
 
   return 0;
}
--------------------------------shell---------------------------------
#include<stdio.h>
int main()
{
    int t,gap,i,A[100],n;
	printf("enter n\n");
	scanf("%d",&n);
	for(i=0;i<n;i++)
	scanf("%d",&A[i]);
	gap=n;
	while(gap>1)
	{
	gap=(gap+1)/2;
	for(i=0;i<n-gap;i++)
	{
	if(A[i+gap]<A[i])
	{
	t=A[i];
	A[i]=A[i+gap];
	A[i+gap]=t;
	}
	}	 
     } 
	printf("The Sorted Elements are\n");
	for(i=0;i<n;i++)
	printf("%3d",A[i]);
}
------------------------------insertion-------------------------------------
#include<stdio.h>
void main()
{
    int i,j,k,z,a[100],n;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for (i=1;i<n;i++)
    {
	k=a[i];
	j=i-1;
	while((j >= 0) && (k < a[j]))
	{
	    a[j+1]=a[j];
	    j--;
	}
	a[j+1]=k;
    }
    for(z=0;z<n;z++)
       {
	printf("%d ",a[z]);
       }
       
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.