Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #15274 > unrolled thread
| Started by | "Roland" <gut@datacomm.ch> |
|---|---|
| First post | 2012-04-10 19:12 +0200 |
| Last post | 2012-04-10 15:53 -0700 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.c++
roblem with the algorithm "insertion sort" "Roland" <gut@datacomm.ch> - 2012-04-10 19:12 +0200
Re: roblem with the algorithm "insertion sort" red floyd <no.spam.here@its.invalid> - 2012-04-10 15:29 -0700
Re: roblem with the algorithm "insertion sort" MiB <michael.boehnisch@gmail.com> - 2012-04-10 15:53 -0700
| From | "Roland" <gut@datacomm.ch> |
|---|---|
| Date | 2012-04-10 19:12 +0200 |
| Subject | roblem with the algorithm "insertion sort" |
| Message-ID | <4f8469e7_1@news.tiscalinet.ch> |
Hello
I have a problem with the algorithm "insertion sort" in c.
C-Code:
struct test *sTest
// vector of struct pointers
struct test *pt_test[10];
for (i = 0; i < 10; i++) {
pt_test[i] = sTest;
sTest = sTest->next;
}
// algorithm "insertion-sort"
for (n = 1; n < 10; n++) {
m = n;
while ((m > 0) && ((strcmp((char*) pt_test[m - 1]->cName, (char*)
pt_test[n]->cName) > 0))) {
strcpy((char*)pt_test[m]->cName, (char*)pt_test[m - 1]->cName);
m--;
}
strcpy((char*)pt_test[m]->cName, (char*)pt_test[n]->cName);
}
The problem is now, that this insertion-sort implementation don't works
correct.
Can you give me a hint?
Thanks a lot
[toc] | [next] | [standalone]
| From | red floyd <no.spam.here@its.invalid> |
|---|---|
| Date | 2012-04-10 15:29 -0700 |
| Message-ID | <jm2c90$s1i$1@dont-email.me> |
| In reply to | #15274 |
On 4/10/2012 10:12 AM, Roland wrote: > Hello > > I have a problem with the algorithm "insertion sort" in c. > [redacted] 1. This is a C++ group, not a C group (see FAQ 5.9) 2. Please see FAQ 5.2 and 5.8. http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2 http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2
[toc] | [prev] | [next] | [standalone]
| From | MiB <michael.boehnisch@gmail.com> |
|---|---|
| Date | 2012-04-10 15:53 -0700 |
| Message-ID | <55520788-71f9-4e25-84bf-654ce2a485df@z38g2000vbu.googlegroups.com> |
| In reply to | #15274 |
On Apr 10, 7:12 pm, "Roland" <g...@datacomm.ch> wrote: > Hello > > I have a problem with the algorithm "insertion sort" in c. > [..] >The problem is now, that this insertion-sort implementation don't works >correct. >Can you give me a hint? 1. Separate the algorithm from the data type it sorts. I.e. try to create a insertion sort procedure that does not depend on the fact you are sorting an array of pointers to structs. 2. This news group's topic is the language C++, not C. If you're really looking for C++ advice, I'd like to note the C++ standard library comes with a set of classes that handle strings and containers for data objects much more convenient than the somewhat ancient strcmp() and strcpy() calls you're using in your implementation attempt. 3. Avoid raw pointers, if you can. The code snippet you gave does not depend on dynamically allocated memory and does not implement safeguards for handling uninitialized memory or 0-pointers anyway. What happens if there are less than 10 objects linked together in the sTest chain? Don't get used to this style of programming. 4. Insertion sort is not a hard algorithm, in fact I cannot think of a simpler algorithm for sorting data right now. Refer to the Wikipedia article http://en.wikipedia.org/wiki/Insertion_sort and try to understand the algorithm before you attempt to implement it [and, I feel there is plenty of understanding missing right now, sorry to be that direct]. This should do you more good than any copy & paste solution provided to you in a reply post to your homework problem. best, MiB
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web