0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

基本選択法によるソートプログラム(C言語)

Last updated at Posted at 2019-10-25

C言語を用いた基本選択法によるソートプログラムです。

# include <stdio.h>

	
	int main(void) {

		int i;			//最小値判定ループ用
		int j;			//仮の最小値設定ループに使うカウント用変数
		int min;		//最小値の添え字用
		int k = 5;		//配列の要素数
		int temp;		//最小値の一時保管用


		int x[] = {2,5,3,4,1};
		printf("並び替え前\n");
		
		for(i = 0; i < 5; i++) {
			printf("x[%d] = %d\n",i, x[i]);
		}
		
		//ここまではソート前の出力処理

		for(i = 0; i < k-1; i++) {
			min = i;
			for(j= i+1 ; j < k; j++) {
				if(x[min] > x[j]) {
					min = j;
				}
			}
			temp = x[min];
			x[min] = x[i];
			x[i] = temp;			
	 	}

		//ソート処理はここで終わり

		printf("並び替え後\n");

			for(i = 0; i < 5; i++) {				

			          printf("x[%d] = %d\n",i, x[i]);
		    }

		return 0;

}

イメージとしては、
最小値を探すソートに入った時、
最初に設定した最小値より小さい値がきたら、
minさんにその最小値が何番目にあったか覚えておいてよ、
って感じです。
そんで、ソートが終わったら、
「minさん、最小値より小さい数値は何番目だっけ?」
と聞いて、
「へい、〇番目でっせ。」
となって、
「じゃあ、最小値と〇番目の数値をチェーンジ!」
という動作イメージです。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?