插入排序
void insertion_sort(char array[], unsigned int first, unsigned int last)
{
int i, j, temp;
for (i = first + 1; i < last; i++)
{
temp = array[i];
j = i - 1;
while ((j >= first) && (array[j] > temp))
{
array[j + 1] = array[j];
j--;
}
array[j + 1] = temp;
}
}
快速排序
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
void quicksort(int arr[], int begin, int end)
{
if (end > begin)
{
int pivot = arr[begin];
int beginIter = begin + 1;
int endIter = end + 1;
// 找到大于pivot的元素,扔到尾部
while ( beginIter < endIter)
{
if (arr[beginIter] < piv)
{
beginIter++;
}
else
{
swap(&arr[beginIter], &arr[endIter]);
endIter--;
}
}
// 将pivot节点放到分界处
if (arr[beginIter] < pivot)
{
swap(&arr[beginIter], &arr[begin]);
}
else if (end - begin == 1)
{
return;
}
else
{
beginIter--;
swap(&arr[beginIter], &arr[begin]);
}
//对两个分区分别迭代
quicksort(arr, begin, beginIter);
quicksort(arr, endIter, end);
}
}