Sorting a singly linked list with quick sort using a random pivot.

This is a discussion on Sorting a singly linked list with quick sort using a random pivot. within the C Programming forums, part of the General Programming Boards category; I am search high and low for an answer to this what I have thus far works fine using the ...

Sorting a singly linked list with quick sort using a random pivot.

I am search high and low for an answer to this what I have thus far works fine using the first element as the pivot element but of course that is not very random..... Any help with this would be so very greatly appreciated.

The problem isn't whether the pivot is the first node in your list, it has to do with the value of the pivot node. It just appears to work when the pivot is the first node because the value of the first node is zero, and nothing is smaller that it, thus no less list. Your actual problem is with your separation and merging. The problems I noticed are:

You need to initialize temp to list before your Divide & Conq loop, not list->next.

You need to make sure to not include the pivot node in your less/more list when you Divide & Conq. Check if (temp == pivot) before adding to less/more.

1) Yes I need to do that.
2) What if I have more than 1 value that equals the temp?
3) I was lost with this, should I remove a value that equals the pivot from the list so the list is 1 value less, then proceed with the D&Q?
4) This should be obvious with 3
5) Again this follows from previous

> 2) What if I have more than 1 value that equals the temp?
Good question. Do the specs require you to handle duplicates values? The check I suggested checks the address of the pivot, not the value, so you only skip the actual pivot, regardless of whether there are other nodes with the same value. The way you have it coded (the if/else in the D&C loop) will put the duplicate value nodes in the more list, but it should still sort/merge fine.

Yes, you get jumbled numbers with your current code, but it's not random, or even pseudo-random. Your createList function will always produce the same numbers since there's no call to rand() in there. If you notice when you print the list after sorting (when it works), the numbers are all double the index. That is, the sequence is 0, 2, 4, 6, 8, 10, ... in decimal (for some reason you print your list in octal). That is also what the the if (list->value != 2*i) check is for in Brass_test. It would be a good idea to test this with other sets of random data also, so you should have a way to create such a list. You need to call srand() once at the beginning of main. That will make the pseudo-random sequence of numbers generated by rand() different each time you run your program.

Somewhat jumbled is not exactly random. The point is that if you test it with such a systematic series of numbers, and only with that series, it may just happen to work for them even though the algorithm is wrong. I assume that's what happened when it seemed to work with always picking the first number.

The check I suggested checks the address of the pivot, not the value, so you only skip the actual pivot, regardless of whether there are other nodes with the same value.

Right. I see. Well that's okay then! (I'm not sure what I was thinking.)