用c语言给10个整数排序
时间: 2023-11-21 07:10:52 浏览: 50
可以使用冒泡排序法来对10个整数进行排序。以下是C语言代码示例:
```c
#include <stdio.h>
int main() {
int arr[10] = { 9, 7, 5, 3, 1, 8, 6, 4, 2, 0 };
int i, j, temp;
// 冒泡排序
for (i = 0; i < 9; i++) {
for (j = 0; j < 9 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// 输出排序后的结果
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
这段代码中,我们定义了一个包含10个整数的数组,并使用冒泡排序法将它们排序。最后,我们输出排序后的结果。
相关问题
用c语言选择法给10个整数排序
以下是使用选择排序算法对10个整数进行排序的C代码:
```
#include <stdio.h>
void selection_sort(int arr[], int n)
{
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; 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
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
int main()
{
int arr[] = {64, 25, 12, 22, 11, 1, 9, 3, 7, 5};
int n = sizeof(arr)/sizeof(arr[0]);
selection_sort(arr, n);
printf("Sorted array: \n");
for (int i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
```
在这个程序中,我们使用选择排序算法对大小为10的整数数组进行排序。该算法首先在未排序的数组中查找最小元素,然后将其放在数组的开头。然后,算法将边界移到未排序的子数组的下一个元素,并重复这个过程,直到整个数组排序完成。
c语言输入10个整数进行排序
好的,以下是 C 语言代码示例:
```c
#include <stdio.h>
int main() {
int numbers[10];
int i, j, temp;
// 输入10个整数
for (i = 0; i < 10; i++) {
printf("请输入第 %d 个整数:", i+1);
scanf("%d", &numbers[i]);
}
// 冒泡排序
for (i = 0; i < 9; i++) {
for (j = 0; j < 9-i; j++) {
if (numbers[j] > numbers[j+1]) {
temp = numbers[j];
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}
// 输出排序后的结果
printf("从小到大排序后的结果为:\n");
for (i = 0; i < 10; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
```
这段代码使用了冒泡排序算法对输入的 10 个整数进行排序,并打印出排序后的结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)