**
冒泡排序(C语言)
** 取1-100以内的伪随机数进行冒泡排序
#include<stdio.h>
#include<stdlib.h>
void Input_Ar(int* Ar,int n) {
for (int i = 0; i < n; i++)
{
Ar[i] = rand() % 100 + 1;
}
}
void Sort_Ar(int* Ar, int n) {
int temp = 0;
for (int i = 0; i < n-1; i++)
{
for (int j = 0; j < n-1-i; j++) {
if (Ar[j] > Ar[j + 1]) {
temp = Ar[j + 1];
Ar[j + 1] = Ar[j];
Ar[j] = temp;
}
}
}
}
int main() {
const int n = 20.;
int Ar[n] = {};
Input_Ar(Ar, n);
Sort_Ar(Ar, n);
for (int i = 0; i < n; i++) {
if (i % 10 == 0) {
printf("\n");
}
printf("%d\t", Ar[i]);
}
system ("pause");
}
** 以下是运行结果**
|