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

See this code in :


C++

Java

Also see :


PROGRAM TO PERFORM LINEAR SEARCH FOR SEARCHING AN ELEMENT IN ARRAY

PROGRAM TO PERFORM BINARY SEARCH FOR SEARCHING AN ELEMENT IN ARRAY

PROGRAM TO PERFORM BUBBLE SORT

PROGRAM TO PERFORM SELECTION SORT

PROGRAM TO GENERATE A FIBONACCI SERIES UPTO N NUMBERS USING RECURSION

PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS AN ARMSTRONG NUMBER OR NOT

Participate in our MONTHLY CHALLENGE,and get a chance to get listed on our site and win the TITLE of "MR.GEEK".


RUSH TO OUR MONTHLY CHALLENGE

Join us :



CODE OF GEEKS

A Platform to LEARN | CODE | ACHIEVE

Connect :



INTERESTING READ FOR YOU :

TOP PROGRAMMING LANGUAGES THAT ARE RULING IN 2018.
TOP PROGRAMMING LANGUAGES FOR WEB DEVELOPMENT IN 2018.

SHARE:



For any Queries,suggestions,feel free to Mail us at :
[email protected]

For reporting any kind of copyright voilation,mail us at : [email protected]

© copyright CODE OF GEEKS 2018