//冒泡排序
void BubbleSort(int *a, int n)
{
int* p = a;
for (int i=0; i<n-1; i++)
{
for (int j=0; j<n-i-1; j++)
{
int temp;
if (p[j]<p[j+1])
{
temp = p[j+1];
p[j+1] = p[j];
p[j] = temp;
}
}
}
}
//直接插入排序(洗牌算法)
void InsertSort(int* a, int n)
{
int temp;
for (int i=1; i<n; i++)
{
temp = a[i];
for (int j = i-1; j>=0; j--)
{
if (temp < a[j])
{
a[j+1] = a[j];
if (j == 0)
{
a[0] = temp;
}
}
else
{
a[j+1] = temp;
break;
}
}
}
}
//简单选择排序
void selectSort(int* a, int n)
{
int* p_min =NULL;
int temp;
for (int i=0; i<n-1; i++)
{
temp = a[i];
for (int j=i+1; j<n; j++)
{
if (temp>a[j])
{
temp = a[j];
p_min = &a[j];
}
}
*p_min = a[i];
a[i] = temp;
}
}
void testSort()
{
int a[]={4,2,5,6,3,0,1,9,7,8,10};
//BubbleSort(a,10);
//InsertSort(a,10);
selectSort(a,10);
for (int i = 0; i < 10; i++)
{
cout<<a[i]<<endl;
}
}