Choose Category
#include<stdio.h> #include<omp.h> #include<stdlib.h> #include<time.h> int n; void simple_merge(int a[],int low,int mid,int high); void merge_sort(int a[],int low,int high) int main() { int a[100],i=0; double start_time,end_time,val; printf("\n enter the size of the array elements:"); scanf("%d",&n); for(i=1;i<=n;i++) a[i]=rand()%100; printf("\n array of elements before sorting are:\n"); for(i=1;i<=n;i++) printf("%d\t",a[i]); start_time=omp_get_wtime(); merge_sort(a,1,n) end_time=omp_get_wtime(); val=(end_time-start_time); printf("\n the time taken to sort the array elements is %f",val); printf("\n the elements after sorting are:\n"); for(i=1;i<=n;i++) printf("%d\t",a[i]); return 0; } void simple_merge(int a[],int low,int mid,int high) { int i=low,j=mid+1;k=low,c[n]; while(i<=mid && j<=high) { if(a[i]<a[j]) { c[k]=a[i]; i++; k++; } else { c[k]=a[j]; j++; k++; } } while(i<=mid) c[k+1]=a[i++]; while(j<=high) c[k++]=a[j++]; for(i=low;i<=high;i++) a[i]=c[i]; } void merge_sort(int a[],int low,int high) { int mid; if(low<high) { mid=(low+high)/2; #pragma omp parallel sections { #pragma omp section merge sort(a,low,mid); #pragma omp section merge sort(a,mid+1,high); } simple_merge(a,low,mid,high); } }