OPENMP 實現快速排序

// qwe.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"
#include  <omp.h>
void quickSort(int *num,int low,int high);//進行分區
int Partition(int *num,int low,int high);//返回分離點
int _tmain(int argc, _TCHAR* argv[])
{
	int num[]={2,3,5,623,32,4324,3,24};//8
	quickSort(num,0,7);
	int i;
	for(i=0;i<8;i++)
		printf("%d\n",num[i]);
	return 0;
}


void quickSort(int *num,int low,int high)
{
	if(low<high)
	{
		int split=Partition(num,low,high);
#pragma omp parallel sections//並行區域
		{
#pragma omp section//負責這個區域的線程對前面部分進行排序
		quickSort(num,low,split-1);
#pragma omp section//負責這個區域的線程對後面部分進行排序
		quickSort(num,split+1,high);
		}

	}

}

int Partition(int *num,int low,int high)
{
	int temp=num[low];//作爲中軸
	while(low<high)
	{
		while(low<high&&num[high]>=temp)high--;
		num[low]=num[high];
		while(low<high&&num[low]<=temp)low++;
		num[high]=num[low];
	}
	num[low]=temp;
	return low;//返回中軸的位置,再進行分離
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章