Monday, February 21, 2011

Selection Sort in C

/*
 * At every iteration, this sorting finds the smallest element
 * in the unsorted subarray and place it in its correct location.
 * Initially the full array = unsorted subarray. If an array has
 * n elements, after 1 iteration, the unsorted subarray contains
 * n-1 elements ( all elements except the smallest one,which is
 * placed at index 0) 
 */


#include<stdio.h>


int main(void)
{
  int i,n,arr[20],j;
  int min,pos,temp;
  printf("Enter the element size:");
  scanf("%d",&n);


  printf("Enter the array elements:");
  for(i=0;i<n;i++)
    scanf("%d",&arr[i]);


  for(i=0;i<n;i++)
  {
    //Assume ith element to be min 
    min=arr[i];
    //Compare with all other elements to find the minimum one
    for(j=i+1;j<n;j++)
    {
      if(arr[j]<min)
      {
        min=arr[j];
        pos=j;
      }
    }
   //If assumed min is not equal to actual min
   // swap their locations
    if(min!=arr[i])
    {
      temp=arr[i];
      arr[i]=min;
      arr[pos]=temp;
    }
  }


  printf("The sorted array\n");
  for(i=0;i<n;i++)
    printf("%d ",arr[i]);
  
  return 0;
}

Written by

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