Thursday, October 28, 2010

Sorting an array with all even numbers followed by odd numbers


/*
 * Input  : A sequence of numbers
 * Output : Sorts the given sequence in a way as to get all 
 *          even numbers (in ascending order) followed by all odd
 *          numbers (in ascending order).
 * Method : First, sort the elements using any of the sorting
 *          technique.Then push all even numbers to the left
 *          and odd numbers to the right.   
 */


#include<stdio.h>
#include<stdlib.h>


int main()
{
  // Function Declaration
  void sort_even_odd();


  int *array,n,i;


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


  // Dynamically allocate memory for n elements
  array=(int *)calloc(n, sizeof(int));


   // Reading values to the list
  printf("Enter the elts:");
  for(i=0;i<n;i++)
    scanf("%d",&array[i]);


  // Invoking function sort_even_odd
  sort_even_odd(array,n);
  
  
  //display sorted array
  printf("sorted array:");
  for(i=0;i<n;i++)
    printf("%d ",array[i]);
  printf("\n");


  return 0;
}


void sort_even_odd(int *arr, int num)
{
  int i,j,temp,pos;
  
  // Bubble Sort
  for(i=0;i<num;i++) {
    for(j=i+1;j<num;j++) {
      if(arr[i]>arr[j])
      {
        temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
      }
    }
  }


  // Pushing all even numbers to the left
  j=0;
  for(i=0;i<num;i++)
  {
    if(arr[i]%2==0)
    {
      temp=arr[j];
      arr[j]=arr[i];
      arr[i]=temp;
      j++;
    }
  }


  // Note the end of the even numbers (pos)
  for(i=0;i<num;i++)
    if(arr[i]%2!=0)
    {
      pos=i;
      break;
    }


  // Sort all the numbers (odd) from pos till 
  // end of array 
  for(i=pos;i<num;i++) {
    for(j=i+1;j<num;j++) {
      if(arr[i]>arr[j])
      {
        temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
      } 
    }
  }   
}

Written by

Monday, October 25, 2010

Bubble Sort in C

/*
 * Input  : A sequence of n numbers <a1, a2, ..., an>
 * Output : Sorted list <a1', a2', ..., an'>
 *          of the input sequence in ascending order
 * Method : Compare each pair of adjacent items and swap if
 *          they are in the wrong order. The pass through the
 *          list is repeated untill no swaps are required.
 */

#include<stdio.h>
#include<stdlib.h>

int main()
{
  // Function declaration
  void bubble_sort(int *, int);

  int *array = NULL; // Pointer to int, initialize to nothing.
  int n, i;

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

  // Dynamically allocate memory for n elements
  array = (int *)calloc(n, sizeof(int));

  // Reading the elements
  printf("\nEnter the elements:");
  for (i = 0;i < n;i++)
    scanf("%d", &array[i]);

  // Invoke insertion_sort
  bubble_sort(array, n);

  // Display sorted array
  printf("sorted array:");
  for (i = 0;i < n;i++)
    printf("%d ", array[i]);
  printf("\n");

  return 0;
}

/*
 * Sorts the elements of the array in ascending order
 */
void bubble_sort(int *arr, int num)
{
  int i, j, temp;

  for (i = 0;i < num;i++) {
    for (j = i + 1;j < num;j++) {
      if (arr[i] > arr[j]) { // swapping
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
      }
    }
  }
}


Code can be downloaded @ GitHub:bubble_sort.c

Written by

Thursday, October 21, 2010

Selection Sort in C


/*
 * Input  : A sequence of n numbers <a1, a2, ..., an>
 * Output : Sorted list <a1', a2', ..., an'>
 *          of the input sequence in ascending order
 * Method : Find the minimum value in the list and swap
 *          it with the first position. Repeat the same for
 *          the remainder of the list, starting with the second
 *          position and advancing each time.
 */

#include<stdio.h>
#include<stdlib.h>

main()
{
  // Function declaration
  void selection_sort(int *, int);

  int *array = NULL; // Pointer to int, initialize to nothing.
  int n, i;

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

  // Dynamically allocate memory for n elements
  array = (int *)calloc(n, sizeof(int));

  // Reading the elements
  printf("\nEnter the elements:");

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

  // Invoke selection_sort
  selection_sort(array, n);

  // Display sorted array
  printf("sorted array:");

  for (i = 0;i < n;i++)
    printf("%d ", array[i]);

  printf("\n");
}

selection_sort(int *arr, int num)
{
  int i, j, small, pos, temp;

  for (i = 0;i < num;i++) {
    small = arr[i];
    pos = i;

    for (j = i;j < num;j++) {
      if (arr[j] < small) {
        small = arr[j];
        pos = j;
      }
    }

    temp = arr[i];
    arr[i] = small;
    arr[pos] = temp;
  }
}


Code can be downloaded @ GitHub:selection_sort.c

Written by

