473,395 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,395 developers and data experts.

Insertion Sort

Ganon11
3,652 Expert 2GB
So far, the Articles sections are filled with slow sorting algorithms. Bubble Sort and Selection Sort, while very easy to comprehend, are relatively slow algorithms at Θ(n^2) running time. Here, I will try to explain an algorithm to sort an array of numbers that is fast for small sets of data, though it is still an O(n^2) algorithm; the Insertion Sort.

Suppose we have an array of n-1 elements that is already sorted in increasing order. We then come to the nth element (let us call it x) to be ‘inserted’ into this sorted array. It follows that there is some portion of the array in which the elements are less than x, and another portion in which the elements are greater than x. Further, because we know that the existing array is sorted, we know that the array generally looks like this:

[elements < x][elements>x]

We begin the algorithm by creating a hole at the end of the array:

[elements<x][elements>x][empty hole]

Now, we want to have our array completely sorted for all n elements at the end, so the alert reader may realize that the array must look like this when we are done at this step:

[elements<x][x][elements>x]

It follows that all of the elements greater than x must shift down the array one slot. We can, in fact, do this very simply by moving any element a[j] to a[j+1] as long as a[j] is greater than x. We start this moving at the element just before the hole and continue until the element we check is less than x. Now every element has been shifted one slot and overwritten every previously copied value except for the last to be shifted, of which there remains 2 copies adjacent to each other. We know that this element is greater than x (or else we would not have shifted it) and so we place x in the position that element originally held.

A powerful note to this algorithm is that it can be implemented to use no extra space. If we consider only the first I elements of any array as our sorted array, then creating a hole is as simple as increasing our array’s size, making a copy of the last term (which must be inserted into its sorted position), and following our steps above. The algorithm in pseudocode is presented below:

Expand|Select|Wrap|Line Numbers
  1. void insertion_sort(int[] array) {
  2.     for (Loop i from 1 to array.size-1)
  3.         int x = array[i]
  4.         int j = array[i-1]
  5.         while (j >= 0 && array[j] > x)
  6.             array[j+1] = array[j]
  7.             j = j - 1
  8.         array[j+1] = x
  9. }
Nov 16 '07 #1
0 4056

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: N. | last post by:
Insertion sort is O(N^2), but I figure I can get it in to O(N log2 N) if the inside loop of the insertion sort is replaced with a binary search. However, I'm having some implimentation problems......
4
by: ashu | last post by:
can anyone tell me that what is the logic of insertion sort. thank you
1
by: Franky, Mondestin | last post by:
Problem description: I have a text file with 700 lines. Each line has 7 elements separated by comma. My text file looks something like : a,b,c,d,e,f,g d,e,h,k,l,m,n x,c,v,f,g,t,z .... and so on...
5
by: Am | last post by:
hi i came to know that microsoft improved the efficiency of quick sort by using a cutoff of 8 elements and continuing with insertion sort then, do anybody have the details about it please...
11
by: Am | last post by:
hi i came to know that microsoft improved the efficiency of quick sort by using a cutoff of 8 elements and continuing with insertion sort then, do anybody have the details about it please...
6
by: Julia | last post by:
I am trying to sort a linked list using insertion sort. I have seen a lot of ways to get around this problem but no time-efficient and space-efficient solution. This is what I have so far: ...
19
by: tkpmep | last post by:
I have an ordered list e.g. x = , and given any positive integer y, I want to determine its appropriate position in the list (i.e the point at which I would have to insert it in order to keep the...
1
by: AhmedGY | last post by:
Hi, am trying to build an app that uses the insertion sort method to sort numbers entered in a textbox and display them sorted in a label, so i wrote this inside the sort button click event: ...
9
by: python_newbie | last post by:
I don't know this list is the right place for newbie questions. I try to implement insertion sort in pyhton. At first code there is no problem. But the second one ( i code it in the same pattern i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.