Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: Sort char array
Date: Wed, 09 Oct 2019 09:43:02 -0700
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <864l0h98fd.fsf@linuxsc.com>
References: <6264b10d-109a-498d-86d5-791426100d0c@googlegroups.com> <87sgo4jdxq.fsf@bsb.me.uk> <19cbac1e-175a-43b9-b2be-98794417d4a1@googlegroups.com> <86h84i8dy3.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="c0077320ebba5bb8d2e49a5d4c287ef2"; logging-data="22909"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180tcCiCrQPR8rvTlT3ElWk1Lk9ZNSJ7zo="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:WOBjTGLDncE7OcYVRQjlht1IkOE= sha1:Zl/6NDiFmab4GxEa+3J3blx+zZw=
Xref: csiph.com comp.lang.c:146809
Tim Rentsch writes:
> John Bode writes:
>
>> On Monday, October 7, 2019 at 7:09:29 PM UTC-5, Ben Bacarisse wrote:
>>
>>> John Bode writes:
>>>
>>>> On Friday, October 4, 2019 at 11:59:39 AM UTC-5, DFS wrote:
>>>>
>>>>> 1 void psort(int n,char *x[]) {
>>>>> 2 char *temp;
>>>>> 3 for(i=0;i>>>> 4 for(j=0;j>>>> 5 if(strcasecmp(x[j],x[i])>0) {
>>>>> 6 temp = x[i];
>>>>> 7 x[i] = x[j]);
>>>>> 8 x[j] = temp;
>>>>> 9 }
>>>>> 10 }
>>>>> 11 }
>>>>> 12 }
>>>>>
>>>>>
>>>>> This works, but is very slow. Like 6 seconds to sort an array of 10,000
>>>>> elements.
>>>>>
>>>>> What am I doing wrong?
>>>>
>>>> You're using a bubble sort (which is O(n^2)) and you've implemented it
>>>> slightly incorrectly,
>>>
>>> It's not bubble sort at all. (I thought it was at first, but I simply
>>> had not looked properly.) It tests every possible pair of values
>>> (twice!) and swaps them if they are out of order.
>>
>> As I said, it's a bubble sort that's been implemented slightly
>> incorrectly. It would be correct if j started from i instead of 0.
>
> Try coding that and see if it gives the desired behavior.
> (Hint: it doesn't.)
Sorry, my bad. The description and code is so awful I got
confused about what it wasn't doing.