Skip to main content

Insertion Sort In Data Structure

    ⇰  INSERTION SORT :-

Insertion sort  is one of the simplest method to sort an array. This is an in-place comparison-based sorting algorithm. The array is searched sequentially and unsorted items are moved left or right side according to order we want to sort. This sorting algorithm is not suitable for large data. Its average and worst case complexity is  Ο(n2), where n is the number of items in an array. 
     An example of insertion sort occurs in everyday life is while playing cards. Ones hand extract the card, and then shift the remaining cards and then insert the extracted card in the correct place. This process is repeated until all the cards are in the correct position or in correct sequence.


  FOR EXAMPLE :- 
   Let us take an unsorted array:-
                3 7 4 1 8 2
now the insertion sort technique compare the 1st element of array to 2nd element, if sequence or order is not correct then corrects it. 
               3 7 4 1 8 2

Comparing the 1st to 3rd element, if sequence or order is not correct then corrects it. 
               3 4 7 1 8 2

Comparing the 1st  to 4th element, if sequence or order is not correct then corrects it. 
              1 3 4 7 8 2

Compare the 1st  to 5th element, if sequence or order is not correct then corrects it. 
              1 3 4 7 8 2

Compare the 1st element of array to 5th element, if sequence or order is not correct then corrects it. 
              1 2 3 4 7 8
Hence at last array is in sorted order.



⇰ ALGORITHM OF INSERTION SORT :-

variable used:- L=list of array, n= no. of element in list, 
           I, J, temp = local variable  

Step1 :- Initialize 
              I = 1
Step2 :- Repeat through step3 to step5 while I<n.

Step3 :- temp = L[I] 
             J=I-1

Step4 :- Repeat while ( J>=0 and temp <L[j] )
             L [J+1] = L[J]
             J = J+1

Step5 :- L[ J+1] = temp
              I =I+1

Step6 :- Exit.



⇰  PROGRAM OF INSERTION SORT IN 'C' :-

# include <stdio.h>
# include <conio.h>

void main()
{
    int  i, j, temp, l[5];
 printf("Enter Values of array for sorting:- ");

for (i = 0 ; i < 5 ; i++)
{
  scanf ( " %d " ,& l[i] );
}

for (i = 1 ; i < 5 ; i++)
 {
   temp= l[i];
   for(  j=i-1;  j >=0  && temp < l[j] ; j-- )
    {
       l[ j+1] = l[j]; 
    }
     
   [ j+1] =temp;
 }

printf(" \n Sorted value :- ");

for ( i=0 ; i < 5; i++)
{
 printf(" \n %d" , l[i] );
}

getch();
}



Share, Follow and please comment if you find anything incorrect or to share more information about the topic discussed above.

Comments

Popular posts from this blog

Process Scheduling And Types of Process Schedular :-

        ⇰ PROCESS SCHEDULING Process Scheduling  is a task  of Operating System that schedules processes of different states like new, ready, waiting, terminated  and running.This scheduling helps in allocation of CPU time for each process, and Operating System allocates the CPU time for each procss. And the process scheduling plays important role to keep the CPU busy all the time.  ⏩   Followings are some objectives of Process Scheduling :-  i > To increase the amount of users within acceptable response times.  ii > To maintain the balance between response and utilization of system. iii > To decrease the enforce priorities and  give reference to the processes holding the key resources.      ⇰  PROCESS SCHEDULAR A scheduler carries out the pro cess scheduling work. Schedulers are often implemented so they keep all computer resources busy and  allows multiple users to shar...

Process & Its state And process control block :-

                ⇰  PROCESS :- A process can be thought of as a program in execution. Means when any program is executed it becomes process. A processwill need certain resources such as CPU time , memory, files and I/O devices to complete its task. These resources are allocated to the process either when it is created or at the time of execution.             A process is the unit of work in most systems. A system consistes of a collection of processes. All these processes may execute concurrently. Traditionally a process contained only a single thread. Most modern operating ststems now supports processes that have multiple threads.         The operating system is responsible for several important works of process management as - the creation and deletion of process, the schrduling of process, communication and deadlock handling of process. Process is broudly divided into two ...

Tokens and its types in 'C'

   Tokens are the smallest individual unit of a program or in simple words it is a main part of C program.Tokens are the building blocks of any program. The smallest individual and basic unit of a C programming is called c tokens.      *    Normally there are six types of tokens in C:- i> Keywords:-          Keywords are special words that are used to give a special meaning to the program and can't be used as variable and constant.They are basically a sequence of characters that have fixed to mean. For example:-                 auto     double      long     break                 float    short        char     if                while    continue   int       void etc. All...