1 / 7

情報演習 ~ソートアルゴリズム1~

情報演習 ~ソートアルゴリズム1~. 2011 年 1 月 17 日 笠井俊信. 新しいデータ型. 今までのデータ型 int a; ・・・ 整数型の変数 1 つを宣言 今日の新しいデータ型 int a[ 5 ]; ・・・整数型の変数 5 個の 配列 を宣言 int a[] = {1,2,3,4,5}; 変数の宣言と初期化 配列の参照の仕方() 1 番目のデータ → a[0] 最後のデータ → a[4]. 演習問題2(復習). #include <stdio.h> void main(void) {

sal
Download Presentation

情報演習 ~ソートアルゴリズム1~

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 情報演習~ソートアルゴリズム1~ 2011年1月17日 笠井俊信

  2. 新しいデータ型 • 今までのデータ型 • int a; ・・・ 整数型の変数1つを宣言 • 今日の新しいデータ型 • int a[5]; ・・・整数型の変数5個の配列を宣言 • int a[] = {1,2,3,4,5}; 変数の宣言と初期化 • 配列の参照の仕方() • 1番目のデータ → a[0] • 最後のデータ → a[4]

  3. 演習問題2(復習) #include <stdio.h> void main(void) { int dat[10], sum, i; sum=0; for (i=0;i<10;i++){ scanf("%d", &dat[i]); sum = sum + dat[i]; } printf("合計 = %d\n", sum); printf("平均 = %d\n", sum/i); } • カウンタと変数SUMの値を0に設定 • (繰り返し)カウンタの値が10未満である限り • 数値を入力する • 入力された数値をSUMに加算 • カウンタの値を1だけ増やす • 合計値(SUM)を出力 • 平均値(SUM/10)を出力

  4. 1.基本選択法 • 基本選択法とは? • 数列の中から最大の数値を探すことを繰り返し,大きい数字の位置から確定していく 5 3 9 6 9 3 5 6 96 5 3

  5. ソートのステップ j p i 6 2 5 3 1 7 4 0 1 0 6 2 5 3 1 7 4 0 7 5 7 2 5 3 1 6 4 1 7 5 7 6 5 3 1 2 4 3 7 6 7 6 5 4 1 2 3 4 7 6 7 6 5 4 3 2 1 i ・・・ 何番目に大きい数か j ・・・ どこまで数を調べたか p ・・・ 1番大きい数 int suu[]={6,2,5,3,1,7,4};

  6. 1.基本選択法のアルゴリズム(7個の数列のソート)1.基本選択法のアルゴリズム(7個の数列のソート) temp = suu[i]; suu[i] = suu[p]; suu[p] = temp; • カウンタ1iに0を代入 • iが6より小さい限り以下を繰り返す • 最大値へのポインタpにiを代入 • カウンタ2jにi+1を代入 • jが7より小さい限り以下を繰り返す • p番目の数字とj番目の数字を比較して,j番目の数字の方が大きければpにjを代入 • jを1増やす • i番目の数字とp番目の数字を入れ替える • iを1増やす i ・・・ 何番目に大きい数か j ・・・ どこまで数を調べたか p ・・・ 1番大きい数 int suu[]={6,2,5,3,1,7,4};

  7. for(i=0;i<7;i++){ printf(“%d “,suu[i]); } } #include<stdio.h> void main(void) { int suu[]={6,2,5,3,1,7,4}; int i, j, p,tmp; for(i=0;i<6;i++){ p=i; for(j=i+1;j<7;j++){ if(suu[j]>suu[p]){ p=j; } } tmp=suu[i]; suu[i]=suu[p]; suu[p]=tmp; } i ・・・ 何番目に大きい数か j ・・・ どこまで数を調べたか p ・・・ 1番大きい数

More Related