copy and destroy

catch and eat

日記の練習です。

『コンピュータの構成と設計』、C 言語のソートのコード(配列に収められている数値を昇順にソートする)をアセンブルする例題が出て来て、ソートのアルゴリズム、全然知らないから(アセンブラじゃなくて C 言語の方で)ハマっている。

sort 手続き

sort(int v[], int n)
{
	int i, j;
	for(i = 0; i < n; i = i + 1){
		for(j = i - 1; j >= 0 && v[j] > v[j + 1]; j = j - 1){
			swap(v, j);
		}
	}
}

swap 手続き

swap(int v[], int k)
{
	int temp;
	temp = v[k];
	v[k] = v[k +1 ];
	v[k + 1] = temp;
}


一晩たってみて、これはたぶん「挿入ソート」*1*2だと思うんだけど…

レジスタやスタックをどう使うのか、 そういう MIPS プロセッサの気持ちはわかるのに C 言語がわからない。アンバランス。

*1:挿入ソート - Wikipedia https://ja.wikipedia.org/wiki/%E6%8C%BF%E5%85%A5%E3%82%BD%E3%83%BC%E3%83%88

*2:「挿入ソートはリストの整列済みの部分に対して新たな要素を適切な位置に挿入することで整列を行うアルゴリズムです / 挿入ソート : アルゴリズム https://www.codereading.com/algo_and_ds/algo/insertion_sort.html

powered by hatena blog.
the nikki system for lifelogging junkies.

all posts © their original owners.
writing is reusable solely under the by creative commons license.