将数组int arr[8] = {81, 93, 4, 22, 1, 14, 59, 9}从小到大排C语言实现
时间: 2024-05-18 15:13:59 浏览: 82
将数组元素按照从小到大的顺序排列
3星 · 编辑精心推荐
要将数组int arr = {81, 93, 4, 22, 1, 14, 59, 9}从小到大排列,可以使用冒泡排序算法。冒泡排序算法的基本思想是比较相邻的元素并交换它们,使得最大的元素逐渐移动到最后。以下是用C语言实现的代码:
```c
#include <stdio.h>
void bubble_sort(int arr[], int sz) {
int i, j;
for (i = 0; i < sz - 1; i++) {
for (j = 0; j < sz - 1 - i; j++) {
if (arr[j > arr[j + 1]) {
int temp = arr[j];
arr[j = arr[j + 1];
arr[j + 1 = temp;
}
}
}
}
int main() {
int arr[] = {81, 93, 4, 22, 1, 14, 59, 9};
int sz = sizeof(arr) / sizeof(arr<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [C语言-数组【详解】](https://blog.csdn.net/weixin_69231613/article/details/128714099)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文