How to do Insertion Sort?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 12
Reputation: swbuko is an unknown quantity at this point 
Solved Threads: 0
swbuko swbuko is offline Offline
Newbie Poster

How to do Insertion Sort?

 
0
  #1
Sep 19th, 2008
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++
  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: How to do Insertion Sort?

 
0
  #2
Sep 19th, 2008
Try with something easy for start.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 12
Reputation: swbuko is an unknown quantity at this point 
Solved Threads: 0
swbuko swbuko is offline Offline
Newbie Poster

Re: How to do Insertion Sort?

 
0
  #3
Sep 19th, 2008
Originally Posted by Sci@phy View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: How to do Insertion Sort?

 
0
  #4
Sep 19th, 2008
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: How to do Insertion Sort?

 
0
  #5
Sep 20th, 2008
It looks like pseudocode.

You really want to translate this into C++ , write the whole program and see what you get.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC