Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to program simple mergesort problem with C language.
but I still have segmentation fault and I'm stuck in it. PLEASE HELP...!

What I have tried:

C++
#include <stdio.h>
#include <stdlib.h>

void merge(int *arr, int start, int mid, int end)
{
	int i, j, k;
	int arrsize=end-start+1;
	int sm_size=mid-start+1;
	int me_size=end-mid+1;
	
	for(i=0; i < arrsize; i++)
        {
            if(j < sm_size && k < me_size)
                if(*(arr+start+j) > *(arr+mid+k))
			{
				*(arr+i) = *(arr+start+j);
				j++;
			}
			
			else
			{
				*(arr+i) = *(arr+mid+k);
				k++;
			}
		}
		
		else if(j
Posted
Updated 5-Nov-16 21:26pm
v6
Comments
Richard MacCutchan 6-Nov-16 3:27am    
Your code is incomplete, but if you look at what you do have above, you are using the variables i, j and k without initialising them so this will never work.
Albert Holguin 8-Nov-16 13:15pm    
You're doing so much pointer arithmetic that you're likely going out of bounds. Use a debugger or put some print statements to figure out where you're going out of bounds.

1 solution

You should think about initializing j and k before using them.
I fear you need to learn the debugger quickly because on first look your program will kill a part of the values to sort. If I remember well, the merge is not done in same array.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Advice: take a sheet of paper and try to do it by hand, your program should use the same procedure.
 
Share this answer
 
v2
Comments
Richard MacCutchan 6-Nov-16 3:23am    
You should really stop posting this as a Solution, it's more a comment.
Patrice T 6-Nov-16 5:20am    
If you read carefully the first 2 sentences, you will see that I spot 2 problems in the code and then suggest to learn the debugger which would have shpw them.
Richard MacCutchan 6-Nov-16 7:58am    
Yes, I missed that part. But I do see this general comment posted as Solution to rather a lot of questions.
Patrice T 6-Nov-16 12:30pm    
I usually don't post this alone, either on beginning or at end, I put something directly related to the question.
It is unfortunate that so many beginners don't learn debugger as part of their lessons.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900