Quick sorting of OpenMP

Source: Internet
Author: User
# Include <stdio. h> # include <stdafx. h> # include <iostream> # include <stdlib. h> # include <time. 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 we select the first element as the sequence or the last element as the sequence, if we want a sequence from small to large, the worst input // case is from large to small. If we want to get a sequence from large to small, the worst input condition is the sequence from small to large */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]; // -------- divide B [] into two small arrays and call the fast Sorting Algorithm for them in parallel # pragma OMP parallel default (none) shared (B, c, Len) private (I) // --- this for loop is parallel {Int J = 50000; # pragma OMP 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) shared (B, D, E, short_len) Private (I) // --- this for loop is parallel {# pragma OMP for (I = 0; I <short_len; I ++) // --- this for loop is parallel {d [I] = B [I]; // Add the first 5000 numbers of B [] to d [] E [I] = B [I + 5000]; // put the last 5000 of B [] into E [] }}# Pragma OMP parallel default (none) shared (E, D, short_len) // Private (I) ------ parallel region for fast sorting {# pragma OMP parallel sections {# pragma OMP section quicksort (D, 0, short_len-1 ); // sort by d [] # pragma OMP section quicksort (E, 0, short_len-1); // sort by E []} For (; k <Len; k ++) // ---------- merge and sort d [] and e [] into B [] {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 ++; }}// ---------------------------- end of the merge algorithm clock_t end = clock (); // ---------------- cost = (double) (end-begin ); 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 ");}

Quick sorting of OpenMP

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.