/**
Program Name: Insertion Sort
Description: This Program Sorts an Array using Insertion Sort Algorithm
Author: Tauqirul Haque
*/
#include <stdio.h>
#include <conio.h>
#define MAX 10
void insertionSort(int []);
void displayArray(int []);
void main()
{
int array[MAX] = {-999, 78,3,567,99,34,65,34,12, 55}; //this first element is the sentinel
printf("\nThe Element Before Sorting .. ");
displayArray(array);
insertionSort(array);
printf("\nThe Element After Sorting ... ");
displayArray(array);
getch();
}
void insertionSort(int array[MAX])
{
int i;
for(i=2;i
{
int ptr = i-1;
int temp = array[i];
while(temp < array[ptr])
{
array[ptr+1]= array[ptr];
ptr--;
}
array[ptr+1] = temp;
}
}
void displayArray(int array[MAX])
{
printf("\n");
for(int i=1;i
{
printf("Array[%d] = %d \n",i,array[i]);
}
}
Program Name: Insertion Sort
Description: This Program Sorts an Array using Insertion Sort Algorithm
Author: Tauqirul Haque
*/
#include <stdio.h>
#include <conio.h>
#define MAX 10
void insertionSort(int []);
void displayArray(int []);
void main()
{
int array[MAX] = {-999, 78,3,567,99,34,65,34,12, 55}; //this first element is the sentinel
printf("\nThe Element Before Sorting .. ");
displayArray(array);
insertionSort(array);
printf("\nThe Element After Sorting ... ");
displayArray(array);
getch();
}
void insertionSort(int array[MAX])
{
int i;
for(i=2;i
{
int ptr = i-1;
int temp = array[i];
while(temp < array[ptr])
{
array[ptr+1]= array[ptr];
ptr--;
}
array[ptr+1] = temp;
}
}
void displayArray(int array[MAX])
{
printf("\n");
for(int i=1;i
{
printf("Array[%d] = %d \n",i,array[i]);
}
}
Comments