Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: sorting Was: Isn't that beauty ? (no it's not) Date: Tue, 07 Apr 2026 08:01:49 -0700 Organization: A noiseless patient Spider Lines: 78 Message-ID: <86bjfuyftu.fsf@linuxsc.com> References: <10otm7r$1ntrg$1@raubtier-asyl.eternal-september.org> <10p9q0h$2a77o$1@dont-email.me> <10pa02q$2d5us$1@dont-email.me> <10pa4eg$2dtli$5@dont-email.me> <10pa78l$2fjaa$1@dont-email.me> <10palrv$2jpeu$2@dont-email.me> <10pbf0q$2rtq3$1@dont-email.me> <10pbg8f$2spms$2@dont-email.me> <10pbhud$2tbk0$2@dont-email.me> <10pcvr2$3ednq$2@dont-email.me> <20260318112151.000021dc@yahoo.com> <10pdu8k$3o60c$1@dont-email.me> <20260319111959.0000447c@yahoo.com> <10ph2db$pn58$1@dont-email.me> <20260319172955.000062ce@yahoo.com> <10phfh8$u3oe$1@dont-email.me> <20260319214046.000004b5@yahoo.com> <10pi29u$15114$1@dont-email.me> <20260320140110.00006b84@yahoo.com> <868qazzugr.fsf@linuxsc.com> <20260407015823.00005a2f@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Tue, 07 Apr 2026 15:01:50 +0000 (UTC) Injection-Info: dont-email.me; posting-host="578c4ea6691f08c6710ee5a21a69b6e8"; logging-data="3051488"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX190SbxMZ1r/fg9GnsEM9LH0hjfT4M99VEs=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:IWyn8EnmbSmMfFvK9E0ZGhbYHGU= sha1:zTQv4+9iXEu/CWLEJdhqomk2c30= Xref: csiph.com comp.lang.c:397413 Michael S writes: > On Mon, 06 Apr 2026 13:48:04 -0700 > Tim Rentsch wrote: > >> Michael S writes: >> >> [discussion of sorting code] >> >>>> ----------------------- >>>> void isort(char** data, int ll, int rr) { >>>> char* temp; >>>> int i = ll, j = rr; >>>> char* pivot = data[(ll + rr) / 2]; >>>> >>>> do { >>>> while (strcmp(pivot, data[i]) > 0 && i < rr) ++i; >>>> while (strcmp(pivot, data[j]) < 0 && j > ll) --j; >>>> >>>> if (i <= j) { >>>> temp = data[i]; data[i] = data[j]; data[j] = temp; >>>> ++i; >>>> --j; >>>> } >>>> } while (i <= j); >>>> >>>> if (ll < j) isort(data, ll, j); >>>> if (i < rr) isort(data, i, rr); >>>> } >>>> >>>> //For a char* array A of N elements, call as 'isort(A, 0, n-1)'. >>> >>> Looks o.k. speed wise. >>> It is not ideomatic C, >> >> What about the code prompts you to say it is not idiomatic C? >> Is it because there is a line with multiple statements on it, >> or is there something else? > > Something else. > Here is a mechanical translation of Bart's code to what I consider more > idiomatic C. Pay attention, the translation was mechanical, I didn't > check if original logic is 100% correct and whether it is possible to > omit some checks. > > void isort(char** data, int len) { > if (len > 1) { > char** i = data, j = &data[len-1]; > char* pivot = data[(len-1) / 2]; > do { > while (strcmp(pivot, *i) > 0 && i < &data[len-1]) ++i; > while (strcmp(pivot, *j) < 0 && j > data) --j; > if (i <= j) { > char* temp = *i; *i = *j; *j = temp; > ++i; > --j; > } > } while (i <= j); > if (j > data) isort(data, j+1-data); > if (i < &data[len-1]) isort(i, &data[len]-i); > } > } > > Whether i and j are pointers or integers is less important. > But passing into call two indices instead of one and the fact that the > second index points into last element of araay instead of the next > place after last element is certainly non-idiomatic. I agree that using one past the last item is more common. I myself wouldn't call using at the last item non-idiomatic, but I understand your point. As for whether the number of index/pointer arguments is one or two, that's an internal design decision. Depending on other factors either choice might be preferable; I have used both in the past, even just in this last exercise of trying out different sorting algorithms. So I have to object to calling either choice non-idiomatic.