Search Program on this blog

Friday 21 August 2015

C- Program for insertion sort

//C-Program for insertion sort


#include<stdio.h>
#include<time.h>
#define MAX_SIZE 10
void swap(int *a,int *b){
  int temp;
  temp=*a;
  *a=*b;
  *b=temp;
}
void sort(int *arr)
{
  int i,j;
  for(i=1;i<MAX_SIZE;i++){
    for(j=i;j>0;j--){
      if(arr[j]<arr[j-1]){
        swap(&arr[j],&arr[j-1]);
      }
    }
  }
}

int main()
{
  int i;
  int arr[MAX_SIZE]={10,20,30,40,50,60,65,70,76,80};
  for(i=0;i<MAX_SIZE;i++) //Array before sorting
    printf("%d ",arr[i]);
  sort(arr);
  printf("\n\n");
  for(i=0;i<MAX_SIZE;i++) //Array after sorting
    printf("%d ",arr[i]);
  return 0;
}

No comments:

Post a Comment