C Code For Insertion Sort
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.
Leave a Reply
Name (required)
Website
Submit Comment
824
6shRk7VqSCXulH
CAPTCHA Code
partner-pub-2986
ISO-8859-1
Search
programminggeek w w w .google.co.
Programminggeek en_US
Subscribe
Delivered by FeedBurner
Login
Username:
Password:
Remember me
Login
1 1
o o o o
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