Saturday, October 16, 2010

Insertion Sort in C


/*
 * Input  : A sequence of n numbers <a1, a2, ..., an>
 * Output : Sorted list <a1', a2', ..., an'>
 *          of the input sequence in ascending order
 * Method : For any element, we assume all the elements
 *          to its left are already sorted. Shifting all
 *          the elements greater to it, in the left to one
 *          position right and then insert the key selected
 *          in the right position.
 */

#include<stdio.h>
#include<stdlib.h>

main()
{
  int *array = NULL; // Pointer to int, initialize to nothing.
  int n, i;

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

  // Dynamically allocate memory for n elements
  array = (int *)calloc(n, sizeof(int));

  // Reading the elements
  printf("\nEnter the elements:");

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

  // Invoke insertion_sort
  insertion_sort(array, n);

  // Display sorted array
  printf("sorted array:");

  for (i = 0;i < n;i++)
    printf("%d ", array[i]);

  printf("\n");
}

/*
 * Sorts the elements of the array in ascending order
 */
insertion_sort(int *arr, int num)
{
  int i, j, key;

  for (i = 1;i < num;i++) {
    // Select the element to be inserted to its
    // correct position
    key = arr[i];

    for (j = i - 1;j >= 0;j--) {
      if (arr[j] > key)
        arr[j+1] = arr[j];
      else
        break;
    }

    // Inserting to the right position
    arr[j+1] = key;
  }
}


Code can be downloaded @ GitHub:insertion_sort.c

Wednesday, October 13, 2010

Linked list in C


/*
 * List - Set of items organised sequentially
 * Linked list - A way to represent a list where by each
 *               item in the list part of a structure also
 *               contains a "link" to the structure
 *               containing the next item
 *
 * Basic operations on a linked list:
 *  1.Insert
 *  2.Traverse
 *  3.Search
 *  4.Display
 *  5.Delete
 */

/* PROGRAM TO PERFORM OPERATIONS ON A LINKED LIST */

#include<stdio.h>
#include<stdlib.h>

// Node structure

struct node {
  int d;

  struct node *next;
};

// Alias the node structure

typedef struct node* NODE;

// Initialize head pointer of the linked list to NULL
NODE head = NULL;

main()
{
  int ch;
  void insert();
  void display();
  void search();
  void delete();

  while (1) {
    printf("MENU\n1. Insert\n 2. Display\n 3. Search\n");
printf("4. Delete\n 5. Exit\n");
printf("Enter choice: ");
    scanf("%d", &ch);

    switch (ch) {

      case 1:
        insert();
        break;

      case 2:
        display();
        break;

      case 3:
        search();
        break;

      case 4:
        delete();
        break;

      case 5:
        exit(0);

      default:
        printf("Invalid Option :-(\n");
        break;
    }
  }
}

void insert()
{
  int v;
  NODE new, temp;
  printf("Enter value: ");
  scanf("%d", &v);

  // Create new node
  new = (NODE)malloc(sizeof(struct node));
  // Store the value for the node
  new->d = v;
  new->next = NULL;

  if (head == NULL) {
    // List is empty
    head = new;
  } else {
    // Otherwise
    temp = head;

    while (temp->next != NULL)
      temp = temp->next;

    temp->next = new;
  }
}

void display()
{
  NODE temp;

  if (head == NULL) {
    printf("List is empty\n");
    return;
  }

  printf("Values in list: ");

  temp = head;

  // Iterate through the list and
  // print the value at each node

  while (temp != NULL) {
    printf("%d ", temp->d);
    temp = temp->next;
  }

  printf("\n");
}

void search()
{
  int e;
  NODE temp;
  printf("Enter element: ");
  scanf("%d", &e);

  if (head == NULL) {
    printf("List is empty\n");
    return;
  }

  temp = head;

  // Iterate through the list to search for the element

  while (temp != NULL) {
    if (temp->d == e) {
      printf("Element found\n");
      break;
    }

    temp = temp->next;
  }

  if (temp == NULL)
    printf("Element not found\n");
}

void delete()
{
  int e;
  NODE temp, t;
  printf("Enter element: ");
  scanf("%d", &e);

  temp = head;

  // The item to be deleted is the first in the list

  if (temp->d == e) {
    head = temp->next;
    free(temp);
    printf("Deleted\n");
    return;
  }

  while (temp != NULL) {
    if (temp->d == e) {
      break;
    }

    t = temp;

    temp = temp->next;
  }

  // Item not present in the list
  if (temp == NULL)
    printf("Element not found\n");
  else {
    t->next = temp->next;
    free(temp);
    printf("Deleted\n");
  }
}


Code can be downloaded @ GitHub:linked_list.c

Saturday, October 9, 2010

Hello World in C without using semicolon


/**
  * C program to print "Hello World" without using semicolon
  */

#include <stdio.h>

void main(void)
{
  if(printf("Hello World\n")) {}
}