编程实现用改进的选择排序对n(n不超过100,n从键盘输入)个浮点数升序排序
时间: 2023-03-28 15:01:17 浏览: 103
可以使用以下代码实现改进的选择排序对n个浮点数升序排序:
#include <stdio.h>
void selectionSort(float arr[], int n) {
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = ; i < n-1; i++) {
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first element
float temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
int main() {
int n, i;
float arr[100];
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i = ; i < n; i++)
scanf("%f", &arr[i]);
selectionSort(arr, n);
printf("Sorted array in ascending order:\n");
for (i = ; i < n; i++)
printf("%.2f ", arr[i]);
return ;
}
阅读全文