From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 3C25D3858D39 for ; Sun, 17 Dec 2023 15:55:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3C25D3858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3C25D3858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702828512; cv=none; b=KrKrwbYa9kxfF8NXzaatOMyU2BF+xGKVxxrCEvJ8vPXXz7Lg3SkgHmZKnS15Pwp2s/59+lro5vaVCerfAPzCpjll+VeNachthV9u2DfhfOYjd9/JETNH5YrV2OA0dgCMnVr743D4aLccuilp7BiM6gJOnWABC8x6R8gluhJo/J4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702828512; c=relaxed/simple; bh=7Hb504JCabPsQ7+1uwFDADnBNWyny+W/D1bpK6nDhIc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=rIZ5C5OiQJyAQP6fHFLm0gtTsEwM8macBMhWfXq1UFMh5oJV644U8ICz2UMe2YZ4/hfOonDzMID9cpLEk1NVmzD2tl7Ty8WpCAP+Z3g6lho9QsTlB2/+5hRs/GCzGqGh4rM+v7DmL4dHpZjnhvG04oosX5AEEMwcJ4iox0xurQY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702828510; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9EyJX5kVShi5vpzpsxDpLIhye7I9bQDgyYx7aRV6zdc=; b=O4yoqntTm1NcWjxqeJMYWsiZmvZ5nKzHcZxLZqgNv5xEzBl3Ir98idq7vo8TRviwabe10i nVSql30KKXnXhXtVOcrUIpw4BZ6u8dx+ecafCxgdQCQPadEFH9dF2DLbT83X1ZsksAsd+d +1BLP/6ooCyp1JaQFOCHUwntQYnlHv8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-408-mWWWzCKBMTekR0s_C7hFbw-1; Sun, 17 Dec 2023 10:55:07 -0500 X-MC-Unique: mWWWzCKBMTekR0s_C7hFbw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C5FD585A588; Sun, 17 Dec 2023 15:55:06 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.77]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 71F5D40C6EB9; Sun, 17 Dec 2023 15:55:05 +0000 (UTC) From: Florian Weimer To: "Zack Weinberg" Cc: "Adhemerval Zanella" , "Kuan-Wei Chiu" , "GNU libc development" , "Noah Goldstein" Subject: Re: [PATCH] stdlib: Optimize number of calls to comparison function References: <20231202214839.2634493-1-visitorckw@gmail.com> <87y1eaxtst.fsf@oldenburg.str.redhat.com> <87plzjd42s.fsf@oldenburg.str.redhat.com> <4b7d0325-7c04-458a-8bec-37ca97b62fd7@linaro.org> Date: Sun, 17 Dec 2023 16:55:03 +0100 In-Reply-To: (Zack Weinberg's message of "Sun, 17 Dec 2023 10:42:17 -0500") Message-ID: <87a5q86cyw.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: * Zack Weinberg: > It occurred to me late last night that *any* in-place sorting algorithm > that operates on an array, can be made stable with no additional storage > cost, by using each element's address as a tiebreaker for the comparison > function. It *doesn't* need to be the element's original address -- all > that matters is that for every pair of elements that compare equal, the > sorting algorithm preserves the < relation for their addresses across > all swaps, which should happen naturally if address-< is used as a > tiebreaker for comparisons. It's not as simple as that because we might have A < B, so we can move A to a lower address than B, but at the same time time, we must make sure that we do not move it past an element C with A =3D=3D C, otherwise the result of the tie-breaking address comparison changes. I haven't really thought about it, but I suspect it's possible to do for some of the quadratic algorithms, but quicksort and heapsort=E2=80=94not so much. Thanks, Florian