Active Members
TodayLast 7 Daysmore...



Ask Experts » Computers » Software »

How to write merge sorting algorithm in C language

Date: 04 Jul 2012   Group: Computers    Category: Software   


Conceptually, a merge sort works as follows

Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).

Repeatedly Merge sublists to produce new sublists until there is only 1 sublist remaining. (This will be the sorted list.)

But I dont know its program in C. Can anyone help me out with this?



Find Answers
Looking for merge sorting algorithm in C language? Here are the best merge sorting algorithm based C programs for students.

Author: Noorul Hasan    09 Jul 2012      Member Level: Silver     Points : 10  (Rs 6)    Voting Score: 0
The working of merge sort is as follows:

1) First you divide the number of elements by 2 and seperate them as two.

2) Divide those two which are divided by 2.

3) Divide them until you get a single element.

4) Start comparing the starting two pair of elements with each other and place them in ascending order.

5) When you combine them compare them so that you make sure they are sorted.

6) When all the elements are compared the array will be surely sorted in an ascending order.

Algorithm for merge sort is:
// For 5 elements

Step 1:Start

Step 2:Declare and low,mid,high where low=a[0], mid= (low + high)/2 and high = a[4]

Step 3:check for the condition if (low < high)

Step 4:Merge (item, low, mid)

Step 5:Merge (item, mid+1, high)

Step 6:For (I=low, J=mid +1, K=low; I<=mid AND J<=high; K++)

Step 7:If (item [I]
Step 8:Set temp [K] is equals to item [I] i++

Step 9:Else

Step 10: set temp [K] is equals to item [J] j++
[ end if ]
[ end for loop ]

Step 11:While (I<=mid)

Step 12:Set temp [K] = item [I] i++ k++

Step 13:While (J<=high)

Step 14:Set temp [K] = item [J] j++ k++

Step 15:For (K=low; K<=high; K++) Update item [K] as temp [K];
[End for ]
[End while loop 2]
[ End while loop 1]

Step 16: Stop

Author: Adi    09 Jul 2012      Member Level: Silver     Points : 10  (Rs 5)    Voting Score: 0
I am giving you the code in C.Refer it.Use "code blocks" compiler,if it does not run in turbo c.
For running it in turbo c make some changes in the code,like add the headers and clrscr(),getch(),etc.

Good Luck!!

See the attachment.


merge.c.txt


Answers      





Post answer or solution to "How to write merge sorting algorithm in C language"

You can earn revenue online from Google AdSense and our revenue sharing programs by post valid, self written answers for the problem How to write merge sorting algorithm in C language




You must Sign In to post a response.


Next Question: How to increase my computer speed
Previous Question: How to repair MBR of a hard disk?
Return to Question Index
Ask Question to Expert
Category:


 
Related Questions
Popular Tags   What are tags ?   Search Tags  
Merge Sort  .  Computer Algorithm  .  

Awards & Gifts
ISC Technologies, Kochi - India. Copyright © All Rights Reserved.