Skip to content

Instantly share code, notes, and snippets.

@mbalayil
mbalayil / merge_sort.c
Created April 1, 2011 18:21
Merge Sort using recursion in C
/**
* Divide : Divide the n-element array into two n/2-element
* sub-arrays
* Conquer : Sort the two sub-arrays recursively using
* merge sort
* Combine : Merge the two sorted subsequences to form the
* sorted array
**/
#include<stdio.h>
@mbalayil
mbalayil / quick_sort.c
Created May 2, 2011 18:12
Quick Sort using recursion in C
/** Divide : Partition the array A[low....high] into two sub-arrays
* A[low....j-1] and A[j+1...high] such that each element
* of A[low....j-1] is less than or equal to A[j], which
* in turn is is less than or equal to A[j+1...high]. Compute
* the index j as part of this partitioning procedure.
* Conquer : Sort the two sub-arrays A[low....j-1] and A[j+1....high]
* by recursive calls to quicksort
**/
#include<stdio.h>
@mbalayil
mbalayil / swap_add_mul.c
Created May 3, 2011 06:16
Swapping without using a temporary variable in C - Addition & Subtraction
/**
* Swapping without using a temporary variable in C
* (Addition & Subtraction)
**/
#include<stdio.h>
int main(void)
{
int a = 5, b = 10;
@mbalayil
mbalayil / binary_search.c
Created May 5, 2011 10:57
Binary Search in C
/** Binary Search locates the position of an item in a sorted list.
*
* Divide : It works by dividing the list into two halves and
* comparing the input value to be searched with the
* middle element of the list.
*
* Conquer : If the input value is equal to the mid value, search
* is successful. If greater, call binary search only
* on the greater sub-array of the sorted list and if
* lesser, call binary search on the lesser sub-array.
@mbalayil
mbalayil / swap_mul_div.c
Created May 8, 2011 18:48
Swapping without using temporary variable in C - Multiplication & Division
/**
* Swapping without using a temporary variable in C
* (Multiplication & Division)
**/
#include<stdio.h>
int main(void)
{
int a = 5, b = 10;
@mbalayil
mbalayil / swap_xor.c
Created May 8, 2011 19:05
Swapping without using temporary variable in C - XOR
/**
* Swapping without using a temporary variable in C
* (XOR)
**/
#include<stdio.h>
int main(void)
{
int a = 5; /* a = 0101 */
@mbalayil
mbalayil / check_prime.c
Created May 13, 2011 06:08
To check whether a number is prime or not
/**
* To check whether a number is prime or not
**/
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int n, i, p = 1;
@mbalayil
mbalayil / factorial.c
Created May 13, 2011 11:02
To find the factorial of a number in C
/**
* To find the factorial of a number
**/
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int n, i, f;
@mbalayil
mbalayil / palindrome.c
Created May 13, 2011 14:07
To check whether a string is a palindrome or not
/**
* A palindrome is a word, phrase, number or other sequence
* of units that can be read the same way in either direction
**/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
@mbalayil
mbalayil / substring.c
Created May 15, 2011 12:32
Check the occurrences of a pattern in a string
/**
* Program to check the occurrences of a pattern in a text/string
* This program checks whether the pattern occur in the string
* or not, if yes at what positions and also print the text/string
* highlighting the occurrences
**/
#include <stdio.h>
#include<string.h>