C言語基本並べ替えアルゴリズムの挿入順序と直接選択並べ替えの実施方法


本明細書の例は、C言語の基本的な順序付けアルゴリズムの挿入順序付けと直接的な選択順序付けの実施方法を説明する。皆さんに参考にしてあげます。具体的には以下の通りです。
ソート対象の要素の種類を宣言します。

/*--------------------------
typedef.h
           
-------------------------------------*/
#ifndef TYPEDEF_H
#define TYPEDEF_H
typedef int T;
#endif

並べ替えの挿入:

/*---------------------------------------------------------------------------------------
Insertion_sort.h
      
             
      (   )N(N-1)/4 = O(N^2),             
       N ,       2+3+…+N
------------------------------------------------------------------------------------------------*/
#ifndef INSERTION_SORT_H
#define INSERTION_SORT_H
#include "typedef.h"
//       
void Insertion_sort(T *a, int n)
{
  for(int i = 1; i != n; ++i)
  {
    T temp = a[i];
    int j = i - 1;
    for(; j >= 0 && temp < a[j]; --j )
      a[j + 1] = a[j];
    a[j + 1] = temp;
  }
}
#endif

並べ替えを直接選択:

/*----------------------------------------------
DirectSelection_sort.h
      
     O(N^2)
--------------------------------------------------------*/
#ifndef DIRECTSELECTION_SORT_H
#define DIRECTSELECTION_SORT_H
#include "typedef.h"
#include "swap.h"
//       
void DirectSelection_sort(T*a, int n)
{
  for(int i = 0; i != n; ++i)
  {
    int k = i;
    for(int j = i; j != n; ++j)
      if(a[j] < a[k]) k = j;
    swap(a[k],a[i]);
  }
}
#endif

ここで述べたように、皆さんのC言語プログラムの設計に役に立ちます。