1.桶排序
#include<iostream>
using namespace std;
int main()
{
int num;cin>>num;
int a[num];
for (int i = 1; i < sizeof(a) / sizeof(a[0]); ++i)
{
//scanf("%d", &a[i]);
cin>>a[i];
}for (int i =1; i < sizeof(a) / sizeof(a[0]); ++i)
{
for (int j = 1; j <=a[i]; ++j)
{
//printf("%d", i);
cout << i << "\t";
}
}
cout << endl;
system("pause");
return 0;
}
2.冒泡排序
#include<iostream>
using namespace std;
int main()
{
int num;
cin >> num;
int *array = new int[num];
for (int i = 0; i < num; ++i)
{
cin >> array[i];
}
for (int i = 0; i < num-1; ++i)
{
for (int j = 0; j < num-i-1; ++j)
{
if (array[j] < array[j+1])
{
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for (int k = 0; k < num; ++k)
cout << array[k] << "\t";
cout << endl;
system("pause");
delete[] array;
return 0;
}
3. 快速排序
#include<iostream>
using namespace std;
int a[10];void quicksort(int left, int right)
{
if (left >= right)
return;
int i, j;
i = left;
j = right;
int temp = a[left];
while (i < j)
{
while (a[j] >= temp && j != i)
j--;
while (a[i] <= temp && i != j)
i++;int temp1 = a[i];
a[i] = a[j];
a[j] = temp1;
}
int temp2 = a[i];
a[i] = a[left];
a[left] = temp2;
quicksort(left, i - 1);
quicksort(i + 1, right);
}
int main()
{
for (int i = 0; i < 10; ++i)
{
scanf("%d", &a[i]);
}
quicksort(0, 9);
for (int i = 0; i < 10; ++i)
cout << a[i] << "\t";
cout << endl;
system("pause");
return 0;
}