1 / 21

Merge Sort

Merge Sort Merging refers to combining arrays. Merge sort involves the technique of merging two sorted arrays to form a single sorted one. The popular way of merging is to sort the array while merging it.

binta
Download Presentation

Merge Sort

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Merge Sort Merging refers to combining arrays. Merge sort involves the technique of merging two sorted arrays to form a single sorted one. The popular way of merging is to sort the array while merging it.

  2. The technique is as follows:- Pointers are set for both the input arrays (say A and B ) and the resultant array (say C) Initially all the pointers are set to 1, thus pointing to the first elements.

  3. Now, the first elements of both the input arrays (A and B) are compared. The smaller one among them is inserted as the first element of the resultant array (say C). • If the smaller one is the first element in array A, then after inserting that element into C, the pointer of A is incremented and set to 2. Otherwise, the pointer of array B is incremented after inserting first element of B into C. The pointer of C is also incremented after inserting the first element.

  4. The process is repeated until all the elements of any one of the arrays have been inserted into array C. • Now, the rest of the elements of the other array are just copied into array C as they are already sorted. • Note that the merged array should be sufficiently big to hold the elements of both the input arrays.

  5. 1 2 3 4 5 6 7 1 2 3 4 5 • Consider the following arrays. A:- B:- C:- Here, the elements corresponding to the pointers, ie, 2 and 3 are compared. The first element in the array A(ie, 2) is inserted into array C as it is the smaller one.

  6. Now, the pointer of array A is incremented and reset to point to the second element. Also the pointer of array C is incremented to point to the second element. Now the array C looks as follows:-

  7. 1 2 3 4 5 6 7 A:- 1 2 3 4 5 B:- C:- The elements 5 and 3 are compared. The smaller one is 3 and it is inserted in the position corresponding to the pointer in array C. And then, pointer C is incremented to position 3.

  8. 1 2 3 4 5 6 7 A:- 1 2 3 4 5 B:- C:- The elements 5 and 5 are compared. The 5 in array A is inserted into array C. And then, pointer C is incremented.

  9. 1 2 3 4 5 6 7 A:- 1 2 3 4 5 B:- C:-

  10. 1 2 3 4 5 6 7 A:- 1 2 3 4 5 B:- C:-

  11. 1 2 3 4 5 6 7 A:- 1 2 3 4 5 B:- C:-

  12. 1 2 3 4 5 6 7 A:- 1 2 3 4 5 B:- C:-

  13. 1 2 3 4 5 6 7 A:- 1 2 3 4 5 B:- C:-

  14. 1 2 3 4 5 6 7 A:- 1 2 3 4 5 B:- C:-

  15. 1 2 3 4 5 6 7 A:- 1 2 3 4 5 B:- C:-

  16. 1 2 3 4 5 6 7 A:- 1 2 3 4 5 B:- C:-

  17. 1 2 3 4 5 6 7 A:- 1 2 3 4 5 B:- C:-

  18. Now, the array C is sorted and merged with the elements of both A and B. If array B has more elements say, 25, 32 and 56 after 15, they are simply added into array C after 15 as all elements in array B are over.

  19. Sorting Technique using Merge Sort where N & M are the sizes of the arrays A and B . C is the resultant array of size M+N. PTRA,PTRB,PTRC variables pointing to elements in the arrays A, B and C

  20. Algorithm 1. PTRA = 0 , PTRB = 0 , PTRC = 0 2. WHILE ( PTRA < N ) AND (PTRB< M) IF A[PTRA] < B[PTRB] THEN C[PTRC] = A[PTRA] PTRA++ PTRC++ ELSE C[PTRC] =B[PTRB] PTRB++ PTRC++ END WHILE

  21. 3. IF PTRA >= N then While PTRB < M C[PTRC] = B[PTRB] PTRC++ PTRB++ End While End IF 4. IF PTRB >= M While PTRA < N C[PTRC] = A[PTRA] PTRC++ PTRA++ End While End IF 5. STOP

More Related