void Swap(int*a, int* b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void BubbleSort(intarray[], int size)
{
if(size <= 1)
{
return;
}
int bound = 0;
for(; bound < size; bound++)
{
int cur = size - 1;
for(; cur >= 0; cur--)
{
if(array[cur] < array[cur - 1])
{
Swap(&array[cur], &array[cur - 1]);
}
}
}//end for(; bound < size; bound++)
}
选择排序
void SelectSort(intarray[], int size)
{
if(size <= 1)
{
return;
}
int bound = 0;
for(; bound < size; bound++)
{
int cur = bound + 1;
for(; cur < size; cur++)
{
if(array[bound] > array[cur])
{
Swap(&array[bound] , &array[cur]);
}
}
}//end for(; bound < size; bound++)
}
插入排序
void InsertSort(intarray[], int size)
{
if(size <= 1)
{
return;//zhi you yi ge yuan su huo zhe fei fa shu ru
}
//ding yi yi ge bian jie zhi bound//qi zhong [0, bound)biao shi you xu qu jian//[bound, cur)dai biao wu xu qu jianint bound = 1;
for(; bound < size; bound++)
{
//ding yi yi ge bound_value bao cun array[bound] de zhi, bian yu ban yunint bound_value = array[bound];
//ding yi yi ge cur bang zhu wo men ban yunint cur = bound;
//cur xiang qian sao miaofor(; cur > 0; cur--)
{
//jiang shu zhu cur-1 dui ying de yuan su he cur bound_value bi jiao, ru guo da yu, //jiu jin xin ban yunif(array[cur - 1] > bound_value)
{
array[cur] = array[cur - 1];
}
//shuo ming yi jing shi he shi wei zhi, jiu bu xu yao ban yunelse
{
break;
}
}
array[cur] = bound_value;
}//end for(; bound < size; bound++)
}
堆排序
void AdjustDown(intarray[], int size, int index)
{
int parent = index;
int child = 2 * parent + 1;
while(child < size)
{
if(child + 1 < size && array[child + 1] > array[child])
{
child = child + 1;
}
if(array[child] > array[parent])
{
Swap(&array[child], &array[parent]);
}
elseif(array[child] < array[parent])
{
break;
}
parent = child;
child = 2 * parent + 1;
}
}
void HeapCreat(intarray[], int size)
{
int i = (size - 1 - 1) / 2;
for(; i >= 0; i--)
{
AdjustDown(array, size, i);
}
}
void HeapPop(intarray[], int index)
{
Swap(&array[0], &array[index]) ;
AdjustDown(array, index, 0);
}
void HeapSort(intarray[], int size)
{
if(size <= 1)
{
return;
}
HeapCreat(array, size);
// int end = size - 1;// for(; end >= 0; --end)// {// Swap(&array[0], &array[end]);// AdjustDown(array, end, 0);// }int i = 1;
for (; i < size; i++)
{
HeapPop(array, size - i);
}
}