Programming Algorithms-K-Way merge sort (k-way merge sort) code (c + +)

Source: Internet
Author: User

K-Way Merge sort (k-way merge sort) code (c + +)


This address: Http://blog.csdn.net/caroline_wendy


The K-way Merge sort is a classic external sorting algorithm that programmers must master.

Knowledge Concept Reference: < data structure >


main idea : in K-sorted files, select the first value, use the loser tree, update the binary tree structure, and finally select the optimal value.

The code is for reference only, such as the minimum value (-1) instead of the maximum value (100) instead.


/* * main.cpp * * Created on:2014 September 11 * author:spike * * #include <fstream> #include <iostream> #include <vector>using namespace Std;class kwaymergesort{public:kwaymergesort (vector<ifstream*>& vif): m_ VIFs (vif), M_ofs ("OUT.txt") {}void calculate () {/*) read from the K input merge segment the keyword of the current first record of the paragraph to the outer node */for (int i = 0;i < K;++i) {input (I, B[i]);}    Createlosertree ();        while (b[ls[0] = Maxkey) {/* Q indicates that the current minimum keyword is in the merge segment */int q = ls[0];        /* Write the current (keyword B[q].key) record in the merge segment numbered Q to the output merge segment */cout << b[q] << "";        M_ofs << B[q] << "";        /* The key to read the next record in the input merge section of number Q */input (q, b[q]);    /* Adjust the loser tree, select the new minimum keyword */adjust (q);        }/* Writes records containing the maximum keyword Maxkey to the output merge segment */cout << Endl; M_ofs << Endl;} private:void input (int i, int& b) {if ((*m_vifs[i]). Good ()) {(* (M_vifs[i])) >> b;}} /*** known b[0] to b[k-1] for the complete binary tree ls leaf node, there is a K keyword, along the K-path from the leaves * to the root to adjust the LS to become the loser tree.   */void Createlosertree () {b[k] = Minkey; /* Set the initial value of "loser" in LS */for (int i = 0; i < K; ++i) {ls[i] = k;    }/* from b[k-1],b[k-2],...,b[0] adjust loser */for (int i = k-1; I >= 0; i) {adjust (i);  }}void adjust (int i) {/* Ls[t] is a parent node of b[s] */int t = (i + k)/2;while (T > 0) {/* s indicates new winner */if (B[i] > B[ls[t]]) {int TMP = I;i = ls[t];ls[t] = tmp;} t = T/2;} Ls[0] = i;} private:static const INT k = 4;static const int minkey = -1;static const int maxkey = 100;int b[k+1]; The leaf node of the loser tree int ls[k]; The remaining nodes of the loser tree vector<ifstream*> m_vifs;ofstream m_ofs;}; int main (void) {vector<ifstream*> vif;ifstream* ifs0 = new Ifstream ("F0.txt"); ifstream* ifs1 = new Ifstream ("F1.txt ifstream* IFS2 = new Ifstream ("F2.txt"), ifstream* ifs3 = new Ifstream ("F3.txt"); Vif.push_back (IFS0); Vif.push_back ( IFS1); Vif.push_back (IFS2); Vif.push_back (IFS3); Kwaymergesort K (vif); K.calculate ();d elete ifs0;delete ifs1;delete ifs2;delete ifs3;return 0;}

The file to be sorted, and the last is the tag to stop updating the loser tree:

F0:5-100f1:7 22 47 48 59 100 (100F2:29-100f3:9)

Output:







Programming Algorithms-K-Way merge sort (k-way merge sort) code (c + +)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.