﻿ C语言基本排序算法之插入排序与直接选择排序实现方法 - 鸿网互联

C语言基本排序算法之插入排序与直接选择排序实现方法

```/*--------------------------
typedef.h

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

```

```/*---------------------------------------------------------------------------------------
Insertion_sort.h

------------------------------------------------------------------------------------------------*/
#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

--------------------------------------------------------*/
#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

```

<