American flag sort

An American flag sort is an efficient, in-place variant of radix sort that distributes items into buckets. Non-comparative sorting algorithms such as radix sort and American flag sort are typically used to sort large objects such as strings, for which comparison is not a unit-time operation.[1] American flag sort iterates through the bits of the objects, considering several bits of each object at a time. For each set of bits, American flag sort makes two passes through the array of objects: first to count the number of objects that will fall in each bin, and second to place each object in its bucket. This works especially well when sorting a byte at a time, using 256 buckets. With some optimizations, it is twice as fast as quicksort for large sets of strings.[1]

The name American flag sort comes by analogy with the Dutch national flag problem in the last step: efficiently partition the array into many "stripes".

Algorithm

Sorting algorithms in general sort a list of objects according to some ordering scheme. In contrast to comparison-based sorting algorithms, such as quicksort, American flag sort is based on directly comparing the bytes (numerical representation) of the underlying objects. In-place sorting algorithms, including American flag sort, run without allocating a significant amount of memory beyond that used by the original array. This is a significant advantage, both in memory savings and in time saved copying the array.

American flag sort works by successively dividing a list of objects into buckets based on the first digit of their base-N representation (the base used is referred to as the radix). When N is 3, each object can be swapped into the correct bucket by using the Dutch national flag algorithm. When N is larger, however, objects cannot be immediately swapped into place, because it is unknown where each bucket should begin and end. American flag sort gets around this problem by making two passes through the array. The first pass counts the number of objects that belong in each of the N buckets. The beginning of each bucket is then computed as the sum of sizes of the preceding buckets. The second pass puts each object into the correct bucket.

American flag sort is most efficient with a radix that is a power of 2, because bit-shifting operations can be used instead of expensive exponentiations to compute the value of each digit. When sorting strings using 8- or 7-bit encodings such as ASCII, it is typical to use a radix of 256 or 128, which amounts to sorting character-by-character.[1]

Performance considerations

For pure English alphabet text, the counts histogram is always sparse. Depending on the hardware, it may be worth clearing the counts in correspondence with completing a bucket (as in the original paper); or it may be worth maintaining a max and min active bucket, or a more complex data structure suitable for sparse arrays. It is also important to use a more basic sorting method for very small data sets, except in pathological cases where keys may share very long prefixes.

Most critically, this algorithm follows a random permutation, and is thus particularly cache-unfriendly for large datasets.[2][user-generated source] It is a suitable algorithm in conjunction with a k-way merge algorithm.[citation needed] (The original paper was written before cached memory was in common use.)

Pseudocode

american_flag_sort(Array, Radix)
    for each digit D:
        # first pass: compute counts
        Counts <- zeros(Radix)
        for object X in Array:
            Counts[digit D of object X in base Radix] += 1
        # compute bucket offsets
        Offsets <- [ sum(Counts[0..i]) for i in 1..Radix]
        # swap objects into place
        for object X in Array:
            swap X to the bucket starting at Offsets[digit D of X in base Radix]
        for each Bucket:
            american_flag_sort(Bucket, Radix)

Sample implementation in Python

This example written in the Python programming language will perform American flag sort for any radix of 2 or greater. Simplicity of exposition is chosen over clever programming, and so the log function is used instead of bit shifting techniques.

from math import floor, log
from copy import copy

def get_radix_val(x, digit, radix):
    return int(floor(x / radix**digit)) % radix

def compute_offsets(a_list, start, end, digit, radix):
    counts = [0 for _ in range(radix)]
    for i in range(start, end):
        val = get_radix_val(a_list[i], digit, radix)
        counts[val] += 1
    offset = start
    offsets = [start]
    for i in range(radix):
        offset += counts[i]
        offsets.append(offset)
    return offsets

def swap(a_list, offsets, start, end, digit, radix):
    next_free = copy(offsets)
    cur_block = 0
    while cur_block < radix:
        i = next_free[cur_block]
        if i >= offsets[cur_block+1]:
            cur_block += 1
            continue
        radix_val = get_radix_val(a_list[i], digit, radix)
        if radix_val != cur_block:
            swap_to = next_free[radix_val]
            a_list[i], a_list[swap_to] = a_list[swap_to], a_list[i]
        next_free[radix_val] += 1

def american_flag_sort_helper(a_list, start, end, digit, radix):
    offsets = compute_offsets(a_list, start, end, digit, radix)
    swap(a_list, offsets, start, end, digit, radix)
    if digit == 0:
        return
    for i in range(len(offsets)-1):
        american_flag_sort_helper(a_list, offsets[i], offsets[i+1], digit-1, radix)

def american_flag_sort(a_list, radix):
    for x in a_list:
        assert type(x) == int
    max_val = max(a_list)
    max_digit = int(floor(log(max_val, radix)))
    american_flag_sort_helper(a_list, 0, len(a_list), max_digit, radix)

See also

References

  1. ^ a b c McIlroy, Peter M.; Bostic, Keith; McIlroy, M. Douglas (1993). "Engineering radix sort" (PDF). Computing Systems. 6 (1): 5–27.
  2. ^ "algorithm - In-Place Radix Sort". Stack Overflow. Retrieved 2020-10-18.

General

Read other articles:

Questa voce sull'argomento centri abitati dello stato di New York è solo un abbozzo. Contribuisci a migliorarla secondo le convenzioni di Wikipedia. Segui i suggerimenti del progetto di riferimento. CooperstownvillaggioCooperstown – Veduta LocalizzazioneStato Stati Uniti Stato federato New York ConteaOtsego TerritorioCoordinate42°41′50″N 74°55′37″W / 42.697222°N 74.926944°W42.697222; -74.926944 (Cooperstown)Coordinate: 42°41′50″N 74°55′37

 

Видинське царство ПрапорГерб Дата створення / заснування 1356 Столиця Видин Час/дата припинення існування 1396  Видинське царство у Вікісховищі Історія Болгарії Стародавність Караново культура Культура Вінча Варненський некрополь Фракія Одриське царство МезіяСеред...

 

Eros PagniEros Pagni dalam Profondo rosso (1975)Lahir28 Agustus 1939 (umur 84)La Spezia, ItaliaPekerjaanPemeran, pengisi suaraTahun aktif1964-kini Eros Pagni (lahir 28 Agustus 1939) adalah seorang pemeran dan pengisi suara asal Italia.[1] Filmografi pilihan Pleasant Nights (1966) Il generale dorme in piedi (1972) Love and Anarchy (1973) Swept Away (1974) All Screwed Up (1974) Processo per direttissima (1974) Deep Red (1975) Soldier of Fortune (1976) I nuovi mostri (1977) Goo...

Overview of education in Kenya This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages) This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: Education in Kenya – news · newspapers · books · scholar · JSTOR (March 2011...

 

Kekaisaran Qing pada tahun 1820. Dalam kawasan Asia ditunjukkan dalam warna kuning. Dinasti Qing di Asia Bagian Dalam adalah bahasan daerah ekspansi dinasti Qing di Asia Bagian Dalam pada abad ke-17 dan abad ke-18 masehi, termasuk Mongolia agian dalam dan Mongolia bagian Luar, Manchuria, Tibet, Qinghai dan Xinjiang. Perang yang terjadi terutama terhadap dinasti Yuan bagian utara (sebelum 1636) dan Urumqi Khanate (1687-1758). Bahkan sebelum penaklukan Cina yang tepat (lihat penaklukan Ming ole...

 

Buddhist temple in Kuruvita, Sri Lanka Delgamuwa Raja Maha Viharaදෙල්ගමුව රජ මහා විහාරයThe Kurahan grinding stone, which was used to hide the tooth relic of BuddhaReligionAffiliationBuddhismDistrictRatnapuraProvinceSabaragamuwa ProvinceLocationLocationDelgamuwa, Kuruwita, Sri LankaGeographic coordinates06°46′33.1″N 80°21′22.8″E / 6.775861°N 80.356333°E / 6.775861; 80.356333ArchitectureTypeBuddhist temple Delgamuwa Raja ...

2006 American filmThe PlagueDirected byHal MasonbergWritten by Hal Masonberg Teal Minton Produced byClive BarkerJorge SaraleguiMartin WileyMatt MilichTim O'HairAnthony DiBlasiStarring James Van Der Beek Ivana Miličević CinematographyBill ButlerEdited byEd MarxMusic byLászló ReményiProductioncompanies Midnight Picture Show Armada Pictures Distributed bySony Pictures Home EntertainmentRelease date September 5, 2006 (2006-09-05) Running time88 minutesCountryUnited StatesLangu...

 

Royal New Zealand Army Ordnance CorpsActive1915–1996Country New ZealandBranch New Zealand ArmyRoleStorage and issuing of ordnanceMotto(s)Sua tela tonanti, commonly translated as To the Warrior his ArmsColoursScarlet and dark blueMarchThe Village BlacksmithAnniversaries12 JulyCommandersColonel-in-ChiefQueen Elizabeth IIMilitary unit The Royal New Zealand Army Ordnance Corps (RNZAOC) concerned itself with the provisioning of troops with the means to fight; specifically uniforms, weapons ...

 

The Apache Software FoundationTanggal pendirian25 Maret 1999; 24 tahun lalu (1999-03-25)PendiriBrian BehlendorfKen CoarMark CoxLars EilebrechtRalf S. EngelschallRoy T. FieldingDean GaudetBen HydeJim JagielskiAlexei KosutMartin KraemerBen LaurieDoug MacEachernAram MirzadehSameer ParekhCliff SkolnickMarc SlemkoBill StoddardPaul SuttonRandy TerbushDirk-Willem van GulikTipeorganisasi 501(c)(3)FokusPerangkat lunak sumber terbukaLokasiWakefield, Massachusetts, U.S.MetodeLisensi ApachePendapata...

Sri Lankan politician Hon.S. SivamohanMP MPCசி. சிவமோகன்Member of Parliamentfor Vanni DistrictIncumbentAssumed office 2015Member of the Northern Provincial Council for Mullaitivu DistrictIn office2013–2015Succeeded byVallipuram Kamaleswaran Personal detailsPolitical partyEelam People's Revolutionary Liberation FrontOther politicalaffiliationsTamil National AllianceProfessionPhysician Sivapragasam Sivamohan (Tamil: சிவப்பிரகாசம் சிவம...

 

French-language Belgian daily newspaper La Dernière HeureTypeDaily newspaperPublisherIPM Publishing GroupEditorMichel MarteauFounded19 April 1906Political alignmentLiberalLanguageFrenchHeadquartersBrusselsWebsitehttp://www.dhnet.be/ La Dernière Heure (lit. 'The Latest Hour') and Les Sports (lit. 'The Sports'), currently sold under the name La DH Les Sports+, is a French-language daily newspaper published in Brussels, Belgium. The paper is known for news and sports. Histo...

 

Figure skater Roman SerovSerov in 2005.Full nameRoman SerovBorn (1976-12-16) 16 December 1976 (age 46)Moscow, Soviet UnionHeight1.76 m (5 ft 9+1⁄2 in)Figure skating careerCountryIsraelRussia Medal record Representing  Russia Men's singles Figure skating Winter Universiade 2001 Zakopane Men's singles Roman Serov (born 16 December 1976 in Moscow) is a Russian-born figure skater and skating coach who has also competed for Israel. He won two medals on the Grand Prix...

ГЕОГРАФІЧНИЙ ПОРТАЛ Портал створено для зручної навігації масивом статей Вікіпедії про нашу планету, окремі її елементи, фізичні процеси в географічній оболонці, географічні науки, науковців, мандрівників і дослідників. Над чим працюємо Редактори П:ГЕО Геологія Мінера...

 

Martina Ceccarelli Nazionalità  Italia Altezza 168 cm Peso 53 kg Calcio Ruolo Centrocampista, attaccante Squadra svincolata Carriera Giovanili  Grifo Perugia Squadre di club1 2011-2014 Grifo Perugia51 (5)2014-2015→  Roma CF25 (9)2015-2018 Grifo Perugia51 (29)2018-2020 Perugia? (?)2020-2023 Arezzo25+ (4+) Nazionale 2013-2014 Italia U-174 (0) Palmarès  Europei di calcio Under-17 Bronzo Inghilterra 2014  Mondiali di calcio Under-17 Bronzo Costa Ri...

 

Confuciustempel Naam (taalvarianten) Vereenvoudigd 孔庙 Traditioneel 孔廟 Pinyin kǒngmiào Wade-Giles K'ong-miao Jyutping (Standaardkantonees) hung2 miu6 Koreaans 공자묘/Kongja myo Zhuyin ㄎㄨㄥˇ ㄇ一ㄠˋ Standaardkantonees Hǒng Mǐew HK-romanisatie (Standaardkantonees) Hung Miu Yale (Standaardkantonees) húng miuh Dapenghua Hǒng Mǐew Minnanyu Khóng-biāu Vietnamees Khổng miếu Confuciustempel Naam (taalvarianten) Vereenvoudigd 文庙 Traditioneel 文廟 Pinyin wénmiào...

Miryang밀양시 Ciudad Bandera MiryangLocalización de Miryang en Corea del Sur Coordenadas 35°29′36″N 128°44′56″E / 35.493333333333, 128.74888888889Idioma oficial CoreanoEntidad Ciudad • País  Corea del Sur • Provincia Gyeongsang del SurSuperficie   • Total 800 km²Población (2011)   • Total 110 000 hab. • Densidad 760 hab/km²Huso horario UTC + 9 Sitio web oficial [editar datos en Wikidata...

 

Douglas Aircraft Company's Model 2229 adalah transportasi supersonik sayap delta yang diusulkan (SST) awalnya dimulai sebagai studi pribadi. Desain berkembang sejauh membuat mock-up dari area kokpit dan model terowongan angin dari tata letak keseluruhan. Setelah mempelajari desain, Douglas menyimpulkan bahwa SST tidak akan bekerja secara ekonomi, dan menolak untuk memasukkan 2229 dalam Program National Supersonic Transport (NST) pada tahun 1963. Referensi Wikimedia Commons memiliki media meng...

 

1955 manifesto on the dangers of nuclear weapons Here, then, is the problem which we present to you, stark and dreadful and inescapable: Shall we put an end to the human race; or shall mankind renounce war?~ Albert Einstein and Bertrand Russell[1] The Russell–Einstein Manifesto was issued in London on 9 July 1955 by Bertrand Russell in the midst of the Cold War. It highlighted the dangers posed by nuclear weapons and called for world leaders to seek peaceful resolutions to internati...

Sekerumunan orang menyaksikan terbakarnya jasad Jesse Washington Jesse Washington adalah seorang buruh tani berusia 17 tahun dan berdarah Afrika-Amerika yang dihakimi dan dibunuhi oleh massa di ibukota county Waco, Texas, pada tanggal 15 Mei 1916. Kejadian ini menjadi contoh terkenal penghakiman dan pembunuhan tanpa peradilan yang bermotif rasial di Amerika Serikat. Jesse didakwa memperkosa dan membunuh Lucy Fryer, istri majikan kulit putihnya di pedesaan Robinson, Texas. Ia dirantai di leher...

 

جبل القديس ميشيل في نورماندي. برج إيفل - باريس جذبت فرنسا 79.50 مليون سائح أجنبي في عام 2011 , مما يجعلها الوجهة السياحية الأكثر شعبية في العالم,[1] وهو ثالث في الدخل من السياحة بسبب السياح , 20٪ و أكثر من السياح أمضى أقل من نصف بقدر ما فعلوا في الولايات المتحدة. فرنسا لديها 37 موق...

 

Strategi Solo vs Squad di Free Fire: Cara Menang Mudah!