SHARE
TWEET

Untitled

a guest May 26th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2.  
  3. void mergesort(int a[],int i,int j);
  4. void merge(int a[],int i1,int j1,int i2,int j2);
  5.  
  6. int main()
  7. {
  8. int a[30],n,i;
  9. printf("Enter no of elements:");
  10. scanf("%d",&n);
  11. printf("Enter array elements:");
  12.  
  13. for(i=0;i<n;i++)
  14. scanf("%d",&a[i]);
  15.  
  16. mergesort(a,0,n-1);
  17.  
  18. printf("\nSorted array is :");
  19. for(i=0;i<n;i++)
  20. printf("%d ",a[i]);
  21.  
  22. return 0;
  23. }
  24.  
  25. void mergesort(int a[],int i,int j)
  26. {
  27. int mid;
  28.  
  29. if(i<j)
  30. {
  31. mid=(i+j)/2;
  32. mergesort(a,i,mid);
  33. mergesort(a,mid+1,j);
  34. merge(a,i,mid,mid+1,j);
  35. }
  36. }
  37.  
  38. void merge(int a[],int i1,int j1,int i2,int j2)
  39. {
  40. int temp[50];
  41. int i,j,k;
  42. i=i1;
  43. j=i2;
  44. k=0;
  45.  
  46. while(i<=j1 && j<=j2)
  47. {
  48. if(a[i]<a[j])
  49. temp[k++]=a[i++];
  50. else
  51. temp[k++]=a[j++];
  52. }
  53.  
  54. while(i<=j1)
  55. temp[k++]=a[i++];
  56.  
  57. while(j<=j2)
  58. temp[k++]=a[j++];
  59.  
  60.  
  61. for(i=i1,j=0;i<=j2;i++,j++)
  62. a[i]=temp[j];
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top