Tuesday, September 5, 2023

Sorting Techniques in C

 Sorting Methods : 

  1.     bubble sort
  2.   insertion sort
  3.  merge sort
  4.   quick sort
1) bubble sort program in c

  #include <stdio.h>

void bubble_sort(long [], long);

int main()
{
  long array[100], n, c;

  printf("Enter number of elements\n");
  scanf("%ld", &n);

  printf("Enter %ld integers\n", n);

  for (= 0; c < n; c++)
    scanf("%ld", &array[c]);

  bubble_sort(array, n);

  printf("Sorted list in ascending order:\n");

  for (= 0; c < n; c++)
     printf("%ld\n", array[c]);

  return 0;
}

void bubble_sort(long list[], long n)
{
  long c, d, t;

  for (= 0 ; c < n - 1; c++) {
    for (= 0 ; d < n - c - 1; d++) {
      if (list[d] > list[d+1]) {
        /* Swapping */
        t         = list[d];
        list[d]   = list[d+1];
        list[d+1] = t;
      }
    }
  }
}

The output of this C program will depend on the input provided by the user. Here's an example of how the program would execute with sample input:

mathematica
Enter number of elements 6
Enter 6 integers
45 12 78 32 61 19 
Sorted list in ascending order: 
12 19 32 45 61 78

2) insertion sort

/* Insertion sort ascending order */

#include <stdio.h>

int main()
{
  int n, array[1000], c, d, t, flag = 0;

  printf("Enter number of elements\n");
  scanf("%d", &n);

  printf("Enter %d integers\n", n);

  for (= 0; c < n; c++)
    scanf("%d", &array[c]);

  for (= 1 ; c <= n - 1; c++) {
    t = array[c];

    for (= c - 1 ; d >= 0; d--) {
      if (array[d] > t) {
        array[d+1] = array[d];
        flag = 1;
      }
      else
        break;
    }
    if (flag)
      array[d+1] = t;
  }

  printf("Sorted list in ascending order:\n");

  for (= 0; c <= n - 1; c++) {
    printf("%d\n", array[c]);
  }

  return 0;
}

The output of this C program will depend on the input provided by the user. Here's an example of how the program would execute with sample input:

mathematica
Enter number of elements 6 
Enter 6 integers 45 12 78 32 61 19
Sorted list in ascending order:
12 19 32 45 61 78

3) merge sort program in c



#include<stdio.h>
void mergesort(int a[],int l,int r);
void merge(int a[],int l1,int r1,int l2,int r2);
int main() {
 int a[30],n,l;
 printf("Enter number of elements:");
 scanf("%d",&n);
 printf("Enter array elements:");
 for(l=0;l<n;l++)
   scanf("%d",&a[l]);
   mergesort(a,0,n-1);
   printf("\nSorted array is :");
   for(l=0;l<n;l++)
     printf("%d ",a[l]);
     return 0;
}
void mergesort(int a[],int l,int r) {
 int mid;
 if(l<r) {
   mid=(l+r)/2;
   mergesort(a,l,mid); //left recursion
   mergesort(a,mid+1,r); //right recursion
   merge(a,l,mid,mid+1,r); //merging of two sorted sub-arrays
 }
}
void merge(int a[],int l1,int r1,int l2,int r2) {
 int temp[50]; //array used for merging
 int l,r,k;
 l=l1; //beginning of the first list
 r=l2; //beginning of the second list
 k=0;
 while(l<=r1 && r<=r2) //while elements in both lists {
   if(a[l]<a[r])
      temp[k++]=a[l++];
   else
      temp[k++]=a[r++];
}
 while(l<=r1) //copy remaining elements of the first list
   temp[k++]=a[l++];
   while(r<=r2) //copy remaining elements of the second list
     temp[k++]=a[r++];
     //Transfer elements from temp[] back to a[]
     for(l=l1,r=0;l<=r2;l++,r++)
       a[l]=temp[r];
}

Here's an example of what the output might look like:

yaml
Enter number of elements: 5 
Enter array elements: 12 5 7 18 3 
Sorted array is: 3 5 7 12 18
4) Quick sort program in c 

/*
 * C Program to sort an array of integers using Quick Sort without recursion
 */
 
#include <stdio.h>
#include <stdlib.h>
 
int quickSort(int *arr, int low, int high)
{
    int i = low, j = high;
    int pivot = arr[(low + high) / 2];
    while (i <= j)
    {
        while (arr[i] < pivot)
            i++;
        while (arr[j] > pivot)
            j--;
        if (i <= j)
        {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            i++;
            j--;
        }
    }
    if (low < j)
        quickSort(arr, low, j);
    if (i < high)
        quickSort(arr, i, high);
    return 0;
}
 
int main(void)
{
    puts("Enter the number of elements in the array: ");
    int n;
    scanf("%d", &n);
    int arr[n];
    puts("Enter the elements of the array: ");
    for (int i = 0; i < n; i++)
    {
        printf("arr[%d]: ", i);
        scanf("%d", &arr[i]);
    }
    int low = 0;
    int high = n - 1;
    int pivot = arr[high];
    int k = low - 1;
    for (int j = low; j < high; j++)
    {
        if (arr[j] <= pivot)
        {
            k++;
            int temp = arr[k];
            arr[k] = arr[j];
            arr[j] = temp;
        }
    }
    int temp = arr[k + 1];
    arr[k + 1] = arr[high];
    arr[high] = temp;
    int pi = k + 1;
    quickSort(arr, low, pi - 1);
    quickSort(arr, pi + 1, high);
    puts("The sorted array is: ");
    for (int i = 0; i < n; i++)
    {
        printf("%d ", arr[i]);
    }
    return 0;
}

Here's an example of what the output might look like:

less
Enter the number of elements in the array5 
Enter the elements of the array:
arr[0]: 12 arr[1]: 5 arr[2]: 7 arr[3]: 18 arr[4]: 3 
The sorted array is: 3 5 7 12 18

No comments:

Post a Comment