http://publications.gbdirect.co.uk/c_book/chapter6/structures.htmlの例6.7を見ています
(便宜上ここに貼り付けました)
struct list_ele *
sortfun( struct list_ele *list )
{
int exchange;
struct list_ele *nextp, *thisp, dummy;
/*
* Algorithm is this:
* Repeatedly scan list.
* If two list items are out of order,
* link them in the other way round.
* Stop if a full pass is made and no
* exchanges are required.
* The whole business is confused by
* working one element behind the
* first one of interest.
* This is because of the simple mechanics of
* linking and unlinking elements.
*/
dummy.pointer = list;
do{
exchange = 0;
thisp = &dummy;
while( (nextp = thisp->pointer)
&& nextp->pointer){
if(nextp->data < nextp->pointer->data){
/* exchange */
exchange = 1;
thisp->pointer = nextp->pointer;
nextp->pointer =
thisp->pointer->pointer;
thisp->pointer->pointer = nextp;
}
thisp = thisp->pointer;
}
}while(exchange);
return(dummy.pointer);
}
基本的な考え方はわかりますが、そこで何が起こっているのか本当に説明できません。誰かがそのソート関数で何が起こっているのかをより深く、しかし簡単な方法で説明できますか?
一般的ないくつかの質問:
- なぜ
dummy.pointer = list;
必要なのですか?dummy
次に、関数の最後にが返されますが、なぜリストはまだソートされているのですか? - コメント
The whole business is confused by working one element behind the first one of interest.
はどういう意味ですか?