#include <stdio.h>
#include <stdlib.h>
#include <Windows.h> //直接插入排序
void InsertSort(int arry[], int n)
{
int i, j;
int temp;//临时变量
for (i = ; i < n; i++)
{
temp = arry[i];
for (j = i - ; j >= ; j--)
{
if (temp > arry[j])
break;
else
arry[j + ] = arry[j];
}
arry[j+] = temp;
}
} //直接选择排序
void SelectSort(int arry[], int n)
{
int i, j;
int temp;
for (i = ; i < n-; i++)
{
temp = i;
for (j = i + ; j < n; j++)
{
if (arry[j] < arry[temp])
temp = j;
}
if(temp!=i)
arry[temp] ^= arry[i] ^= arry[temp] ^= arry[i];//实现arry[temp]和arry[i]值交换
}
} //冒泡排序
void BubbleSort(int arry[], int n)
{
int i, j, k;
for (i = ; i < n-; i++)
{
for (j = i; j < n; j++)
{
if (arry[j - ]>arry[j])
arry[j - ] ^= arry[j] ^= arry[j - ] ^= arry[j];//实现arry[temp]和arry[i]值交换
}
}
} //希尔排序第一类,
void ShellSort1(int arry[], int n)
{
int i, j, k, gap,temp;
for (gap = n / ; gap > ; gap /= )
{
for (k = ; k < gap; k++)//同组先排
{
for (i = k+gap; i < n; i += gap)
{
temp = arry[i];
for (j = i - gap; j >= ; j -= gap)
{
if (temp > arry[j])
break;
else
arry[j + gap] = arry[j];
}
arry[j + gap] = temp;
}
}
}
} //希尔排序第二类
void ShellSort2(int arry[], int n)
{
int i, j, k, gap, temp;
for (gap = n / ; gap > ; gap /= )
{
for (i = gap; i < n; i++)//所有组一起排序
{
temp = arry[i];
for (j = i - gap; j >= ; j -= gap)
{
if (temp > arry[j])
break;
else
arry[j + gap] = arry[j];
}
arry[j + gap] = temp;
}
}
} //希尔排序,利用gap分组后采用冒泡方式排序
void ShellSort3(int arry[], int n)
{
int i, j, k, gap, temp;
for (gap = n / ; gap > ; gap /= )
{
for (k = ; k < gap; k++)
{
for (i = k; i < n-gap; i += gap)
{
for (j = k+gap; j <n ; j += gap)
{
if (arry[j - gap]>arry[j])
arry[j - gap] ^= arry[j] ^= arry[j - gap] ^= arry[j];
} }
}
}
} //快速排序,递归调用
void QuickSort(int arry[], int s,int t)
{
int i, j; if (s < t)
{
i = s;
j = t+;
while ()
{
do
{
i++;
}while (!(arry[s] <= arry[i] || i >= t));
do
{
j--;
} while (!(arry[s] >= arry[j] || j <= s)); if (i < j)
arry[i] ^= arry[j] ^= arry[i] ^= arry[j]; else
break;
}
if (arry[j]!= arry[s])
arry[j] ^= arry[s] ^= arry[j] ^= arry[s];
QuickSort(arry, s, j-);
QuickSort(arry, j + , t);
}
} int main()
{
int arry1[] = { ,,,,,,,,,,,,,,};
int arry2[] = { ,,,,,,,,,,,,,, };
int arry3[] = { ,,,,,,,,,,,,,, };
int arry4[] = { ,,,,,,,,,,,,,, };
int arry5[] = { ,,,,,,,,,,,,,, };
int arry6[] = { ,,,,,,,,,,,,,, };
int arry7[] = { ,,,,,,,,,,,,,, }; //七种方法任选一种,但效率有别
//一般情况下:快速排序>直接插入>直接选择==冒泡
//希尔排序看情况而定。一般优于直接选择排序 //InsertSort(arry1, 15); //SelectSort(arry2, 15); //BubbleSort(arry3, 15); //ShellSort1(arry4, 15);
//ShellSort2(arry5, 15);
//ShellSort2(arry6, 15); //QuickSort(arry7,0,14); int i = ;
for (i = ; i < ; i++)
{
printf("%d ", arry7[i]);
} system("pause");
}