473,383 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

insertion sort algo

consider this insertion sort algo(from cormen)
INSERTION-SORT(A)
Expand|Select|Wrap|Line Numbers
  1.     for j<--- 2 to length[A]
  2.         do key<--- A[j]
  3.         >Insert A[j] into the sorted seq A[1......j-1].
  4.         i <---  j -1
  5.         while i > 0 and A[i] > key 
  6.             do A[i+1]<--- A[i]
  7.             i <--- i - 1
  8.         A[i+ 1] <--- key
  9.  
i'm unable to understand from line 6 to 8.
in 6) we put value of A[i] in A[i+1] , then again we put value of "key " in A[i+1] in line 8).How's that possible.what is the meaning of line 7) i'm unable to interpret it.
plz help, where i'm making mistake
Apr 18 '10 #1
1 2584
Banfa
9,065 Expert Mod 8TB
The whole thing makes more sence with the white space preserved so the structure is clear. I have edited your post to do this. Also not this sudo code starts arry/list indexes at 1.

Lines 6 and 7 are responsible for moving the correct part of the currently sorted list down 1 item at a time until a space is made for the item currently being sorted at the correct location to maintain the sorting.

Line 8 then puts the item currently being sorted into that space.

For example imagine we are sorting the list

1, 3, 5, 7, 4, <OtherData>

and j is 5

key = A[j] = 4

After running the loop in lines 4 - 7 the list looks like this

1, 3, 5, 5, 7, <OtherData>

and i has a value of 2

Line 8 reinserts the key at i+1

A[i+1] = key = 4

And the list finishes that iteration as

1, 3, 4, 5, 7, <OtherData>

ready to sort the <OtherData>
Apr 18 '10 #2

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
2
by: Bit byte | last post by:
I just came accross this : "Note that the STL sort algorithm does NOT work for lists; that's why a sort member function is supplied." Is this true? Has this been fixed in newer versions of the...
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...
0
Ganon11
by: Ganon11 | last post by:
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...
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...
2
by: Gestorm | last post by:
Suppose we have an array a, the idea is: build another array, int next, for each 0<i<N, next = next position of a in the sorted array, if a is the max, then next is undefined. For example, let the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.