Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 04-01-2011
Member
 
Join Date: Dec 2010
Posts: 12
What is the use of insertion sort in data structure?

I am not able to get the topic insertion sort in data structure. This topic is quite similar to the sorting. This is what I am finding it. I don?t know whether my observation is true or not? I want to know the method used in insertion sort in data structure. If anybody knowing anything or having the solution to this question then please let me know. I am eagerly waiting for the reply as I am very keen in knowing the solution.
Reply With Quote
  #2  
Old 04-01-2011
Member
 
Join Date: May 2009
Posts: 525
Re: What is the use of insertion sort in data structure?

Insertion sort is implemented by inserting a particular element at the appropriate position. In this method the first iteration starts with comparison of 1st element with the 0th element. In the second iteration 2nd element is compared with the 0th and 1st element .In general every iteration an element is compared with all elements before it. During comparison if it is found that the element in question can be inserted at a suitable position then space is created for it by shifting the other element at the suitable position. This procedure is repeated for all elements in the array.
Reply With Quote
  #3  
Old 04-01-2011
Member
 
Join Date: May 2009
Posts: 532
Re: What is the use of insertion sort in data structure?

The insertion sort is also a type of sorting where it starts the comparison of element with the 0th element. It also stores and declares the array which is also done in the other sorting methods.

Code:
   Declare array AA
    store elements in to array AA
    set i=0 
     Repeat till i= 0 to i<=length of array-1                        // for passess
          Repeat for j=0 to j<i                    // for comparision
           check  if AA[j] >AA[i]             // for exchanging largest number
                                   int  k = AA[j]
                                       AA[j]=AA[i]
                              set h =i
                                    Repeat till  h>j     
                                            arr[h] = arr[h - 1] ;
                     	        arr[h+ 1] = k ;
                                  decrement h by 1            
                                   end of loop
        repeat step 6 to 14
      End of loop
   increment i by 1    
   repeat step  5 to 18
  end of loop
Reply With Quote
  #4  
Old 04-01-2011
Member
 
Join Date: May 2009
Posts: 620
Re: What is the use of insertion sort in data structure?

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

void main( )
{
	int arr[5] = { 25, 17, 31, 13, 2 } ;
	int i, j, k, temp ;
	clrscr( ) ;
	printf ( "Insertion sort.\n" ) ;
	printf ( "\nArray before sorting:\n") ;
	for ( i = 0 ; i <= 4 ; i++ )
               	printf ( "%d\t", arr[i] ) ;

             for ( i = 1 ; i <= 4 ; i++ )
	{

    for ( j = 0 ; j < i ; j++ )
	     {
	       if ( arr[j] > arr[i] )
	        {
		 temp = arr[j] ;
		arr[j] = arr[i] ;
		for ( k = i ; k > j ; k-- )
                             	arr[k] = arr[k - 1] ;
			arr[k + 1] = temp ;
			}
		}
	}
	printf ( "\n\nArray after sorting:\n") ;
	for ( i = 0 ; i <= 4 ; i++ )
		printf ( "%d\t", arr[i] ) ;
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "What is the use of insertion sort in data structure?"
Thread Thread Starter Forum Replies Last Post
What is bubble sort in data structure punguzhali Software Development 3 04-01-2011 03:49 AM
What do you mean by Merge sort in data structure fLUTE Software Development 3 04-01-2011 02:12 AM
What is the difference between binary tree sort and heap sort in data structure sRIPRIYA Software Development 3 04-01-2011 01:25 AM
what is Quick Sort or partition Exchanger in data structure Venugopala Software Development 3 03-01-2011 08:47 AM
Insertion of clean Excel data into oracle table using html Valerian Software Development 3 13-06-2009 09:34 PM


All times are GMT +5.5. The time now is 01:53 PM.