943,990 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 11378
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 28th, 2007
0

Insertion Sort

Expand Post »
Hi to all ,

I wished if any one of you could throw light on mechanism of insertion sorting in C++. I know what is bubble sorting but I had confusion regarding what is insertion sorting? In school my professor had mentioned about it, but I do not have any idea about it.
I got mixed up. I searched for it at the net but no clear ideas were given. It has been confusing for 7 days!!
I am unable to go about it.
I would appreciate if you could give an example of it ,so that i can understand it better.
Please help.

Thanks in advance.
Hinduengg
Last edited by hinduengg; Jun 28th, 2007 at 2:12 am.
Similar Threads
Reputation Points: 36
Solved Threads: 4
Junior Poster in Training
hinduengg is offline Offline
88 posts
since Jun 2007
Jun 28th, 2007
1

Re: Insertion Sort

Hinduengg,

Insertion sorting is simply inserting each of the unsorted values into the list of sorted values in the location that keeps the list of sorted values correctly sorted. e.g. to sort in ascending order, insert each of the unsorted values into the sorted list after all values that are smaller than it, and before all values that are larger than it.

visual e.g.:
C++ Syntax (Toggle Plain Text)
  1.  
C++ Syntax (Toggle Plain Text)
  1. unsorted list:
  2. 3 1 5 4 2
  3. sorted list:
  4. 1) (insert '3')
  5. 3
  6.  
  7. 2) (insert '1'; '3' is shuffled to the right)
  8. 1 3
  9.  
  10. 3) (insert '5')
  11. 1 3 5
  12.  
  13. 4) (insert '4'; '5' is shuffled to the right)
  14. 1 3 4 5
  15.  
  16. 5) (insert '2'; '3', '4' & '5' are shuffled to the right)
  17. 1 2 3 4 5
Last edited by dougy83; Jun 28th, 2007 at 5:22 am.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Jun 28th, 2007
0

Re: Insertion Sort

Thank you dougy83 . What is meant by function-insert ?I wished you could give the example in cplusplus syntax for I have to use it in C++.
If an array is inputted like this:

if the array is- [2][7][3][9][4]
then how will insertion sorting occur to sort it.

will it take out first 2
then read the entire array for finding next - 3
then again after entire process- 4
then next in the order would be-7
then -9
so that the ordered array comes to
[2][3][3][7][9]

Please explain, I can not get it
Last edited by hinduengg; Jun 28th, 2007 at 7:39 am.
Reputation Points: 36
Solved Threads: 4
Junior Poster in Training
hinduengg is offline Offline
88 posts
since Jun 2007
Jun 28th, 2007
4

Re: Insertion Sort

What you've just described is selection sort, where you select the minimum element from an array and append that onto an array that you're growing.

Insertion sort takes out the first element of the remaining elements and inserts it into the right place. To sort [2 7 3 9 4], you'd sort in the following fashion, where the list on the left is the sorted list that we're building, and the list on the right is the unsorted list we're taking elements from:

[] [2 7 3 9 4]
-- take the 2 and insert it 'in the right place'
[2] [7 3 9 4]
-- take the 7 and insert it 'in the right place'
[2 7] [3 9 4]
-- take the 3 and insert it 'in the right place'
[2 3 7] [9 4]
-- take the 9 and insert it...
[2 3 7 9] [4]
-- take the 4 and insert it...
[2 3 4 7 9] []
-- we're done!

And the sorted list becomes [2 3 4 7 9].

Now, I've been using the word 'list' instead of array, but that's only because I'm speaking abstractly. You can do an insertion sort on an array, in the fashion described above, in-place. Instead of changing the sizes of two 'lists' (whatever that means in the context of programming), you can simply walk an index through the array, from 0 to the size of the array. The part of the array before the index is sorted, and the part of the array after the index is the unsorted part.

For example, above, after you've taken the 3 and inserted it into the sorted portion, the array will look like [2 3 7 9 4], and the index will currently be 3, so the next element you take out will be 9.
Last edited by Rashakil Fol; Jun 28th, 2007 at 9:03 am.
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Jun 28th, 2007
0

Re: Insertion Sort

Thank you so much for the SUPERB explanation .

Now I would try out a program that implements insertion sort and see to it whether I am successful .

Loads of thanks once again.
Reputation Points: 36
Solved Threads: 4
Junior Poster in Training
hinduengg is offline Offline
88 posts
since Jun 2007
Jun 28th, 2007
0

Re: Insertion Sort

Click to Expand / Collapse  Quote originally posted by hinduengg ...
Thank you so much for the SUPERB explanation .

Now I would try out a program that implements insertion sort and see to it whether I am successful .

Loads of thanks once again.
now it's time to write your own insertion method

no better way to learn than with your very own hands
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Jun 30th, 2007
0

Re: Insertion Sort

I haven't yet started this assignment for I have to submit my E.V.S PROJECT within the next 2 days , so I am really busy in that.

On another note, I wanted to know what are actually random nos., because I have often seen all of you using it quite a many times.
Reputation Points: 36
Solved Threads: 4
Junior Poster in Training
hinduengg is offline Offline
88 posts
since Jun 2007
Jul 1st, 2007
0

Re: Insertion Sort

Click to Expand / Collapse  Quote originally posted by hinduengg ...
I haven't yet started this assignment for I have to submit my E.V.S PROJECT within the next 2 days , so I am really busy in that.

On another note, I wanted to know what are actually random nos., because I have often seen all of you using it quite a many times.
what are random numbers?

just that random numbers

http://www.daniweb.com/forums/post394829-10.html

this is an array pointer example i wrote, but it deals with random numbers
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Jul 1st, 2007
0

Re: Insertion Sort

Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 2nd, 2007
0

Re: Insertion Sort

Thanks Killer Typo and Narue.


I understand about random nos. now.
But regards insertion sort , the site 's code is in c not in c++ and i long for C++ for it doesn't have ->signs , I believe, I understand pointers to some extent but not struct that well , I wondered whether you could simplify it with C++ syntax and using for loops. Is there any established site where I can get syntax only pertaining to C++ like the eternally confused.


Please help and advice,
hinduengg
Last edited by hinduengg; Jul 2nd, 2007 at 10:47 am.
Reputation Points: 36
Solved Threads: 4
Junior Poster in Training
hinduengg is offline Offline
88 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Programing functions and having a problem with one of the functions.
Next Thread in C++ Forum Timeline: Two-dimensional char array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC