Merge algorithm

Merge algorithms are a family of algorithms that take multiple sorted lists as input and produce a single list as output, containing all the elements of the inputs lists in sorted order. These algorithms are used as subroutines in various sorting algorithms, most famously merge sort.

Application

A graph exemplifying merge sort. Two red arrows starting from the same node indicate a split, while two green arrows ending at the same node correspond to an execution of the merge algorithm.

The merge algorithm plays a critical role in the merge sort algorithm, a comparison-based sorting algorithm. Conceptually, the merge sort algorithm consists of two steps:

  1. Recursively divide the list into sublists of (roughly) equal length, until each sublist contains only one element, or in the case of iterative (bottom up) merge sort, consider a list of n elements as n sub-lists of size 1. A list containing a single element is, by definition, sorted.
  2. Repeatedly merge sublists to create a new sorted sublist until the single list contains all elements. The single list is the sorted list.

The merge algorithm is used repeatedly in the merge sort algorithm.

An example merge sort is given in the illustration. It starts with an unsorted array of 7 integers. The array is divided into 7 partitions; each partition contains 1 element and is sorted. The sorted partitions are then merged to produce larger, sorted, partitions, until 1 partition, the sorted array, is left.

Merging two lists

Merging two sorted lists into one can be done in linear time and linear or constant space (depending on the data access model). The following pseudocode demonstrates an algorithm that merges input lists (either linked lists or arrays) A and B into a new list C.[1][2]: 104  The function head yields the first element of a list; "dropping" an element means removing it from its list, typically by incrementing a pointer or index.

algorithm merge(A, B) is
    inputs A, B : list
    returns list

    C := new empty list
    while A is not empty and B is not empty do
        if head(A) ≤ head(B) then
            append head(A) to C
            drop the head of A
        else
            append head(B) to C
            drop the head of B

    // By now, either A or B is empty. It remains to empty the other input list.
    while A is not empty do
        append head(A) to C
        drop the head of A
    while B is not empty do
        append head(B) to C
        drop the head of B

    return C

When the inputs are linked lists, this algorithm can be implemented to use only a constant amount of working space; the pointers in the lists' nodes can be reused for bookkeeping and for constructing the final merged list.

In the merge sort algorithm, this subroutine is typically used to merge two sub-arrays A[lo..mid], A[mid+1..hi] of a single array A. This can be done by copying the sub-arrays into a temporary array, then applying the merge algorithm above.[1] The allocation of a temporary array can be avoided, but at the expense of speed and programming ease. Various in-place merge algorithms have been devised,[3] sometimes sacrificing the linear-time bound to produce an O(n log n) algorithm;[4] see Merge sort § Variants for discussion.

K-way merging

k-way merging generalizes binary merging to an arbitrary number k of sorted input lists. Applications of k-way merging arise in various sorting algorithms, including patience sorting[5] and an external sorting algorithm that divides its input into k = 1/M − 1 blocks that fit in memory, sorts these one by one, then merges these blocks.[2]: 119–120 

Several solutions to this problem exist. A naive solution is to do a loop over the k lists to pick off the minimum element each time, and repeat this loop until all lists are empty:

  • Input: a list of k lists.
  • While any of the lists is non-empty:
    • Loop over the lists to find the one with the minimum first element.
    • Output the minimum element and remove it from its list.

In the worst case, this algorithm performs (k−1)(nk/2) element comparisons to perform its work if there are a total of n elements in the lists.[6] It can be improved by storing the lists in a priority queue (min-heap) keyed by their first element:

  • Build a min-heap h of the k lists, using the first element as the key.
  • While any of the lists is non-empty:
    • Let i = find-min(h).
    • Output the first element of list i and remove it from its list.
    • Re-heapify h.

Searching for the next smallest element to be output (find-min) and restoring heap order can now be done in O(log k) time (more specifically, 2⌊log k comparisons[6]), and the full problem can be solved in O(n log k) time (approximately 2n⌊log k comparisons).[6][2]: 119–120 

A third algorithm for the problem is a divide and conquer solution that builds on the binary merge algorithm:

  • If k = 1, output the single input list.
  • If k = 2, perform a binary merge.
  • Else, recursively merge the first k/2⌋ lists and the final k/2⌉ lists, then binary merge these.

When the input lists to this algorithm are ordered by length, shortest first, it requires fewer than n⌈log k comparisons, i.e., less than half the number used by the heap-based algorithm; in practice, it may be about as fast or slow as the heap-based algorithm.[6]

Parallel merge

A parallel version of the binary merge algorithm can serve as a building block of a parallel merge sort. The following pseudocode demonstrates this algorithm in a parallel divide-and-conquer style (adapted from Cormen et al.[7]: 800 ). It operates on two sorted arrays A and B and writes the sorted output to array C. The notation A[i...j] denotes the part of A from index i through j, exclusive.

algorithm merge(A[i...j], B[k...ℓ], C[p...q]) is
    inputs A, B, C : array
           i, j, k, ℓ, p, q : indices

    let m = j - i,
        n = ℓ - k

    if m < n then
        swap A and B  // ensure that A is the larger array: i, j still belong to A; k, ℓ to B
        swap m and n

    if m ≤ 0 then
        return  // base case, nothing to merge

    let r = ⌊(i + j)/2⌋
    let s = binary-search(A[r], B[k...ℓ])
    let t = p + (r - i) + (s - k)
    C[t] = A[r]

    in parallel do
        merge(A[i...r], B[k...s], C[p...t])
        merge(A[r+1...j], B[s...ℓ], C[t+1...q])

The algorithm operates by splitting either A or B, whichever is larger, into (nearly) equal halves. It then splits the other array into a part with values smaller than the midpoint of the first, and a part with larger or equal values. (The binary search subroutine returns the index in B where A[r] would be, if it were in B; that this always a number between k and .) Finally, each pair of halves is merged recursively, and since the recursive calls are independent of each other, they can be done in parallel. Hybrid approach, where serial algorithm is used for recursion base case has been shown to perform well in practice [8]

The work performed by the algorithm for two arrays holding a total of n elements, i.e., the running time of a serial version of it, is O(n). This is optimal since n elements need to be copied into C. To calculate the span of the algorithm, it is necessary to derive a Recurrence relation. Since the two recursive calls of merge are in parallel, only the costlier of the two calls needs to be considered. In the worst case, the maximum number of elements in one of the recursive calls is at most since the array with more elements is perfectly split in half. Adding the cost of the Binary Search, we obtain this recurrence as an upper bound:

The solution is , meaning that it takes that much time on an ideal machine with an unbounded number of processors.[7]: 801–802 

Note: The routine is not stable: if equal items are separated by splitting A and B, they will become interleaved in C; also swapping A and B will destroy the order, if equal items are spread among both input arrays. As a result, when used for sorting, this algorithm produces a sort that is not stable.

Parallel merge of two lists

There are also algorithms that introduce parallelism within a single instance of merging of two sorted lists. These can be used in field-programmable gate arrays (FPGAs), specialized sorting circuits, as well as in modern processors with single-instruction multiple-data (SIMD) instructions.

Existing parallel algorithms are based on modifications of the merge part of either the bitonic sorter or odd-even mergesort.[9] In 2018, Saitoh M. et al. introduced MMS [10] for FPGAs, which focused on removing a multi-cycle feedback datapath that prevented efficient pipelining in hardware. Also in 2018, Papaphilippou P. et al. introduced FLiMS [9] that improved the hardware utilization and performance by only requiring pipeline stages of P/2 compare-and-swap units to merge with a parallelism of P elements per FPGA cycle.

Language support

Some computer languages provide built-in or library support for merging sorted collections.

C++

The C++'s Standard Template Library has the function std::merge, which merges two sorted ranges of iterators, and std::inplace_merge, which merges two consecutive sorted ranges in-place. In addition, the std::list (linked list) class has its own merge method which merges another list into itself. The type of the elements merged must support the less-than (<) operator, or it must be provided with a custom comparator.

C++17 allows for differing execution policies, namely sequential, parallel, and parallel-unsequenced.[11]

Python

Python's standard library (since 2.6) also has a merge function in the heapq module, that takes multiple sorted iterables, and merges them into a single iterator.[12]

See also

References

  1. ^ a b Skiena, Steven (2010). The Algorithm Design Manual (2nd ed.). Springer Science+Business Media. p. 123. ISBN 978-1-849-96720-4.
  2. ^ a b c Kurt Mehlhorn; Peter Sanders (2008). Algorithms and Data Structures: The Basic Toolbox. Springer. ISBN 978-3-540-77978-0.
  3. ^ Katajainen, Jyrki; Pasanen, Tomi; Teuhola, Jukka (1996). "Practical in-place mergesort". Nordic J. Computing. 3 (1): 27–40. CiteSeerX 10.1.1.22.8523.
  4. ^ Kim, Pok-Son; Kutzner, Arne (2004). Stable Minimum Storage Merging by Symmetric Comparisons. European Symp. Algorithms. Lecture Notes in Computer Science. Vol. 3221. pp. 714–723. CiteSeerX 10.1.1.102.4612. doi:10.1007/978-3-540-30140-0_63. ISBN 978-3-540-23025-0.
  5. ^ Chandramouli, Badrish; Goldstein, Jonathan (2014). Patience is a Virtue: Revisiting Merge and Sort on Modern Processors. SIGMOD/PODS.
  6. ^ a b c d Greene, William A. (1993). k-way Merging and k-ary Sorts (PDF). Proc. 31-st Annual ACM Southeast Conf. pp. 127–135.
  7. ^ a b Cormen, Thomas H.; Leiserson, Charles E.; Rivest, Ronald L.; Stein, Clifford (2009) [1990]. Introduction to Algorithms (3rd ed.). MIT Press and McGraw-Hill. ISBN 0-262-03384-4.
  8. ^ Victor J. Duvanenko (2011), "Parallel Merge", Dr. Dobb's Journal
  9. ^ a b Papaphilippou, Philippos; Luk, Wayne; Brooks, Chris (2022). "FLiMS: a Fast Lightweight 2-way Merger for Sorting". IEEE Transactions on Computers: 1–12. doi:10.1109/TC.2022.3146509. hdl:10044/1/95271. S2CID 245669103.
  10. ^ Saitoh, Makoto; Elsayed, Elsayed A.; Chu, Thiem Van; Mashimo, Susumu; Kise, Kenji (April 2018). "A High-Performance and Cost-Effective Hardware Merge Sorter without Feedback Datapath". 2018 IEEE 26th Annual International Symposium on Field-Programmable Custom Computing Machines (FCCM). pp. 197–204. doi:10.1109/FCCM.2018.00038. ISBN 978-1-5386-5522-1. S2CID 52195866.
  11. ^ "std:merge". cppreference.com. 2018-01-08. Retrieved 2018-04-28.
  12. ^ "heapq — Heap queue algorithm — Python 3.10.1 documentation".

Further reading

Read other articles:

Air Terjun SegenterLokasiDesa Pakuan, Kecamatan Narmada, Kabupaten Lombok Barat, Provinsi Nusa Tenggara Barat, IndonesiaTipePlungeTinggi total12 meter (39 ft)Anak sungaiAliran Sungai Rinjani Air Terjun Segenter merupakan salah satu air terjun yang berada di wilayah Kecamatan Narmada Kabupaten Lombok Barat. Air terjun ini berada di Dusun Qumbi Desa Pakuan, tepatnya 5 kilo Dari Desa Selat Narmada.Jika anda ingin berkunjung ke lokasi air terjun ini maka anda melalui rute yang dari kota mata...

 

 

Patung PahlawanTugu TaniLetakJakarta, IndonesiaDibangun1963; 61 tahun lalu (1963)ArsitekMatvey Manizer, Ossip Manizer Patung Pahlawan atau disebut juga Tugu Tani adalah sebuah patung yang terbuat dari perunggu dengan figur satu orang pria bercaping dan seorang wanita yang terletak di dekat Stasiun Gambir Jakarta.[1] Patung ini dibuat oleh dua pematung Rusia kenamaan, Matvey Manizer dan Ossip Manizer, sebagai hadiah dari pemerintah Uni Soviet atas persahabatannya dengan Indonesia....

 

 

artikel ini perlu dirapikan agar memenuhi standar Wikipedia. Tidak ada alasan yang diberikan. Silakan kembangkan artikel ini semampu Anda. Merapikan artikel dapat dilakukan dengan wikifikasi atau membagi artikel ke paragraf-paragraf. Jika sudah dirapikan, silakan hapus templat ini. (Pelajari cara dan kapan saatnya untuk menghapus pesan templat ini) Call of Duty: Modern Warfare 2 PublikasiWindows, PlayStation 3, Xbox 36010 November, 2009OS X20 Mei, 2014Campaign Remastered PlayStation 430 Maret...

FleabagJudul FleabagGenre Drama komedi Tragikomedi PembuatPhoebe Waller-BridgeDitulis olehPhoebe Waller-BridgeSutradara Harry Bradbeer Tim Kirkby (e. 1) Pemeran Phoebe Waller-Bridge Sian Clifford Olivia Colman Bill Paterson Brett Gelman Hugh Skinner Hugh Dennis Ben Aldridge Jamie Demetriou Jenny Rainsford Andrew Scott Fiona Shaw Kristin Scott Thomas Ray Fearon Penata musikIsobel Waller-BridgeNegara asalBritania RayaBahasa asliInggrisJmlh. seri2Jmlh. episode12 (daftar episode)ProduksiPr...

 

 

الدوري السوري الممتاز 2017–18 تفاصيل الموسم الدوري السوري الممتاز  النسخة 47  البلد سوريا  البطل نادي الوثبة السوري  الهابطون نادي الجهاد،  ونادي المحافظة  مباريات ملعوبة 182   عدد المشاركين 14   الدوري السوري الممتاز 2016–17  الدوري السوري الممتاز 2018–19  ...

 

 

Radio station in Austin, Texas For the radio station in Georgetown, Texas, which currently uses the call sign formerly used by KVET-FM, see KHFI-FM. KVET-FMAustin, TexasBroadcast areaGreater AustinFrequency98.1 MHz (HD Radio)Branding98.1 K-VETProgrammingFormatCountrySubchannelsHD2: 1980s hits 103.1, Austin's 80s StationOwnershipOwneriHeartMedia(iHM Licenses, LLC)Sister stationsKASE-FMKHFI-FMKPEZKVETHistoryFirst air dateMarch 25, 1956; 68 years ago (1956-03-25) (as KHFI-FM at...

Collegiate public research university in Durham, United Kingdom Durham UniversityCoat of arms of the universityLatin: Universitas DunelmensīsOther nameThe University of DurhamMottoLatin: Fundamenta eius super montibus sanctisMotto in EnglishHer foundations are upon the holy hills (Psalm 87:1)TypePublic research universityEstablished1832; 192 years ago (1832) (university status)Academic affiliations ACU Coimbra Group EUA Matariki Network of Universities N8 Research Part...

 

 

SMA Negeri 12 MedanInformasiDidirikan1979JenisSekolah NegeriAkreditasiA [1]Nomor Statistik Sekolah30.1.07.60.06.052Nomor Pokok Sekolah Nasional10210876Kepala SekolahDra. Ade Melinda Banjarnahor, M.SiJurusan atau peminatanIPA dan IPSRentang kelasX, XI IPA, XI IPS, XII IPA, XII IPSKurikulumKurikulum Tingkat Satuan Pendidikan‎NEM terendah8,94 Jalur Nilai UN 2014 (70% daya tampung)'AlamatLokasiJl. Cempaka No. 75, Medan, Sumatera Utara, IndonesiaTel./Faks.061-8455904Situs web[2...

 

 

Subset of graphic design 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: Motion graphic design – news · newspapers · books · scholar · JSTOR (July 2007) (Learn how and when to remove this template message) A photo of After Effects being used in a motion graphics project Motion graphic design, also known as m...

Cet article est une ébauche concernant une localité italienne et le Trentin-Haut-Adige. Vous pouvez partager vos connaissances en l’améliorant (comment ?) selon les recommandations des projets correspondants. Prato allo Stelvio Armoiries Noms Nom allemand Prad am Stilfserjoch Administration Pays Italie Région Trentin-Haut-Adige  Province Bolzano   Code postal 39026 Code ISTAT 021067 Code cadastral H004 Préfixe tel. 0473 Démographie Gentilé pratesi Population 3 80...

 

 

本條目存在以下問題,請協助改善本條目或在討論頁針對議題發表看法。 此條目需要編修,以確保文法、用詞、语气、格式、標點等使用恰当。 (2013年8月6日)請按照校對指引,幫助编辑這個條目。(幫助、討論) 此條目剧情、虛構用語或人物介紹过长过细,需清理无关故事主轴的细节、用語和角色介紹。 (2020年10月6日)劇情、用語和人物介紹都只是用於了解故事主軸,輔助�...

 

 

Artikel ini perlu diwikifikasi agar memenuhi standar kualitas Wikipedia. Anda dapat memberikan bantuan berupa penambahan pranala dalam, atau dengan merapikan tata letak dari artikel ini. Untuk keterangan lebih lanjut, klik [tampil] di bagian kanan. Mengganti markah HTML dengan markah wiki bila dimungkinkan. Tambahkan pranala wiki. Bila dirasa perlu, buatlah pautan ke artikel wiki lainnya dengan cara menambahkan [[ dan ]] pada kata yang bersangkutan (lihat WP:LINK untuk keterangan lebih lanjut...

Station in suburbs of Dublin, Ireland ClonsillaCluain SaileachA push-pull train of former CIÉ 2600 Class railcars at Clonsilla in 1982General informationLocationClonsilla Road, Dublin 15, D15 YA36IrelandCoordinates53°22′59″N 6°25′23″W / 53.383°N 6.423°W / 53.383; -6.423Owned byIarnród ÉireannOperated byIarnród ÉireannPlatforms3Tracks2Bus routes2Bus operatorsDublin Bus, Go-Ahead IrelandConnections39L52ConstructionStructure typeAt-gradeOther informationS...

 

 

British politician For other people named Philip Hammond, see Philip Hammond (disambiguation). The Right HonourableThe Lord Hammond of RunnymedePCOfficial portrait, 2017Chancellor of the ExchequerIn office13 July 2016 – 24 July 2019Prime MinisterTheresa MayPreceded byGeorge OsborneSucceeded bySajid JavidSecretary of State for Foreign and Commonwealth AffairsIn office15 July 2014 – 13 July 2016Prime MinisterDavid CameronPreceded byWilliam HagueSucceeded byBoris JohnsonSec...

 

 

Catholic schoolSt. Anne SchoolLocation1-30 Summit AvenueFair Lawn, NJ 07410InformationTypeCatholic schoolEstablished1949School districtRoman Catholic Archdiocese of NewarkPrincipalJason FelicianoFaculty25.0 (on FTE basis)[1]GradesPreK - 8Enrollment124 (as of 2019-20)[1]Student to teacher ratio12.7[1]Color(s)Blue and WhiteInformation201-796-3353WebsiteSchool website St. Anne School, founded in 1949, is a private, parochial elementary school, grades pre-Kindergarten thro...

Dr. (HC). Ir. Ary Mochtar Pedju, M.Arch (lahir 9 Juni 1936) adalah seorang teknokrat, konsultan dan pengusaha berdarah Gorontalo, Indonesia. Ary berpengalaman dalam bidang perekayasaan (engineering design) di Indonesia. PT Encona Group merupakan salah satu badan usaha terbesar yang didirikannya di Indonesia.[1] Universitas Negeri Gorontalo melalui program Pascasarjana memberikan gelar kehormatan Doktor Honoris Causa (Dr. HC) di bidang pendidikan sains dan teknologi kepadanya.[2 ...

 

 

Mexican footballer (born 1990) For other people named Héctor Herrera, see Héctor Herrera (disambiguation). In this Spanish name, the first or paternal surname is Herrera and the second or maternal family name is Lopez. Héctor Herrera Herrera with Mexico at the 2022 FIFA World CupPersonal informationFull name Héctor Miguel Herrera López[1]Date of birth (1990-04-19) 19 April 1990 (age 34)[1]Place of birth Tijuana, Baja California, Mexico[2]Height 1.80&#...

 

 

Sporting event delegationPanama at the1996 Summer OlympicsIOC codePANNOCComité Olímpico de PanamáWebsitewww.copanama.com (in Spanish)in AtlantaCompetitors7 in 6 sportsFlag bearer Eileen CoparropaMedals Gold 0 Silver 0 Bronze 0 Total 0 Summer Olympics appearances (overview)189619001904190819121920192419281932193619481952195619601964196819721976198019841988199219962000200420082012201620202024 Panama competed at the 1996 Summer Olympics, held in Atlanta, United States. Results by ev...

Italian politician Alessandro FortisPrime Minister of ItalyIn office28 March 1905 – 8 February 1906MonarchVictor Emmanuel IIIPreceded byTommaso TittoniSucceeded bySidney Sonnino Personal detailsBorn(1842-09-16)16 September 1842Forlì, Papal StatesDied4 December 1909(1909-12-04) (aged 67)Rome, Kingdom of ItalyNationalityItalyPolitical partyHistorical Left Alessandro Fortis (16 September 1842 – 4 December 1909) was an Italian politician who served as the 18th prime ministe...

 

 

Sveriges ambassad i Peking BeskickningstypAmbassadFrånSverigeTillKinaAdressBesöksadress: Sveriges Ambassad 3 Dongzhimenwai Dajie Chaoyang District Peking Postadress: Embassy of Sweden 3 Dongzhimenwai Dajie Chaoyang District 100600 Peking KinaKoordinater39°56′30″N 116°27′33″Ö / 39.94177°N 116.45927°Ö / 39.94177; 116.45927Öppnad1631BeskickningschefPer Augustsson (sedan 2023) Sveriges ambassad i Peking är Sveriges diplomatiska beskickning i Kina som är ...