Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
18BCE2182 ASSESS_3 Q3
//gcc 7.4.0 #include <stdio.h> #include <stdlib.h> #include <omp.h> void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; int L[n1], R[n2]; for (i = 0; i < n1; i++) { L[i] = arr[l + i]; } for (j = 0; j < n2; j++) { R[j] = arr[m + 1 + j]; } i = 0; j = 0; k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } void mergeSort(int arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; mergeSort(arr, l, m); mergeSort(arr, m + 1, r); merge(arr, l, m, r); } } void quicksort(int number[], int first, int last) { int i, j, pivot, temp; if (first < last) { pivot = first; i = first; j = last; while (i < j) { while (number[i] <= number[pivot] && i < last) i++; while (number[j] > number[pivot]) j--; if (i < j) { temp = number[i]; number[i] = number[j]; number[j] = temp; } } temp = number[pivot]; number[pivot] = number[j]; number[j] = temp; quicksort(number, first, j - 1); quicksort(number, j + 1, last); } } void sequentially(int arr[], int n) { quicksort(arr, 0, n); mergeSort(arr, 0, n); } int main() { int n=10; int arr[n]; for (int i = 0; i < n; i++) { arr[i] = rand() % 1000; } double start = omp_get_wtime(); sequentially(arr, n); double end = omp_get_wtime(); printf("time took sequentially %lf \n", end - start); start = omp_get_wtime(); #pragma omp parallel { #pragma omp sections { #pragma omp section { mergeSort(arr, 0, n); } #pragma omp section { quicksort(arr, 0, n); } } } end = omp_get_wtime(); printf("time took parallely %lf \n", end - start); }
run
|
edit
|
history
|
help
0
Is const really const?
Project 3 part 2 Book v1.1
Lior Yehieli Lesson
Project 5 v.03
Cbinsearch
Pregunta 3 de registros
UserDefinedDataType_pointer1
PREGUNTA 10 CADENAS
-Wall
20171103_factorial