C语言编写用冒泡法对 10 个整数排序程序,并输出
时间: 2023-12-03 17:47:32 浏览: 165
下面是C语言冒泡排序的代码:
```c
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j for (i = 0; i < n1; i++) {
for (j = ; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// swap[j] and arr[j+1 int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
int i;
bubbleSort(arr, 10);
printf("Sorted array: ");
for (i=0; i < 10; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
```
这个程序使用了冒泡排序算法对10个整数进行排序,并输出排序后的结果。
相关问题
用c语言编写用选择法对10个整数排序
在C语言中,我们可以使用冒泡排序、插入排序、选择排序等算法来对一组整数进行排序。这里以选择排序为例,因为它的操作简单直观,适合教学。选择排序的基本思想是在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后再从剩余未排序元素中继续寻找最小(大)元素,直到所有元素均排序完毕。
以下是用C语言实现选择排序对10个整数数组进行升序排序的一个基本示例:
```c
#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int find_min(int arr[], int n) {
int min_idx = 0;
for (int i = 1; i < n; i++) {
if (arr[i] < arr[min_idx]) {
min_idx = i;
}
}
return min_idx;
}
void selection_sort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int min_idx = find_min(&arr[i], n - i - 1);
swap(&arr[i], &arr[min_idx]);
}
}
int main() {
int numbers[10] = {5, 2, 9, 1, 7, 6, 8, 3, 4, 10};
int n = sizeof(numbers) / sizeof(numbers[0]);
printf("Original array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}
selection_sort(numbers, n);
printf("\nSorted array using Selection Sort:\n");
for (int i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
```
在这个程序中,`find_min` 函数找出数组中的最小值的索引,然后`selection_sort`函数通过不断交换当前未排序部分的最小元素来完成排序过程。
用C语言编写程序,输入n个整数排序并输出。要求用冒泡法排序,将排序单独写成一个函数,n个整数在主函数中输入,排序结果在主函数中输出。编写程序,用指针实现上述功能要求。
好的,我会尽力回答你的问题。以下是用指针实现冒泡排序的C语言程序:
```
#include <stdio.h>
void bubble_sort(int *arr, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (*(arr + j) > *(arr + j + 1)) {
int temp = *(arr + j);
*(arr + j) = *(arr + j + 1);
*(arr + j + 1) = temp;
}
}
}
}
int main() {
int n;
printf("请输入整数个数n:");
scanf("%d", &n);
int arr[n];
printf("请输入%d个整数:", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
bubble_sort(arr, n);
printf("排序结果为:");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
在这个程序中,我们首先定义了一个 `bubble_sort` 函数,它接受一个整型指针 `arr` 和一个整数 `n` 作为参数,用冒泡排序算法对 `arr` 数组进行排序。
在主函数中,我们先读入整数个数 `n`,然后定义一个长度为 `n` 的整型数组 `arr`,并读入 `n` 个整数到这个数组中。接着调用 `bubble_sort` 函数对 `arr` 数组进行排序,最后输出排序结果。
需要注意的是,在函数中我们是通过指针访问数组元素的,因此需要使用 `*(arr + i)` 的形式来访问数组元素,而不能使用 `arr[i]` 的形式。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)
![](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)