Example : A Sorting Example/program in C to show Bubble Sort.
#include<stdio.h>
#include<conio.h>
int main()
{
  int i, n, x[50];

  printf("Enter the size (less than 50) of an array\n");
  scanf("%d", &n);

  printf("Enter %d integer numeric values for array\n", n);
  for (i = 0; i < n; i++)
  	{  
          scanf("%d", &x[i]);
	}
	
//sorting the array values
  for(i=1; i<n; i++)
    {
       for(int j=0; j<n-i; j++)
        {
            if(x[j]>x[j+1])
            {
                int temp=x[j];
                x[j]=x[j+1];
                x[j+1]=temp;
            }
        }
    }
    
    printf("\nThe Sorted Order is :\n");
    for(i=0; i<n; i++)
    {
	printf(" %d ",x[i]);
    }  

  return 0;
}

Output: 
Enter the size (less than 50) of an array
5
Enter 5 integer numeric values for array
20
12
3
650
80

The Sorted Order is:
 3  12  20  80  650
Example : A Sorting Example/program in C to show Insertion Sort.
#include<stdio.h>
#include<conio.h>
int main()
{
  int i, j,temp,n, k, x[50];

  printf("Enter the size (less than 50) of an array\n");
  scanf("%d", &n);

  printf("Enter %d integer numeric values for array\n", n);
  for (i = 0; i < n; i++)
  	{  
    	  scanf("%d", &x[i]);
	}
		
	//sorting the array values using insertion sort concept
	
	for (i = 1; i < n; i++)
	{ 
	   temp = x[i];
           j = i - 1;        
           while (j >= 0 && x[j] > temp) 
           {
              x[j + 1] = x[j];
              j = j - 1;
           }
           x[j + 1] = temp;
         }    
    printf("\nThe Sorted Order is:\n");
    for(i=0; i<n; i++)
    {
	printf(" %d ",x[i]);
    }
  return 0;
}

-------------  OR ------------- 

#include<stdio.h>
#include<conio.h>
int main()
{
  int i, j,temp,n, flag=0, x[50];

  printf("Enter the size (less than 50) of an array\n");
  scanf("%d", &n);

  printf("Enter %d integer numeric values for array\n", n);
  for (i = 0; i < n; i++)
  	{  
    	scanf("%d", &x[i]);
	}
		
	//sorting the array values using insertion sort concept

  for (i = 1 ; i <= n - 1; i++) 
	{
           temp = x[i];
           for (j = i - 1 ; j >= 0; j--) 
	     {
                 if (x[j] > temp) 
		   {
                      x[j+1] = x[j];
                      flag = 1;
                   }
                 else
                      break;
             }
           if (flag)
             x[j+1] = temp;
        } 
  
  printf("\nThe Sorted Order are :\n");
  for(i=0; i<n; i++)
    {
	printf(" %d ",x[i]);
    }
  return 0;
}

Output:

Enter the size (less than 50) of an array
5
Enter 5 integer numeric values for array
23
5
600
82
1

The Sorted Order is:
1  5  23  82  600
Example :Sorting Example/program in C to show Selection Sort.
#include<stdio.h>
#include<conio.h>
int main()
{
  int i, j,temp,n, x[50];

  printf("Enter the size (less than 50) of an array\n");
  scanf("%d", &n);

  printf("Enter %d integer numeric values for array\n", n);
  for (i = 0; i < n; i++)
  	{  
    	scanf("%d", &x[i]);
	}
		
//sorting the array values using insertion sort concept

for(i=0; i<n; i++)
    {
     for(j=i+1; j<n; j++)
      {
          if(x[i]>x[j])
          {
              temp=x[i];
              x[i]=x[j];
              x[j]=temp;
          }
      }
    }  
  
    printf("\nThe Sorted Order is :\n");
    for(i=0; i<n; i++)
    {
	printf(" %d ",x[i]);
    }
  return 0;
}

Output:
Enter the size (less than 50) of an array
5
Enter 5 integer numeric values for array
201
3
1
555
58

The Sorted Order is:
 1  3  58  201  555
Example :Sorting Example/program in C to show Quick Sort.
#include<stdio.h>
#include<conio.h>
  int size;
  int main()
  {
	  int y[50],first,last,i;
	  
	  void quick_sort(int y[], int, int);
	  
	  printf("\nInput the size of Array: ");
	  scanf(" %d",&size);
	  
	  printf("\nInput %d values for array one by one:\n",size);
	  for(i=0;i<size;i++)
	  {
	     scanf("%d",&y[i]);	  
	     first=0;
	     last=size-1;
	  }
				
	  quick_sort(y, first, last);
	  
	  printf("\nThe Sorted values using quick sorted array are:\n");
	  for(i=0;i<size;i++)
	  {
	     printf("%d ",y[i]);
	  }	  
	  return 0;
  }
  
  void quick_sort(int y[], int low, int high)
  {
	  int temp,left,right,x,k;
	  if(low>=high)
	  	return;
	  else
	  {
		x=y[low];
		right=low+1;
		left = high;
			  
		while(right<=left)
		{
		   while(y[right]<x && right<=high)
			{
			   right++;
			}
				  
		   while(y[left]>x && left>low)
			{
			   left--;
			}
			  
		   if(right<left)
			{
			   temp=y[right];
			   y[right]=y[left];
			   y[left]=temp;
			   right++;
			   left--;
			}	  
		  }		  	
	  	
		  y[low]=y[left];
		  y[left]=x;
		  
		  quick_sort(y,low,left-1);		  
		  quick_sort(y,left+1,high);
	  }
  }

Output: 
Input the size of Array: 5

Input 5 values for array one by one:
52
3
98
1200
1

The Sorted values using quick sorted array are:
1 3 52 98 1200

Loading


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.