C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# Linked List Sorting with Strings In C

By : SpiderXC
Date : November 21 2020, 07:01 PM
wish helps you When you enter AAA, BBB and then CCC namebox is always NULL when while loop finishes.
And then you are doing:
code :

Share :

## Sorting linked list by sorting the actual nodes and not just by swapping node values

By : Bart93
Date : March 29 2020, 07:55 AM
wish of those help This is the traditional and fastest way to merge sort a linked list. It's a bottom up style merge sort that uses an array of pointers to nodes to hold intermediate lists, where array[i] is either null or points to a list with 2 to the power i nodes. Nodes from the original list are merged into the array, then the lists in the array are merged to form a single sorted list. HP / Microsoft C++ STL std::list::sort uses the same algorithm.
code :
``````/* prototype */
NODE * MergeLists(NODE *pSrc1, NODE *pSrc2);

/* sort list using array of pointers to first nodes of list   */
/* aList[i] = NULL or ptr to list with 2 to the power i nodes */

#define NUMLISTS 32                 /* size of array */
NODE * SortList(NODE *pList)
{
NODE * aList[NUMLISTS];             /* array of lists */
NODE * pNode;
NODE * pNext;
int i;
if(pList == NULL)               /* check for empty list */
return NULL;
for(i = 0; i < NUMLISTS; i++)   /* zero array */
aList[i] = NULL;
pNode = pList;                  /* merge nodes into array */
while(pNode != NULL){
pNext = pNode->next;
pNode->next = NULL;
for(i = 0; (i < NUMLISTS) && (aList[i] != NULL); i++){
pNode = MergeLists(aList[i], pNode);
aList[i] = NULL;
}
if(i == NUMLISTS)           /* don't go past end of array */
i--;
aList[i] = pNode;
pNode = pNext;
}
pNode = NULL;                   /* merge array into one list */
for(i = 0; i < NUMLISTS; i++)
pNode = MergeLists(aList[i], pNode);
return pNode;
}

/* mergelists -  compare uses src2 < src1           */
/* instead of src1 <= src2 to be similar to C++ STL */

NODE * MergeLists(NODE *pSrc1, NODE *pSrc2)
{
NODE *pDst = NULL;                  /* destination head ptr */
NODE **ppDst = &pDst;               /* ptr to head or prev->next */
if(pSrc1 == NULL)
return pSrc2;
if(pSrc2 == NULL)
return pSrc1;
while(1){
if(pSrc2->data < pSrc1->data){  /* if src2 < src1 */
*ppDst = pSrc2;
pSrc2 = *(ppDst = &(pSrc2->next));
if(pSrc2 == NULL){
*ppDst = pSrc1;
break;
}
} else {                        /* src1 <= src2 */
*ppDst = pSrc1;
pSrc1 = *(ppDst = &(pSrc1->next));
if(pSrc1 == NULL){
*ppDst = pSrc2;
break;
}
}
}
return pDst;
}
``````

## Runtime error sorting a linked list of strings

By : art.sanderson
Date : March 29 2020, 07:55 AM
hop of those help? char *str; ... strcpy(str,x->name); copies data pointed to by name to an someplace (it is undefined behavior) as str is an uninitialized pointer.
Just swap pointers.
code :
``````       if (strcmp(x->name,y->name) > 0) {
char *temp = x->name;
x->name = y->name;
y->name = temp;
}
``````

## sorting a linked list of strings by first letter

By : Joseph M. Durnal
Date : March 29 2020, 07:55 AM
I wish did fix the issue. This solution would require three O(N) traversings of the list, so it will still be O(N). The problem with it is that it creates a new list, and the requirements seem to imply inplace sorting.
An inplace approach could be to go over the list, and move any items starting with A to the beginning and any items starting with C to the end. E.g.:
code :
``````for item in list:
if item.data[0] == 'A'
item.remove()
list.prepend(item.data)
elif item.data[0] == 'C'
item.remove()
list.append(item.data)
``````

## Sorting strings in a linked list

By : Sephirothphoenix
Date : March 29 2020, 07:55 AM
Hope this helps The function works quite ok but the problem comes in when I insert the same surname more than once. Please help, it's my school project. , You have a simple bug in the swapping part of your code:

## Is sorting a Linked List using Quicksort really slower than Mergesort because of no random access in Linked List?

By : user5588445
Date : March 29 2020, 07:55 AM
To fix the issue you can do I'm updating this answer to provide a better comparison. In my original answer below, I include an example of bottom up merge sort, using a small array of pointers to lists. The merge function merges two lists into a destination list. As an alternative, the merge function could merge one list into the other via splice operations, which would mean only updating links about half the time for pseudo random data. For arrays, merge sort does more moves but fewer compares than quicksort, but if the linked list merge is merging one list into the other, the number of "moves" is cut in half.
For quicksort, the first node could be used as a pivot, and only nodes less than pivot would be moved, forming a list prior to the pivot (in reverse order), which would also mean only updating links about half of the time for pseudo random data.