Skip to main content

Insertion sort algorithm in C language

Insertion sort is used when number of elements is small. It can also be useful when input array is almost sorted, only few elements are misplaced in complete big array.
Insertion sort takes maximum time to sort if elements are sorted in reverse order. And it takes minimum time (Order of n) when elements are already sorted.

Following are the steps involved in insertion sort:
  1. We start by making the second element of the given array, i.e. element at index 1, the key. The key element here is the new card that we need to add to our existing sorted set of cards(remember the example with cards above).
  2. We compare the key element with the element(s) before it, in this case, element at index 0:
    • If the key element is less than the first element, we insert the key element before the first element.
    • If the key element is greater than the first element, then we insert it after the first element.
  3. Then, we make the third element of the array as key and will compare it with elements to it's left and insert it at the right position.
  4. And we go on repeating this, until the array is sorted.
Let's consider an array with values {5, 1, 6, 2, 4, 3}


code .......

// Insertion sort algorithm in C language
#include <stdio.h>
int main()
{
  int a[5]={4,2,0,6,1},n=5,i,j,temp;
  //start sorting
  for(i=1;i<n;i++){
    j=i;
    while(j>0 && a[j-1] > a[j]){
      temp = a[j];
      a[j]= a[j-1];
      a[j-1] = temp;
      j--;
    }
  }
  // Print array
  for(i=0;i<n;i++){
    printf("%d\n", a[i]);
  }
  return 0;
}


if you have any query comment below ...

Comments

Popular posts from this blog

PHP Hacker rank Question solution Mini-Max sum in PHP

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers. For example,  . Our minimum sum is   and our maximum sum is  . We would print 26 24 Function Description Complete the  miniMaxSum  function in the editor below. It should print two space-separated integers on one line: the minimum sum and the maximum sum of   of   elements. miniMaxSum has the following parameter(s): arr : an array of   integers Input Format A single line of five space-separated integers. Constraints Output Format Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly  four  of the five integers. (The output can be greater than a 32 bi...

PHP Hacker Rank Question "Time Conversion" Solution in php

Given a time in  -hour AM/PM format , convert it to military (24-hour) time. Note: Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock. Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock. Function Description Complete the  timeConversion  function in the editor below. It should return a new string representing the input time in 24 hour format. timeConversion has the following parameter(s): s : a string representing time in   hour format Input Format A single string   containing a time in  -hour clock format (i.e.:   or  ), where   and  . Constraints All input times are valid Output Format Convert and print the given time in  -hour format, where  . Solution : ---------------------------------- <?php /* Write your PHP code here */ $s = "12:45:54PM";  $t= $c = substr($s,0,2); $a = (int)$t; $sym = su...

PHP Hacker Rank "Apple and Orange" Solution

Sam's house has an apple tree and an orange tree that yield an abundance of fruit. In the diagram below, the red region denotes his house, where   is the start point, and   is the endpoint. The apple tree is to the left of his house, and the orange tree is to its right. You can assume the trees are located on a single point, where the apple tree is at point  , and the orange tree is at point  . When a fruit falls from its tree, it lands   units of distance from its tree of origin along the  -axis. A negative value of   means the fruit fell   units to the tree's left, and a positive value of   means it falls   units to the tree's right. Given the value of   for   apples and   oranges, determine how many apples and oranges will fall on Sam's house (i.e., in the inclusive range  )? For example, Sam's house is between   and  . The apple tree ...