Skip to content

Instantly share code, notes, and snippets.

@niyaton
Created January 29, 2014 21:03
Show Gist options
  • Save niyaton/8697151 to your computer and use it in GitHub Desktop.
Save niyaton/8697151 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int main(){
int n;
printf("配列のサイズを入力してください");
scanf("%d", &n);
int array[n];
int i;
for(i = 0; i < n; ++i){
printf("%d番目の要素を入力してください", i);
scanf("%d", &array[i]);
}
int j;
for(i = 0; i < n; ++i){
// 手順1:array[i]からarray[n]までで最小値を検索する
// 手順2:array[i]と最小値を交換する
// ここから最小値の検索
int j;
int min_index = i; // 最小値ではなく,最小値の添え字を記憶する
for(j = i; j < n; ++j){
if(array[j] < array[min_index]){
min_index = j;
}
}
// ここから最小値と先頭の交換
int tmp = array[i];
array[i] = array[min_index];
array[min_index] = tmp;
}
printf("ソート完了\n");
for(i = 0; i < n; ++i){
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment