| | |
How to do Insertion Sort?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 12
Reputation:
Solved Threads: 0
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++
I need help, I'm kinda new to C++
C++ Syntax (Toggle Plain Text)
insertionSort(array BM[]) for i = 1 to length[BM]-1 do { value = BM[i] j = i-1 while (j >= 0 && BM[j] > value) { BM[j + 1] = BM[j] j = j-1 } BM[j+1] = value }
Last edited by Narue; Sep 19th, 2008 at 4:24 pm. Reason: added code tags
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
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:
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
http://eternallyconfuzzled.com/tuts/...t_sorting.aspx
This code:
C++ Syntax (Toggle Plain Text)
insertionSort(array BM[]) for i = 1 to length[BM]-1 do { value = BM[i] j = i-1 while (j >= 0 && BM[j] > value) { BM[j + 1] = BM[j] j = j-1 } BM[j+1] = value }
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. ![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: inheritance help
- Next Thread: few questions
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






