943,568 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 692
  • C++ RSS
Sep 19th, 2008
0

How to do Insertion Sort?

Expand Post »
I've got an array BM[] with a length of 5. How would I declare my function in the heading and what am I doing wrong in my code, it keep giving me errors. Also I dont know how to apply it to my program to do the sorting. Like would I write cout<<insertionSort(BM[])<<endl;

I need help, I'm kinda new to C++
C++ Syntax (Toggle Plain Text)
  1. insertionSort(array BM[])
  2. for i = 1 to length[BM]-1 do
  3. {
  4. value = BM[i]
  5. j = i-1
  6. while (j >= 0 && BM[j] > value)
  7. {
  8. BM[j + 1] = BM[j]
  9. j = j-1
  10. }
  11. BM[j+1] = value
  12. }
Last edited by Narue; Sep 19th, 2008 at 4:24 pm. Reason: added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
swbuko is offline Offline
12 posts
since Sep 2008
Sep 19th, 2008
0

Re: How to do Insertion Sort?

Try with something easy for start.
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Sep 19th, 2008
0

Re: How to do Insertion Sort?

Click to Expand / Collapse  Quote originally posted by Sci@phy ...
Try with something easy for start.
Well I have to do Insertion Sort or my program will run too slow; can you help me with learning how to do Insertion Sort?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
swbuko is offline Offline
12 posts
since Sep 2008
Sep 19th, 2008
0

Re: How to do Insertion Sort?

Here's a good link from Narue on many sorts, including the Insertion Sort. Go about 1/3 of the way down to find the Insertion Sort segment. It's an efficient sort on data that's mostly sorted already, but not so great for random lists. Narue explains on the link. An array of length 5 is pretty small, so any sort will work well on it.

http://eternallyconfuzzled.com/tuts/...t_sorting.aspx

This code:

C++ Syntax (Toggle Plain Text)
  1. insertionSort(array BM[])
  2. for i = 1 to length[BM]-1 do
  3. {
  4. value = BM[i]
  5. j = i-1
  6. while (j >= 0 && BM[j] > value)
  7. {
  8. BM[j + 1] = BM[j]
  9. j = j-1
  10. }
  11. BM[j+1] = value
  12. }

isn't valid C++ code. You need brackets surrounding the function implementation, you need parentheses in your for-loop declaration, and you have a do and a while, but it isn't in the form of a do-while loop. Also, what is length ? I'm not sure what language this is.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is online now Online
5,372 posts
since Jan 2008
Sep 20th, 2008
0

Re: How to do Insertion Sort?

It looks like pseudocode.

You really want to translate this into C++ , write the whole program and see what you get.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007

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: inheritance help
Next Thread in C++ Forum Timeline: few questions





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


Follow us on Twitter


© 2011 DaniWeb® LLC