选择排序的openMP实现

时间:2023-03-09 10:01:31
选择排序的openMP实现
// test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include "omp.h"
#include <iostream>
using namespace std;
void swap(long *a, long *b); //交换两个数
int _tmain(int argc, _TCHAR* argv[])
{

    omp_set_num_threads();
    ];
    long     i;
    clock_t t1 = clock();
    //初始化数组
#pragma omp parallel for
    ; i < ; i++)
    {
        str[i] =  - i;
    }

#pragma omp parallel for
    ; i < ; i++)
    {
        ; j < ; j++)
        {
            if (str[i] > str[j])
            {
                swap(&str[i], &str[j]);
            }
        }
    }

    clock_t t2 = clock();

    //for (i = 0; i < 10000; i++)
    //printf("%d\n", str[i]);

    clock_t t3 = t2-t1;

    printf("parallel time =%d\n",t3);

    ];
    long     m;
    t1 = clock();
    ; m < ; m++)
    {
        str[m] =  - m;
    }

    ; m < ; m++)
    {
        ; n < ; n++)
        {
            if (str[m] > str[n])
            {
                swap(&str[m], &str[n]);
            }
        }
    }

    t2 = clock();

    //for (m = 0; m < 10000; m++)
    //printf("%d\n", str[m]);
    clock_t t4 = t2-t1;

    printf("serial time=%d\n",t4);

    printf("加速比:%f\n",(t4*1.0/t3));

    system("Pause");
    ;
}

void swap(long *a, long *b)
{
    long     c;
    c = *a;
    *a = *b;
    *b =  c;
}