#include <stdlib.h>
//選擇排序
void SelectSort(int *p, const int length)
{
if (p == NULL)
{
return;
}
for (int i = 0; i<length; i++)
{
int k = i; //記錄一輪找到最小值的下標
for (int j = i+1; j<length; j++)
{
if (p[k] > p[j])
{
k = j;
}
}
int temp = p[k];
p[k] = p[i];
p[i] = temp;
}
}
//冒泡排序
void BubbleSort(int *p, const int length)
{
if (p == NULL)
{
return;
}
for (int i = 0; i<length; i++)
{
for (int j = i+1; j<length; j++)
{
if (p[i] > p[j])
{
int temp = p[j];
p[j] = p[i];
p[i] = temp;
}
}
}
}
//遞歸選擇法排序
int RecursiveSelectSort(int *p, int length)
{
if (p == NULL || 1 == length)
{
return 0;
}
int k = 0;
for(int i = 0; i<length; i++) //找到最小值的下標
{
if (p[k] > p[i])
{
k = i;
}
}
int temp = p[k];
p[k] = p[0];
p[0] = temp;
p++; //找到最小值后,數組向前進一步
length--; //長度當然減小一步
RecursiveSelectSort(p, length); //找下一個最小值
return 1;
}
int main()
{
int a[7] = {3, 8, 5, 7, 4, 2, 1};
//SelectSort(a, 7);
//BubbleSort(a, 7);
RecursiveSelectSort(a, 7);
return 1;
}