Search
 
SCRIPT & CODE EXAMPLE
 

C

marge sort in c language

#include <stdio.h>
void mergesort();
void merge();
void main()
{
    int n;
    printf("
enter size of array : ");
    scanf("%d", &n);
    int a[n], i;
    printf("
enter element :
");
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    mergesort(a, 0, n - 1);
    printf("
your sorted array on based  merge sort :
");
    for (i = 0; i < n; i++)
    {
        printf("	%d", a[i]);
    }
    printf("

");
}
void mergesort(int a[], int first, int last)
{
    int mid;
    if (first < last)
    {
        mid = (first + last) / 2;
        mergesort(a, first, mid);
        mergesort(a, mid + 1, last);
        merge(a, first, mid, last);
    }
}
void merge(int a[], int first, int mid, int last)
{
    int b[50];
    int i, j, k;
    i = first;
    j = mid+1;
    k = first;
    while (i <= mid && j <= last)
    {
        if (a[i] <= a[j])
        {
            b[k++] = a[i++];
        }
        else
        {
            b[k++] = a[j++];
        }
    }
    if (i > mid)
    {
        while (j <= last)
        {
            b[k++] = a[j++];
        }
    }
    else
    {
        while (i <= mid)
        {
            b[k++] = a[i++];
        }
    }
    for (i = first; i <= last; i++)
    {
        a[i] = b[i];
    }
}
Comment

merge sort code c

/* C program for Merge Sort */
#include <stdio.h>
#include <stdlib.h>
 
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 = r - m;
 
    /* create temp arrays */
    int L[n1], R[n2];
 
    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];
 
    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        }
        else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
 
    /* Copy the remaining elements of L[], if there
    are any */
    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }
 
    /* Copy the remaining elements of R[], if there
    are any */
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}
 
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
    if (l < r) {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        int m = l + (r - l) / 2;
 
        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m + 1, r);
 
        merge(arr, l, m, r);
    }
}
 
/* UTILITY FUNCTIONS */
/* Function to print an array */
void printArray(int A[], int size)
{
    int i;
    for (i = 0; i < size; i++)
        printf("%d ", A[i]);
    printf("
");
}
 
/* Driver code */
int main()
{
    int arr[] = { 12, 11, 13, 5, 6, 7 };
    int arr_size = sizeof(arr) / sizeof(arr[0]);
 
    printf("Given array is 
");
    printArray(arr, arr_size);
 
    mergeSort(arr, 0, arr_size - 1);
 
    printf("
Sorted array is 
");
    printArray(arr, arr_size);
    return 0;
}
Comment

merge sort in c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// Finding mid element
int m = l+(r-l)/2;
// Recursively sorting both the halves
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
 
// Merge the array
merge(arr, l, m, r);
}
}
Comment

merge sort in C

// Merge sort in C

#include <stdio.h>

// Merge two subarrays L and M into arr
void merge(int arr[], int p, int q, int r) {

  // Create L ← A[p..q] and M ← A[q+1..r]
  int n1 = q - p + 1;
  int n2 = r - q;

  int L[n1], M[n2];

  for (int i = 0; i < n1; i++)
    L[i] = arr[p + i];
  for (int j = 0; j < n2; j++)
    M[j] = arr[q + 1 + j];

  // Maintain current index of sub-arrays and main array
  int i, j, k;
  i = 0;
  j = 0;
  k = p;

  // Until we reach either end of either L or M, pick larger among
  // elements L and M and place them in the correct position at A[p..r]
  while (i < n1 && j < n2) {
    if (L[i] <= M[j]) {
      arr[k] = L[i];
      i++;
    } else {
      arr[k] = M[j];
      j++;
    }
    k++;
  }

  // When we run out of elements in either L or M,
  // pick up the remaining elements and put in A[p..r]
  while (i < n1) {
    arr[k] = L[i];
    i++;
    k++;
  }

  while (j < n2) {
    arr[k] = M[j];
    j++;
    k++;
  }
}

// Divide the array into two subarrays, sort them and merge them
void mergeSort(int arr[], int l, int r) {
  if (l < r) {

    // m is the point where the array is divided into two subarrays
    int m = l + (r - l) / 2;

    mergeSort(arr, l, m);
    mergeSort(arr, m + 1, r);

    // Merge the sorted subarrays
    merge(arr, l, m, r);
  }
}

// Print the array
void printArray(int arr[], int size) {
  for (int i = 0; i < size; i++)
    printf("%d ", arr[i]);
  printf("
");
}

// Driver program
int main() {
  int arr[] = {6, 5, 12, 10, 9, 1};
  int size = sizeof(arr) / sizeof(arr[0]);

  mergeSort(arr, 0, size - 1);

  printf("Sorted array: 
");
  printArray(arr, size);
}
Comment

PREVIOUS NEXT
Code Example
C :: exclamation mark in c 
C :: add field to model rails 
C :: console log observable data 
C :: concatenate char * c 
C :: c fractional sleep 
C :: c define array size 
C :: function for quicksort in c 
C :: how to combine strings in c 
C :: arduino millis 
C :: c program to find minimum of 4 numbers using conditional operator in c 
C :: uuidv4 javascript 
C :: Area of a Circle in C Programming 
C :: Access denied creating xampp-control.ini 
C :: read a document from console in c 
C :: what is string::npos 
C :: binary to decimal in c 
C :: mpi example 
C :: functions in c 
C :: malloc contiguous 2d array 
C :: typedef c struct 
C :: c remove last charachter from string 
C :: C - program to create 1D array 
C :: FCFS algorithm in c to find average turnaround time and waiting time 
C :: Turn on the first n Bits in number 
C :: refresh a chromebook terminal 
C :: print 0 1 2 3 4 in c while loop 
C :: hash function in c 
C :: how to declare a struct in c 
C :: C/AL Convertion of Decimal to String/Text 
C :: how to only show tenths place in c 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =