1/*
2 * Copyright (C) 2015 Apple Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28namespace WTF {
29
30// Why would you want to use bubble sort? When you know that your input is already mostly
31// sorted! This sort is guaranteed stable (it won't reorder elements that were equal), it
32// doesn't require any scratch memory, and is the fastest available sorting algorithm if your
33// input already happens to be sorted. This sort is also likely to have competetive performance
34// for small inputs, even if they are very unsorted.
35
36// We use this sorting algorithm for compiler insertion sets. An insertion set is usually very
37// nearly sorted. It shouldn't take more than a few bubbles to make it fully sorted. We made
38// this decision deliberately. Here's the performance of the testb3 Complex(64, 384) benchmark
39// with the Air::InsertionSet doing no sorting, std::stable_sorting, and bubbleSorting:
40//
41// no sort: 8.8222 +- 0.1911 ms.
42// std::stable_sort: 9.0135 +- 0.1418 ms.
43// bubbleSort: 8.8457 +- 0.1511 ms.
44//
45// Clearly, bubble sort is superior.
46//
47// Note that the critical piece here is that insertion sets tend to be small, they must be
48// sorted, the sort must be stable, they are usually already sorted to begin with, and when they
49// are unsorted it's usually because of a few out-of-place elements.
50
51template<typename IteratorType, typename LessThan>
52void bubbleSort(IteratorType begin, IteratorType end, const LessThan& lessThan)
53{
54 for (;;) {
55 bool changed = false;
56 ASSERT(end >= begin);
57 size_t limit = end - begin;
58 for (size_t i = limit; i-- > 1;) {
59 if (lessThan(begin[i], begin[i - 1])) {
60 std::swap(begin[i], begin[i - 1]);
61 changed = true;
62 }
63 }
64 if (!changed)
65 return;
66 // After one run, the first element in the list is guaranteed to be the smallest.
67 begin++;
68
69 // Now go in the other direction. This eliminates most sorting pathologies.
70 changed = false;
71 ASSERT(end >= begin);
72 limit = end - begin;
73 for (size_t i = 1; i < limit; ++i) {
74 if (lessThan(begin[i], begin[i - 1])) {
75 std::swap(begin[i], begin[i - 1]);
76 changed = true;
77 }
78 }
79 if (!changed)
80 return;
81 // Now the last element is guaranteed to be the largest.
82 end--;
83 }
84}
85
86template<typename IteratorType>
87void bubbleSort(IteratorType begin, IteratorType end)
88{
89 bubbleSort(
90 begin, end,
91 [](auto& left, auto& right) {
92 return left < right;
93 });
94}
95
96} // namespace WTF
97
98using WTF::bubbleSort;
99