利用 randarr 函数生成65535个 0 ~ RAND_MAX 的随机数进行从大到小排序。设待排序的数组长度为 len (实例函数中的 arrmaxn )
void bubblesort(void)
{
for (int i = 0; i != arrmaxn - 1; ++i)
for (int j = 0; j != arrmaxn - i - 1; ++j)
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
return;
}
void selectionsort(void)
{
for (int i = 0; i != arrmaxn - 1; ++i)
{
auto tempindex = i;
for (int j = i + 1; j != arrmaxn; ++j)
if (arr[j] < arr[tempindex])
tempindex = j;
swap(arr[i], arr[tempindex]);
}
return;
}
void insertionsort(void)
{
for (int i = 1; i != arrmaxn; ++i)
{
auto tempindex = i - 1, tempvalue = arr[i];
while (tempindex >= 0 && arr[tempindex] > tempvalue)
{
arr[tempindex + 1] = arr[tempindex];
--tempindex;
}
arr[tempindex + 1] = tempvalue;
}
return;
}
void shellsort(void)
{
auto gap = arrmaxn >> 1;
while (gap)
{
for (int i = gap; i != arrmaxn; ++i)
{
auto tempindex = i - gap, tempvalue = arr[i];
while (tempindex >= 0 && arr[tempindex] > tempvalue)
{
arr[tempindex + gap] = arr[tempindex];
tempindex -= gap;
}
arr[tempindex + gap] = tempvalue;
}
gap >>= 1;
}
return;
}
void mergesort(int l = 0, int r = arrmaxn - 1)
{
if (l < r)
{
auto mid = (l + r) >> 1;
mergesort(l, mid);
mergesort(mid + 1, r);
int templ = l, tempr = mid + 1, spindex = 0;
while (templ <= mid && tempr <= r)
backuparr[spindex++] = (arr[templ] < arr[tempr]) ? (arr[templ++]) : (arr[tempr++]);
while (templ <= mid)
backuparr[spindex++] = arr[templ++];
while (tempr <= r)
backuparr[spindex++] = arr[tempr++];
for (int i = 0; i != r - l + 1; ++i)
arr[l + i] = backuparr[i];
}
return;
}
void quicksort(int l = 0, int r = arrmaxn - 1)
{
if (l >= r)
return;
int temp = arr[l], templ = l, tempr = r;
while (templ != tempr)
{
while (templ != tempr && arr[tempr] >= temp)
--tempr;
arr[templ] = arr[tempr];
while (templ != tempr && arr[templ] <= temp)
++templ;
arr[tempr] = arr[templ];
}
arr[templ] = temp;
quicksort(l, templ - 1);
quicksort(tempr + 1, r);
return;
}
void heapsort(void)
{
for (int i = arrmaxn >> 1; i; --i)
{
int copyi = i;
while (1)
{
int maxindex = copyi;
if (copyi * 2 <= arrmaxn && arr[copyi * 2 - 1] > arr[maxindex - 1])
maxindex = copyi * 2;
if (copyi * 2 + 1 <= arrmaxn && arr[copyi * 2] > arr[maxindex - 1])
maxindex = copyi * 2 + 1;
if (maxindex != copyi)
{
swap(arr[copyi - 1], arr[maxindex - 1]);
copyi = maxindex;
}
else
break;
}
}
int lens = arrmaxn;
while (lens)
{
swap(arr[0], arr[lens - 1]);
--lens;
int copyi = 1;
while (1)
{
int maxindex = copyi;
if (copyi * 2 <= lens && arr[copyi * 2 - 1] > arr[maxindex - 1])
maxindex = copyi * 2;
if (copyi * 2 + 1 <= lens && arr[copyi * 2] > arr[maxindex - 1])
maxindex = copyi * 2 + 1;
if (maxindex != copyi)
{
swap(arr[copyi - 1], arr[maxindex - 1]);
copyi = maxindex;
}
else
break;
}
}
return;
}
void countingsort(void)
{
for (const auto i : arr)
++bucket[i];
int spindex = 0;
for (int i = 0; i <= RAND_MAX; ++i)
while (bucket[i])
arr[spindex++] = i, --bucket[i];
return;
}
sort(begin(arr), end(arr));
stable_sort(begin(arr), end(arr));
make_heap(begin(arr), end(arr));
sort_heap(begin(arr), end(arr));
is_sorted(begin(arr), end(arr));
bubblesort is sorted. 19008 ms
selectionsort is sorted. 4760 ms
insertionsort is sorted. 2882 ms
shellsort is sorted. 17 ms
mergesort is sorted. 12 ms
quicksort is sorted. 8 ms
heapsort is sorted. 20 ms
heapsortSTL is sorted. 17 ms
sortSTL is sorted. 12 ms
stablesortSTL is sorted. 13 ms
countingsort is sorted. 1 ms
复习于2019-05-18