Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
merge_sort.c File Reference

Implementation of merge sort algorithm. More...

#include <stdio.h>
#include <stdlib.h>
Include dependency graph for merge_sort.c:

Functions

void swap (int *a, int *b)
 Swap two integer variables.
 
void merge (int *a, int l, int r, int n)
 Perform merge of segments.
 
void merge_sort (int *a, int n, int l, int r)
 Merge sort algorithm implementation.
 
int main (void)
 Main function.
 

Detailed Description

Implementation of merge sort algorithm.

Function Documentation

◆ main()

int main ( void  )

Main function.

107{
108 int *a, n, i;
109 printf("Enter Array size: ");
110 scanf("%d", &n);
111 if (n <= 0) /* exit program if arraysize is not greater than 0 */
112 {
113 printf("Array size must be Greater than 0!\n");
114 return 1;
115 }
116 a = (int *)malloc(n * sizeof(int));
117 if (a == NULL) /* exit program if can't malloc memory */
118 {
119 printf("Can't Malloc! Please try again.");
120 return 1;
121 }
122 for (i = 0; i < n; i++)
123 {
124 printf("Enter number[%d]: ", i);
125 scanf("%d", &a[i]);
126 }
127
128 merge_sort(a, n, 0, n - 1);
129 printf("Sorted Array: ");
130 for (i = 0; i < n; i++)
131 {
132 printf("%d ", a[i]);
133 }
134 printf("\n");
135
136 free(a);
137
138 return 0;
139}
void merge_sort(int *a, int n, int l, int r)
Merge sort algorithm implementation.
Definition merge_sort.c:87
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition malloc_dbg.h:18
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26
Here is the call graph for this function: