Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi I want to sort an array of pointers by the addresses of what is inside the pointers.
That is, the array holds a pointer within a particular cell within an array that contains strings.
Want to do this with the help of MERGE SORT
The array of pointer is char** ptr_to_chars_array

What I have tried:

C
void mergeSort(char* arr, int size)
{
	char* tmpArr = NULL;
	if (size <= 1)
		return;

	mergeSort(arr, size / 2);
	mergeSort(arr + size / 2, size - size / 2);
	tmpArr = (char*)malloc(size * sizeof(char));

	if (tmpArr)
	{
		merge(arr, size / 2, arr + size / 2, size - size / 2, tmpArr);
		for (int i = 0; i < size; i++)
		{
			arr+i = &tmpArr+i;
		}
		free(tmpArr);

		
	}
	else
	{
		printf("Memory allocation failure!!!\n");
		exit(1);	// end program immediately with code 1 (indicating an error)
	}
}

void merge(char* a1, int size1, char* a2, int size2, char* tmpArr)
{
	int ind1, ind2, tmpArrInd;
	ind1 = ind2 = tmpArrInd = 0;

	while ((ind1 < size1) && (ind2 < size2))
	{
		if (a1+ind1 >= a2+ind2)
		{
			tmpArr[tmpArrInd] = a1 + ind1;
			ind1++;
		}
		else {
			tmpArr[tmpArrInd] = a2 + ind2;
			ind2++;
		}
		tmpArrInd++;
	}

	while (ind1 < size1)
	{
		tmpArr[tmpArrInd] = a1 + ind1;
		ind1++;
		tmpArrInd++;
	}
	while (ind2 < size2) {
		tmpArr[tmpArrInd] = a2 + ind2;
		ind2++;
		tmpArrInd++;
	}
}
Posted
Updated 19-Jul-22 17:24pm
v2
Comments
OriginalGriff 11-Mar-22 8:10am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.

Perhaps an example of the input and output will help us understand exactly what you are trying to do, together with some explanation of what problem you are having with what code you already have?

Use the "Improve question" widget to edit your question and provide better information.
Richard MacCutchan 11-Mar-22 8:12am    
Pointers are just numbers, so you sort them the same way you sort any other values.

1 solution

He wants to sort some data.

He begins with, "hi I want to sort".

He later details that with, "Want to do this with the help of MERGE SORT".


- - - - - - - - - -


OK, here is MERGE SORT:

You could start with https://duckduckgo.com/?q=merge+sort+algorithm&t=h_&ia=web , which from my experience are a lot of links to the wrong way to do things.

So, I looked through them and found

Wikipedia has a nice article on mergesort at https://en.wikipedia.org/wiki/Merge_sort .
I suggest the Top-down implementation.

"A C++ program to Sort an array using merge sort algorithm", it seems OK.
https://www.simplilearn.com/tutorials/data-structure-tutorial/merge-sort-algorithm

There is a nice graphical representation, with pictures and easy to understand explainations at https://www.enjoyalgorithms.com/blog/merge-sort-algorithm .

There is a little more complicated graphical representation at https://www.tutorialspoint.com/data_structures_algorithms/merge_sort_algorithm.htm .

To bring all that together, there is another graphical explaination which might help you that you can find more about at https://www.geeksforgeeks.org/merge-sort/

Quoted from that page:

•    Declare left variable to 0 and right variable to n-1 
•    Find mid by medium formula. mid = (left+right)/2
•    Call merge sort on (left,mid)
•    Call merge sort on (mid+1,rear)
•    Continue till left is less than right
•    Then call merge function to perform merge sort.

Algorithm:

step 1: start
step 2: declare array and left, right, mid variable 
step 3: perform merge function.
        mergesort(array,left,right)
        mergesort (array, left, right)
        if left > right
        return
        mid= (left+right)/2
        mergesort(array, left, mid)
        mergesort(array, mid+1, right)
        merge(array, left, mid, right)
step 4: Stop


Don's be a lurker sucking the life from this site by reading and rarely responding to an offer of a solution. If you are so smart that you would have thought something better, then answer better, don't suck entertainment from this site without giving a response to those posters that attempt to answer. A little encouragement goes a long way. A lot of negativity to a poster is seen as fear of them. Vote up or vote down, and if you vote down, tell technically why you voted down, don't be a sucking lurker. The owners of this site can remove your account the same as they can mine. Engage, don't just wallow in your own high status for actually your status does not belong to you.

Thank you.
 
Share this answer
 
v4
Comments
Richard Deeming 20-Jul-22 3:43am    
Begging for up-votes is strongly frowned upon. It makes it look like you only posted to try to increase your reputation, not to help the person who asked the question.
Member 15078716 20-Jul-22 18:57pm    
Well, lets see how this works: I got a -16 Authority Answer Downvoted for this. I must be seen as a terrible threat to that person. I read fear in that vote. I read unconstrained terror in someone voting a -16 downvote. That happened before when I quoted the rules of this site to someone that was being insulting.

At the level of a 16 +- they should be able to "edit the question" as it says at the bottom this page. But, no, that person seems to have been so afraid that they wet their pants pissing that -16 at me.

I suggest to them that they read the rules and when a newbee to this site makes a mistake then they just comment that it is not proper and then move on.

And, lets get to the point of this: I have read a lot of discussions and have seen that many thousands of other people have read those discussions and answers and etc. and rarely, almost NEVER does ANYONE upvote that they liked the answers or comments. To me those readers might be lurkers, sucking on this web site's years of hard work to read or at least be entertained by this site while very rarely giving anyone else some modicum of credit. I have asked for that a couple of times. I got pissed at for bringing up the need for at least some response. I think that I should maybe post that request more often, especially if it brings to the fore the lurker drain on this site.

Thank you for your opinion.

OK, so Mr. 622,947 points and multiple times MVP, you did not give the question any good solid explained answer, you left the person with nothing, not even a comment, but I, a newbee, tried to answer it in a useful manner. So, you have me thinking that you are so smart, I read your stuff (some of it), and I would like to know technically or semi-technically what you think exactly is correct and/or incorrect about my answer.

Please try to get that answer before I get kicked off of this site for pointing out their lurker drain and after I pointed out their insulter barrier to new questions.

Thank you.

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