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 02:29:08 -0700 Organization: A noiseless patient Spider Lines: 39 Message-ID: <86h84i8dy3.fsf@linuxsc.com> References: <6264b10d-109a-498d-86d5-791426100d0c@googlegroups.com> <87sgo4jdxq.fsf@bsb.me.uk> <19cbac1e-175a-43b9-b2be-98794417d4a1@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="c0077320ebba5bb8d2e49a5d4c287ef2"; logging-data="842"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+dAE0IGq856CHzWfxwEKTeM9Ne+KrTo90=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:C0cEatNl59IOMgy5Ebwd4uqOrS4= sha1:FoUmdlomlaXJehV54FNXHD4mxP0= Xref: csiph.com comp.lang.c:146793 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.)