You are on page 1of 4

C code for Insertion Sort

Posted by Saurabh on July 17th, 2010 Writing programs for sorting of a given set of numbers is one of the common programming tasks. Various types of sorting techniques like selection sort, inserting sort and quick sort are quite popular. Here C code for Insertion sort is being presented. The program is quite simple. Here, in each iteration, the elements are placed in their correct position. The algorithm has the complexity of O(n)
#include "stdio.h" void main( ) { int arr[5] = { 25, 17, 31, 13, 2 } ; int i, j, k, temp ; 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] ) ; }

Related Posts

Java Code for generating Unique Random Numbers Java code for shuffling C Code for Quick Sort C Code for Bubble Sort C Code for Selection Sort

Posted in C, Codes Tags: sorting Googles Do-It-Yourself App Creation Software ROILA, a New Spoken Language Designed for Robots You can leave a response, or trackback from your own site.

3 Responses to C code for Insertion Sort


1. Mark4 says: July 7, 2011 at 11:53 am Thank You^6 Reply 2. mahesh says: September 11, 2011 at 6:18 pm this is good.thankz Reply 3. Kumar Ritu Raj says: October 7, 2011 at 11:53 am Hey Bro , why are you using 3 loops it looks a bit complicated. Try this one http://electrofriends.com/source-codes/software-programs/c/sorting-programs/programto-sort-the-numbers-using-insertion-sort/ Reply

Leave a Reply

Name (required)

Mail (will not be published) (required)

Website

Submit Comment

824

6shRk7VqSCXulH

CAPTCHA Code

Subscribe! Follow me!

partner-pub-2986

ISO-8859-1

Search

programminggeek w w w .google.co.

Subscribe via Mail


Enter your email address:

Programminggeek en_US

Subscribe

Delivered by FeedBurner

Login
Username:

Password:

Remember me
Login
1 1

o o o o

Register Lost your password? 2011 2010

Archives

Copyright Programming Geeks Coding Logic - For the crazy programming geeks | Shop Free Cellular Phones at Bestincellphones.com. | Thanks to Best CD Rates, iCellPhoneDeals.com Offers Best Cell Phone Deals. and Incinerador De Grasa