问题一:写出冒泡排序
void bubble_sort(int arr[], int len)
{
for (int i = 0; i < len - 1; i++) {
for (int j = len - 1; j > i; j--) {
if (arr[j] < arr[j - 1]) {
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
}
问题二:写出选择法排序
void select_sort(int arr[], int len)
{
for (int i = 0; i < len; i++) {
int index = i;
for (int j = i + 1; j < len; j++) {
if (arr[j] < arr[index])
index = j;
}
if (index != i)
{
int temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
}
}