Discover the depth  of  C

  C C++  Linux ProgrammingNEW   Operating Systems  Data StructuresNEW  Compilers  Contact Us

Navigation

Join C4SwimmersGroup

 sitepromotion.gif (577 bytes) New Member

Data Structures

  Introduction

Stacks

  About Stacks

  Types of Notations

  Infix to Postfix

  Infix to Prefix

  Evaluating Postfix

Queues

  About Queues
  Circular Queue
  Priority Queue
Linked Lists
 About Linked Lists
 Singly Lists
 Doubly Lists
 Circular Lists
 Circular Doubly Lists
Sorting
 Bubble Sort
 Insertion Sort
 Selection Sort
 Shell Sort
 Merge Sort
 Quick Sort
Searching
 About Searching
 Linear Search
 Binary Search
Online Certifications
premiumservices.gif (1070 bytes) Brainbench
premiumservices.gif (1070 bytes) Benchmarks Global
Help and Support
customer_care_small.gif (1018 bytes) Suggestions
post-question_icon.gif (1062 bytes) Contribute
premiumservices.gif (1070 bytes) Feedback
premiumservices.gif (1070 bytes) Advertise with us
 

 memberhome.gif (1052 bytes) -Home Page-

 
 
.  

C For Swimmers : Mastering C step-by-step

Google


WWW c4swimmers.esmartguy.com

Merge Sort

 

Definition : 

Merge-sort is based on the divide-and-conquer paradigm. The Merge-sort algorithm can be described in general terms as consisting of the following three steps:

1. Divide Step 
If given array A has zero or one element, return S; it is already sorted. Otherwise, divide A into two arrays, A1 and A2, each containing about half of the elements of A. 

2. Recursion Step 
Recursively sort array A1 and A2. 

3. Conquer Step 
Combine the elements back in A by merging the sorted arrays A1 and A2 into a sorted sequence. 


ALGORITHM : MERGE SORT

To begin, suppose that we have two sorted arrays  A1[1], A1[2], . . , A1[M] and A2[1], A2[2], . . . , A2[N]. The following is a direct algorithm of the obvious strategy of successively choosing the smallest remaining elements from A1 to A2 and putting it in A.
MERGE (A1, A2, A)

i = j1
A1[m+1], A2[n+1] = INT_MAX
For k = 1 to m + n do
    if A1[i] < A2[j]
        then A[k] = A1[i]
            i = i +1
        else
            A[k] = A2[j]
                j = j + 1

The Merge sort algorithm is as follows:

MERGE_SORT (A)

A1[1 . . floor(n/2)] = A[1 . . floor(n/2)]
A2[1 . . floor(n/2)] = A[1 + floor(n/2) . . n]
Merge Sort (A1)
Merge Sort (A1)
Merge Sort (A1, A2, A)

 

FREE Source Code

premiumservices.gif (1070 bytes) Click Here: Merge Sort - Ascending Order

 

Animation Effect
premiumservices.gif (1070 bytes)Click Here : To learn more about Merge Sort

You are Visitor No.

Sign my Guestbook View my Guestbook

Subscribe to C4Swimmers Group
Designed and Maintained by  Nanda Kishor

Thanks for using C For Swimmers.
Regarding this material, you can send Bug Reports, Suggestions, Comments, etc. to

nandakishorkn@rediffmail.com

Note: All logos or trademarks are related to their respective owners.

Although every precaution has been taken, the designer(s) owe no responsibility for malicious errors.

No liability assumed for damages resulting from the use of the available information.