1、冒泡排序
void Bubble_Sort(int *array, int len)
{
int temp, i, j;
for (i = 0; i < len - 1; i++)
{
for (j = 0; j < len - 1; j++)
{
if (array[j]>array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
2、桶排
void Counting_Sort(int *array, int len)
{
int *temparr = new int[100+9];
memset(temparr, 0, sizeof(int)*(109));
for (int i = 0; i < len; i++)
{
temparr[array[i]]++;
}
int k = 0;
for (int i = 0; i < 109; i++)
{
while (temparr[i])
{
array[k++] = i;
temparr[i]--;
}
}
delete[]temparr;
}
3、插入排序
void Insertion_Sort(int *array, int len)
{
int temp, i, j;
for (i = 1; i < len; i++)
{
temp = array[i];
j = i - 1;
while (j >= 0 && array[j]>temp)
{
array[j + 1] = array[j];
j--;
}
array[j + 1] = temp;
}
}
4、归并排序
void merge(int arr[], int l, int mid, int r)
{
int len = r - l + 1;
int *aux=new int[len];//开辟一个新的数组,将原数组映射进去
for (int m = l; m <= r; m++)
{
aux[m - l] = arr[m];
}
int i = l, j = mid + 1;//i和j分别指向两个子数组开头部分
for (int k = l; k <= r; k++)
{
if (i>mid)
{
arr[k] = aux[j - l];
j++;
}
else if (j>r)
{
arr[k] = aux[i - l];
i++;
}
else if (aux[i - l]<aux[j - l])
{
arr[k] = aux[i - l];
i++;
}
else
{
arr[k] = aux[j - l];
j++;
}
}
delete[]aux;
}
//递归的使用归并排序,对arr[l....r]排序
void merge_sort(int arr[], int l, int r)
{
if (l >= r)
return;
int mid = (l + r) / 2;
merge_sort(arr, l, mid);
merge_sort(arr, mid + 1, r);
merge(arr, l, mid, r);
}
void my_merge_sort(int arr[], int n)
{
merge_sort(arr, 0, n - 1);
}
5、基数排序
int Max(int *array, int len)
{
int ans = 0;
int max = -1;
for (int i = 0; i < len; i++)
{
ans = 0; int temp = array[i];
while (temp)
{
ans++;
temp /= 10;
}
max=ans>max ? ans : max;
}
return max;
}
void Radix_Sort(int *array, int len)
{
//cout << Max(array, len) << endl;
int times = Max(array, len);
int mod = 1;
int *count = new int[10];
int **temp = new int*[10];
for (int i = 0; i < 10; i++)
{
temp[i] = new int[len];
}
for (int i = 0; i < times; i++)
{
memset(count,0,sizeof(int)*10);
for (int j = 0; j < 10;j++)
memset(temp[j], 0, sizeof(int)* len);
for (int j = 0; j < len; j++)
{
int div = array[j] / mod;
int Mod = div % 10;
count[Mod]++;
temp[Mod][count[Mod] - 1] = array[j];
}
int k = 0;
for (int j = 0; j < 10; j++)
{
int th = 0;
while (count[j])
{
array[k++] = temp[j][th++];
count[j]--;
}
}
mod *= 10;
}
delete[]count;
count = NULL;
for (int i = 0; i < 10; i++)
{
delete[] temp[i];
}
delete[]temp;
return;
}
6、选择排序
void Selection_Sort(int *array, int len)
{
int temp;
int i, j, k;
for (i = 0; i < len - 1; i++)
{
k = i;
for (j = i + 1; j < len; j++)
{
if (array[j] < array[k])
{
k = j;
}
}
temp = array[i];
array[i] = array[k];
array[k] = temp;
}
}
7、希尔排序
void Shell_Sort(int *array, int len)
{
int temp;
int i, j, jump;
jump = len >> 1;
while (jump)
{
for (i = jump; i < len; i++)
{
temp = array[i];
j = i - jump;
while (j >= 0 && array[j]>temp)
{
array[j + jump] = array[j];
j -= jump;
}
array[j+jump] = temp;
}
jump = jump >> 1;
}
}
8、快速排序
void quick_sort(int s[], int l, int r)
{
if (l < r)
{
int i = l, j = r, x = s[l];
while (i < j)
{
while(i < j && s[j] >= x) // 从右向左找第一个小于x的数
j--;
if(i < j)
s[i++] = s[j];
while(i < j && s[i] < x) // 从左向右找第一个大于等于x的数
i++;
if(i < j)
s[j--] = s[i];
}
s[i] = x;
quick_sort(s, l, i - 1); // 递归调用
quick_sort(s, i + 1, r);
}
}