I am wondering how to implement a bubble sort on a singly-linked list. Let's say for example that we have list that consists of following nodes:
struct node {
int value;
struct node* next;
}
I believe that there are 2 ways to acomplish this:
1)to directly exchange `values` in memory
2)to change `nexts`, to point to a different nodes
Which way is more efficient,开发者_C百科 and can somebody give me some example on how to do it? I'm aware that using Bubble Sort isn't very efficient compared to other sorting algorithms.
Your values are very small, so I expect exchanging them to be more efficient than changing the pointer structure. As always, you will have to measure the actual performance in your use case to be sure.
You have to consider various special cases if you exchange the nexts
. This makes it a little bit slower than changing value
s. If your values have more structure you have either the option to implement the next
-version or replace value by a pointer.
精彩评论