编写函数用冒泡排序法对数组中的n个数据进行从小到大的排序。

如题所述

1、新建一个163.php。

2、输入php网页的结构(<?php?>)。

3、声明PHP与浏览器交互的文件类型和编码。

4、使用 array() 函数定义一个$numbers数组。

5、使用 sort() 函数对数组 $numbers 中的元素进行排序。

6、使用 print_r() 函数,输出排序后的数组。

7、运行网页,在浏览器中输出排序后的数组。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-01-02
#include <stdio.h>#include <time.h>#include <stdlib.h> #define N 50 void bubbleSort(int a[], int n); int main(void){ int a[N] = {0}, i = 0; srand(time(NULL)); for(i = 0; i < N; i++) printf("%d ", a[i] = rand() % 100); printf("\n排序后:\n"); bubbleSort(a, N); for(i = 0; i < N; i++) printf("%d ", a[i]); printf("\n"); return 0;} void bubbleSort(int a[], int n){ int i = 0, j = 0, temp = 0; for(i = 0; i < n - 1; i++) { for(j = 0; j < n - 1 - i; j++) { if(a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } }}
相似回答