Skip to content.

Sections
Personal tools
You are here: Home » ダウンロード » ThreadJack用 » multimerge

Document Actions
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <threadjack.h>
 

class SortThread : public TJThread {
public:
    SortThread(int* array, int num) : myArray(array), myNum(num) { }

    virtual void run() {
        // ソート処理(バブルソート)
        for (int i=0; i<myNum-1; i++) {
            for (int j=i+1; j<myNum; j++) {
                if (myArray[i] > myArray[j]) swap(myArray[i], myArray[j]);
            }
        }
    }

private:
    int* myArray;
    int  myNum;
};
 

int main() {
    int numbers[100]  = {
        19, 17,  1, 44, 18, 26, 74,  7,  8,  9,
         0, 98, 86, 89, 94, 36, 43, 39, 34, 87,
        80, 28, 27, 49,  5, 52, 33, 22, 93, 65,
        30, 45, 32, 82, 73, 35, 21, 66, 16,  4,
        40, 78, 42, 76, 83, 61, 97, 47, 75, 23,
        50, 31, 96, 12, 53, 55, 58, 15, 38, 63,
        24, 37, 56, 88, 64, 14, 46, 91, 68, 69,
        59, 81, 72, 10, 90, 95, 41, 77, 13, 84,
        67, 62, 25, 99, 60, 70, 20, 71,  3, 85,
        29, 79, 92,  2, 54, 11, 57, 51, 48,  6
    };

    SortThread* st[10];
    for (int i=0; i<10; i++) st[i] = new SortThread(&numbers[i*10], 10);
    for (int i=0; i<10; i++) st[i]->start();
    for (int i=0; i<10; i++) st[i]->join();

    for (int i=0; i<9; i++)
        inplace_merge(&numbers[0], &numbers[i*10+10], &numbers[i*10+20]);

    for (int i=0; i<10; i++) {
        cout << i << " : ";
        for (int j=0; j<10; j++) {
            cout << numbers[i*10+j] << ", ";
        }
        cout << endl;
    }
}