CODE OF GEEKS


A PLATFORM TO LEARN | CODE | ACHIEVE


" Programming is the real test of your creativity,its all about how you turn ideas into codes. "



Connect :


THIS WEBSITE IS BEST VIEWED IN THE GOOGLE CHROME WEB BROWSER.



TO PERFORM INSERTION SORT IN C

INSERTION SORT


Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages.

To begin,the leftmost number is considered fully sorted.Next,from the remaining numbers the leftmost number is taken out and compared to the already number to its left.If the already sorted number is larger,the two number swaps.This operation repeats until a number smaller appears,or the number reaches the left edge.

Worst-case performance : O(n2)
Best-case performance : O(n)
Average performance : O(n2)


Code

#include<stdio.h>
int main()
{
 int a[100];
 int size;
 int i;
 int pos;
 int temp=0;
 printf("***** INSERTION SORT - CODE OF GEEKS *****\n\n");
 printf("ENTER THE NUMBER OF ELEMENTS ? \n");
 scanf("%d ",&size);
 printf("ENTER THE ARRAY : \n")
 for(i=0;i<size;i++)
 {
  scanf("%d ",&a[i]);	
 }
 for(i=1;i<=size-1;i++)
 {
  pos=i;
  while(pos>0 && (a[pos-1]>a[pos]))
  {
   temp=a[pos];
   a[pos]=a[pos-1];
   a[pos-1]=temp;	
   pos--;
  }	
 }
 printf(" SORTED ARRAY : \n ");
 for(i=0;i<size;i++)
 {
  printf("%d ",a[i]);	
 }
 return 0;
}

Output


***** INSERTION SORT - CODE OF GEEKS *****


ENTER THE NUMBER OF ELEMENTS ?
6
ENTER THE ARRAY :
23
12
53
12
35
26
SORTED ARRAY :
12 12 23 26 35 53


insertion sort

Join us :



CODE OF GEEKS

A Platform to LEARN | CODE | ACHIEVE

Connect :



INTERESTING READ FOR YOU :

SHARE:



For any Queries,suggestions,feel free to Mail us at :
codeofgeeks@gmail.com

For reporting any kind of copyright voilation,mail us at : cogcopyclaim@gmail.com

© copyright CODE OF GEEKS 2018