C Tutorial

C Tutorial C Language Environment Setup Execution flow of C program C printf and Scanf C Data type C Token Variable in C Operators in C Comments in C Escape Sequence in C C – Storage Classes C Decision control statement Loop Statement in C Break, continue and goto statement in C Type Casting in C Function in C Recursion in C String in C C Array Pointer in C Dynamic memory allocation C –Structure Nested Structure in C Union in C File Handling in C C pre-processor Static Function In C Sizeof In C Selection Sort In C Scope Of Variables In C Runtime Vs Compile Time In C Random Access Lseek In C Queue Implementation In C Pseudo Code In C Prototype In C Pointer To Pointer In C Pointer Arithmetic In C Passing Array To Function In C Null Character In C Merge Sort In C Macros In C Library Functions In C Memory Leak In C Int In C Goto And Labels In C Fibonacci Series In C Fflush In C Derived Data Types In C Data Types In C Const Vs Volatile In C Character Set In C Character Class Tests In C Calloc In C C Pointers Arrays In C Include In C Clrscr In C C Vs Java String Literals In C Types Of Pointers In C Variables In C Volatile In C Why C Is A Middle Level Language Infix To Postfix Program In C Ceil function in C LCM of two numbers in C Quick sort in C Static in C function pointer as argument in C Top Array Keywords in C Add two numbers using the function in C Armstrong program in C using function Array, Declaring Arrays and Array Initialization Limitations of Inline Function in C Merge and Merge sort with example in C Do-While Loop in C For Loop in C While-Loop in C Difference between while and do-while loop in C Array Of Structures in C Data Structures And Algorithms in C Types Of Structures In C How to Avoid Structure Padding in C Use of Structure in C Do WHILE LOOP in C Programming Examples For Loop in C Programming Examples Entry Control Loop in C Exit control loop in C Infinite loop in C Nested loop in C pow() function in C String Handling functions in C

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique.

  • Compare a[0] and b[0] first. Put whichever is smaller in c[0], say b[0].
  • Compare and contrast a[0] with b[1]. Put whichever of the two is smaller, say b [1,] into c[1].
  • Compare and contrast a [0] with b[2], put whichever is smaller, such as a [0], into c[2].
  • Compare a [1] with b[2] and so on. One of the arrays a[] or b[] will eventually be exhausted.
  • The remaining elements in the other array are simply copied into c[] at this point.

In file mergesort.h

#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
void merge ( int a [ ], int b [ ], int c [ ], int m, int n ) ; 
void mergesort( int key [ ], int n )  ;
void wrt( int key [ ], int sz ) ;
in file merge.c
/ * merge a [ ] of size m\n and b [ ] of size n into c [ ] * /
# include “mergesort.h”
void merge ( int a [ ], int b [ ], int c [ ], int m, int n )
{
	int i = 0, j = 0, k = 0 ;
	while ( i< m && j < n )
		if ( a [ i ] < b [ j ] ) 
			c [ k  + + ] = a [ i + + ] ;
		else
			c [ k  + + ] = b [ j + + ] ;
	while ( i< m )
		c [ k  + + ] = a [ i + + ] ;
	while ( j< n )
		c [ k  + + ] = a [ j + + ] ;
}

It is expected that the array c [ ] has adequate area to accommodate both a [ ] and b[ ]. The programmer must ensure that the c[] boundaries are not exceeded. It's worth noting that one or both of the last two while statements, which are intended to gather up any remaining values, are not performed. This is due to the fact that at least one of the two criteria i< m and j< n will be false.

Next, we'll create a mergesort() method that calls merge(). A merge sort is far more efficient than a bubble sort. Our mergesort() function will operate on an array key[, which has a size of a power of two. The "power of two" condition will simplify the explanation. Let's pretend key [] has the following 16 numbers:

4316755804-53774291-1

The data will be processed by the algorithm in several stages. The table below depicts how we want the data to be appeared after each pass:

Unordered data
4316755804-53774291-1
First pass
3416785504-5374729-11
Second pass
1346704855-54737-1129
Third pass
0134485567-5-11247937
Fourth pass
-5-101123444789375567

We want each succeeding pair of integers to be in order after the first pass. We want each succeeding quartet of integers to be in order after the second pass. We want each subsequent octet of Integers to be in order on the third pass. Finally, we want all 16 integers to be in order after the fourth run. Merge() is used at each stage to get the desired ordering. After the third iteration, for example, we have two sub-arrays.

0    1     3     4     4     8     55     67      and      -5     -1     1     2     4     7     9     37

Both of which are correct. We get the totally sorted array in the last line of the table by combining these two subarrays. Surprisingly, the code that does this is rather quick, demonstrating the power of pointer arithmetic.

In file mergesort.c
/ * mergesort : using merge ( ) for sorting an array if size n. * / 
#include "mergesort.h"
void mergesort (int keyl], int n)
{
Int J, K, m, * w ;
for ( m = 1 ; m < n ; m * =  2 ) 		/ * m is a power of 2 * /
if ( n< m ) {
print( " ERROR: Array size not a power of 2 – end !  \n " ) ;
exit ( 1 ) ;
	}
w= calloc( n, sizeof ( int ) ) ;			/ * allocate workspace * /
assert ( w 1 = NULL ) ;			/ * check that calloc ( ) worked * /
for ( k = 1 ; k < n ; K * = 2 ) {
for ( j = 0 ; j < n – K ; i + =  2 * k )
/ *
/ / Merge two subarrays of key [ ] into a subarray of w [ ].
* /
Merge ( key + j , key + j + k, W + J, k, k ) ;
for ( i = 0; j < n ; + + j )
key [ j ] = w [ j ] ;				/ * write w back into key * /
}
free(w);					/ *  free the workspace * /
}



ADVERTISEMENT
ADVERTISEMENT