OpenMP Selection Sort

#include <iostream> #include <omp.h> #include <conio.h> #include <Windows.h> using namespace std; void ompsort(int *a, int *b, int count){ #pragma omp parallel for num_threads(8) for (int i = 0; i < count; i++){ int loc = 0; for (int j = 0; j < count; j++){ if (a[j] < a[i]) loc++; } b[loc] = a[i]; } } void last_found_fill(int *a, int len){ int last_found = -1; for (int i = 0; i < len; i++){ if (a[i] != -1){ last_found = a[i]; } else{ a[i] = last_found; } } } int main(){ int a[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 5, 5, 4, 2, 3, 5, 6, 7, 7, 8, 7, 6, 4, 3, 6, 2, 3, 5, 6, 7, 8, 9, 7, 6, 4, 3 }; int len = sizeof(a) / sizeof(*a); int *b = new int[len]; memset(b, -1, len*sizeof(int)); ompsort(a, b, len); last_found_fill(b, len); for (int i = 0; i < len; i++) cout << b[i] << " "; _getch(); return 0; }
The program uses two functions, ompsort and last_found_fill . In case of distinct elements, last_found_fill function can be omitted.

For straight away use,
Compile using Visual Studio 2013 Community with OpenMP switch enabled in the Property Sheet

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.