Quick sort of OpenMP

Source: Internet
Author: User
#include <stdio.h> #include <stdafx.h> #include <iostream> #include <stdlib.h> #include <tim
e.h> #include "omp.h" using namespace std;  
	int count=0;  
		void swap (int &a, int &b)//{int tmp;  
		TMP = A;  
		A = b;  
	b = tmp;  
		} void quicksort (int *a,int L, int u) {int i,m,k;   
		if (l >= u) return;  
		m = l; for (i = l+1; I <= u; i++) if (A[i] < a[l])/* Whether you choose the first element as pivot or the last as pivot, if we want a small to large sequence, then the worst input//situation is from the big to  
		Small, if we want to get from the big to the small sequence, the worst input is from small to large sequence */swap (a[++m], a[i]);    
		Swap (A[l], a[m]);  
		Quicksort (a,l, m-1);  
	Quicksort (a,m+1, u);   
		} void Main (int argc, char *argv) {omp_set_num_threads (2);//----------------Set the number of threads to 2 because it is a dual-core CPU int k=0,i=0;  
		int m=0,n=0;  
		Double cost=0;  
		int len=10000;  
		int SHORT_LEN=LEN/2; int b[10000],c[10000],d[5000],e[5000];//--------will b[] into two small arrays, parallel to them call fast sort algorithm #pragma omp parallel default (None) Shared (B,c,len) private (i)//--This for loop is a parallel {int j=50000;
				#pragma omp for for (i=0;i<len;i++) {b[i]=j--;
				c[i]=j--; Initialize b[],c[] Array}} clock_t begin = Clock ();//----------------timing start point #pragma omp parallel default (None) Sha   
			Red (B,d,e,short_len) Private (i)//---this for loop is parallel {#pragma omp for for (i=0;i<short_len;i++)//---this for loop is parallel  
		{d[i]=b[i];//The first 5,000 digits of b[] into d[] e[i]=b[i+5000];//the last 5,000 of B[]}} #pragma omp parallel default (None) Shared (E,d,short_len)//private (i)------fast sort parallel Region {#pragma omp parallel s ections {#pragma omp section quicksort (D, 0, short_len-1);//d[] sort #pragma omp section qui Cksort (E, 0, short_len-1),//e[] sort}} for (; k<len;k++)//----------d[] and e[] merge sort into b[] inside {if (m  
				<short_len && N<short_len) {if (D[n]<=e[m]) {b[k] = D[n];  
			n++;  
				} else {b[k]=e[m];  
			m++; }  
		} if (M==short_len | | n==short_len) {if (M==short_len) b[k]=e[m];  
				else b[k]=d[n-1];  
				K+=1;  
			Break  
			}} if (/*m==short_len &&*/n<short_len) {int tem=short_len-n;  
				for (int p=0;p<tem;p++) {b[k]=d[n];  
				n++;  
			k++;  
			}} else if (/*n==short_len &&*/m<short_len) {int tem=short_len-m;  
			for (int q=0;q<tem;q++) {b[k]=e[m];  
			m++;  
		k++; }}//----------------------------merge algorithm ends clock_t end = Clock ();//----------------timing end point cost = (double) (end-be  
		gin);
		cout<< "Parallel Time" <<cost<<endl;
		Serial start Begin=clock ();
		Quicksort (C, 0, len-1);
		end = Clock ();  
		Cost = (double) (end-begin);
		cout<< "Serial Time" <<cost<<endl;  
		System ("pause");  }

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.