用c++写一个最简单的排序
时间: 2024-05-15 10:15:35 浏览: 9
以下是使用冒泡排序算法的C程序,对数组进行从小到大的排序:
```
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
for (int i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
```
该程序首先定义了一个冒泡排序函数`bubbleSort()`,该函数使用嵌套的循环遍历数组中的每个元素,并按升序对它们进行排序。然后,程序将一个整数数组传递给函数并输出排序后的结果。
相关问题
用C++写一个简单选择排序
以下是用C语言编写的简单选择排序程序:
```c
#include <stdio.h>
void selection_sort(int arr[], int n) {
int i, j, min_index, temp;
for (i = 0; i < n - 1; ++i) {
min_index = i;
for (j = i + 1; j < n; ++j) {
if (arr[j] < arr[min_index]) {
min_index = j;
}
}
temp = arr[i];
arr[i] = arr[min_index];
arr[min_index] = temp;
}
}
int main() {
int arr[] = {10, 5, 3, 9, 2, 7, 6, 4, 8, 1};
int n = sizeof(arr) / sizeof(arr[0]);
int i;
printf("Original array:\n");
for (i = 0; i < n; ++i) {
printf("%d ", arr[i]);
}
printf("\n");
selection_sort(arr, n);
printf("Sorted array:\n");
for (i = 0; i < n; ++i) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
该程序的运行结果为:
```
Original array:
10 5 3 9 2 7 6 4 8 1
Sorted array:
1 2 3 4 5 6 7 8 9 10
```
该程序采用了简单选择排序算法的实现方式,其中变量 `arr` 存储了要进行排序的数组,变量 `n` 存储了数组的大小。在 `selection_sort` 函数中,程序通过两个嵌套的 `for` 循环来进行排序,外层循环控制当前排序位置,内层循环用于查找未排序部分中的最小值,并将其放在当前位置。最后,主函数 `main` 中分别输出原始数组和排序后的结果。
用c++写一个冒泡排序
### 回答1:
好的,以下是用 C 语言写的冒泡排序代码:
```
#include <stdio.h>
void bubble_sort(int arr[], int len)
{
int i, j, temp;
for (i = 0; i < len - 1; i++)
{
for (j = 0; j < len - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main()
{
int arr[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5 };
int len = sizeof(arr) / sizeof(arr[0]);
int i;
printf("Before sorting: ");
for (i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
bubble_sort(arr, len);
printf("After sorting: ");
for (i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
这个程序可以对一个整数数组进行冒泡排序,将数组中的元素从小到大排序。
### 回答2:
冒泡排序是一种基本的排序算法,其基本思想是通过相邻元素之间的比较和交换来实现排序。以下是用C语言写的一个冒泡排序算法示例:
```c
#include <stdio.h>
// 定义冒泡排序函数
void bubbleSort(int arr[], int n) {
int i, j;
for (i = 0; i < n-1; i++) { // 控制比较的轮数
for (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 arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("排序前的数组:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
bubbleSort(arr, n); // 调用冒泡排序函数
printf("\n排序后的数组:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
该示例中,首先定义了一个冒泡排序函数bubbleSort,接受一个整型数组和数组长度n作为参数。在主函数main中,初始化一个待排序的数组arr,通过调用bubbleSort函数进行排序,并通过printf函数打印排序前和排序后的数组。最后返回0表示程序顺利结束。运行上述代码,将得到输出结果为:
```
排序前的数组:
64 34 25 12 22 11 90
排序后的数组:
11 12 22 25 34 64 90
```
以上就是用C语言写的一个冒泡排序的示例,希望对你有帮助!
### 回答3:
冒泡排序是一种基础的排序算法,思路很简单,通过不断比较相邻的元素并交换位置,使得最大的元素逐渐移动到最后。下面是用C语言写的一个冒泡排序算法:
```c
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// 交换 arr[j] 和 arr[j+1] 的位置
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("排序后的数组:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
上述代码实现了一个冒泡排序算法。在`bubbleSort`函数中,通过两个嵌套的循环遍历数组,如果当前元素比下一个元素大,则交换它们的位置。在主函数中,定义一个整型数组`arr`,并确定数组的大小`n`。调用`bubbleSort`函数对数组进行排序,并输出排序后的结果。
以上是用C语言写的一个简单的冒泡排序算法,它可以对给定的数组进行升序排序。当然,冒泡排序算法的时间复杂度较高,如果排序规模较大,建议使用更高效的排序算法。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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_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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)