Latest Updates ☛
  • Graphs: Introduction of graphs and types of graphs
  • SPOJ : Geeky solves geeky solution
  • Solution Code : Pass the time problem on SPOJ.
  • Find the minimum shortest path solution in c language ! !

Friday, August 9, 2013

C++ program to perform MergeSort using Divide and conquer technique..



Merge Sort using C++

  This program makes the use of recursion in order to sort the elements of the array. The technique used here is of the famous algorithm divide and conquer. Its complexity is O(n log n).







 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<stdio.h>
#include<conio.h>

//function prototyping
void mergesort(int *a,int *temp,int l,int u);
void merge(int *a,int *temp,int l,int u);

//main method to perform operations
main()
{
      int n;      
      printf("Enter the value of n-array size");
      scanf("%d",&n);
     
      //declaring arrays
      int a[n];
      int temp[n];

      
      //adding contents to array
      for(int i=0;i<n;i++)
     {
               scanf("%d   ",&a[i]);
               temp[i]=0; //intializing temp
     }
      
      puts("Array before sorting : ");
      for(int i=0;i<n;i++)
      printf("%d   ",a[i]);
      
      //calling function
      mergesort(a,temp,0,n-1);
      
      //Displaying sorted array
      puts("\nArray after sorting");
      for(int i=0;i<n;i++)
      printf("%d   ",a[i]);
      getch();
}

//functon to mergesort elements of array
void mergesort(int *a,int *temp,int l,int u)
{
       int m; 
       if(l<u)
       {
             m=(l+u)/2;
             mergesort(a,temp,l,m);
             mergesort(a,temp,m+1,u);
             merge(a,temp,l,u);
       }
}

//function to merge array elements
void merge(int *a,int *temp,int l,int u)
{
         int m=(l+u)/2;
         int i=l,j=m+1;
         int c;
         for(c=l;i<=m&&j<=u;c++)
         {
                if(a[i]==a[j])
                {
                    temp[c++]=a[i];
                    temp[c]=a[i];
                    i++;
                    j++;
                }
                else
                {
                    if(a[i]<a[j])
                    {
                        temp[c]=a[i];
                        i++;
                    }
                    else
                    {
                        temp[c]=a[j];
                        j++;
                    }
                }
         }
         for(;i<=m;)
         temp[c++]=a[i++];
         for(;j<=u;)
         temp[c++]=a[j++];
         for(i=l;i<=u;i++)
         a[i]=temp[i];
}                                    

The output of the program is--------------------


Enter the value of n-array size 10
18 45 3 11 8 0 32 1 67 2

Array before sorting :
18   45   3   11   8   0   32   1   67   2

Array after sorting
0   1   2   3   8   11   18   32   45   67

1 comment :

Privacy Policy | Disclaimer | Terms of Service

Copyright © 2013, Code Author. Powered by Blogger.