0% found this document useful (0 votes)
206 views6 pages

Merge Sort Ada

Merge sort is a sorting algorithm based on the divide and conquer paradigm. It divides the array into halves, recursively sorts them, and then merges the sorted halves into a single sorted array. Its worst case running time has a lower order of growth than insertion sort. Merge sort works by dividing the array into subproblems of sorting subarrays, recursively sorting the subarrays, and then merging the sorted subarrays back into a fully sorted array.

Uploaded by

ridam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
206 views6 pages

Merge Sort Ada

Merge sort is a sorting algorithm based on the divide and conquer paradigm. It divides the array into halves, recursively sorts them, and then merges the sorted halves into a single sorted array. Its worst case running time has a lower order of growth than insertion sort. Merge sort works by dividing the array into subproblems of sorting subarrays, recursively sorting the subarrays, and then merging the sorted subarrays back into a fully sorted array.

Uploaded by

ridam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Merge Sort

Merge sort is based on the divide-and-conquer paradigm. Its worst-case


running time has a lower order of growth than insertion sort. Since we are dealing
with subproblems, we state each subproblem as sorting a subarray A[p .. r].
Initially, p = 1 and r = n, but these values change as we recurse through
subproblems.
To sort A[p .. r]:
1. Divide Step
If a given array A has zero or one element, simply return; it is already
sorted. Otherwise, split A[p .. r] into two subarrays A[p .. q] and A[q +
1 .. r], each containing about half of the elements of A[p .. r]. That
is, q is the halfway point of A[p .. r].
2. Conquer Step
Conquer by recursively sorting the two subarrays A[p .. q] and A[q +
1 .. r].
3. Combine Step
Combine the elements back in A[p .. r] by merging the two sorted
subarrays A[p .. q] and A[q + 1 .. r] into a sorted sequence. To
accomplish this step, we will define a procedure MERGE (A, p, q, r).

Algorithm: Merge Sort


To sort the entire sequence A[1 .. n], make the initial call to the procedure
MERGE-SORT (A, 1, n).
MERGE-SORT (A, p, r)
1.
2.
3.
4.
5.

IF p < r
THEN q = FLOOR [(p + r)/2]
MERGE (A, p, q)
MERGE (A, q + 1, r)
MERGE (A, p, q, r)

// Check for base case


// Divide step
// Conquer step.
// Conquer step.
// Conquer step.

MERGE (A, p, q, r )
1.
n1 q p + 1
2.
n2 r q
3.
Create arrays L[1 . . n1 + 1] and R[1 . . n2 + 1]
4.
FOR i 1 TO n1
5.
DO L[i] A[p + i 1]
6.
FOR j 1 TO n2
7.
DO R[j] A[q + j ]
8.
L[n1 + 1]
9.
R[n2 + 1]
10. i 1
11. j 1
12. FOR k p TO r
13.
DO IF L[i ] R[ j]
14.
THEN A[k] L[i]
15.
ii+1
16.
ELSE A[k] R[j]
17.
jj+1

Analyzing Merge Sort

T(n) = (n lg n).

Implementation
1.

#include <iostream>

2.

using namespace std;

3.

#include <conio.h>

4.

#include<time.h>

5.

#include<dos.h>

6.

void merge(int *,int, int , int );

7.

void mergesort(int *a, int low, int high)

8.

9.

int mid;

10.

if (low < high)

11.

12.

mid=(low+high)/2;

13.

mergesort(a,low,mid);

14.

mergesort(a,mid+1,high);

15.

merge(a,low,high,mid);

16.

17.

return;

18.

19.

void merge(int *a, int low, int high, int mid)

20.

21.

int i, j, k, c[50];

22.

i = low;

23.

k = low;

24.

j = mid + 1;

25.

while (i <= mid && j <= high)

26.

27.

if (a[i] < a[j])

28.

29.

c[k] = a[i];

30.

k++;

31.

i++;

32.

33.

else

34.

35.

c[k] = a[j];

36.

k++;

37.

j++;

38.

39.

40.

while (i <= mid)

41.

42.

c[k] = a[i];

43.

k++;

44.

i++;

45.

46.

while (j <= high)

47.

48.

c[k] = a[j];

49.

k++;

50.

j++;

51.

52.

for (i = low; i < k; i++)

53.

54.

a[i] = c[i];

55.

56.

57.

int main()

58.

59.

Clock_t start,end;

60.

int a[20], i, b[20];

61.

cout<<"enter the elements\n";

62.

for (i = 0; i < 5; i++)

63.

64.
65.
66.
67.

cin>>a[i];
}
Start=clock();
mergesort(a, 0, 4);

68.

End=clock();

69.

cout<<"sorted array\n";

70.

for (i = 0; i < 5; i++)

71.

72.

cout<<a[i];

73.
74.

}
Cout<<the time taken by algorithm is:<<(end-start)/CLK_TCK;

75.
76.

getch();
}

Worst case:

Best case

You might also like