You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

19 lines
695 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

//冒泡排序
void Sort(int a[], int n)
{
int i, j, t;
for(i = 0; i < n - 1; i ++)//控制循环次数
{
for(j = 0; j < n - 1 - i; j ++)//通过遍历与交换依次将较大的数放在数组后边
{
if(a[j] > a[j+1])////a[j] > a[j + 1]是升序, a[j] < a[j + 1]是降序
{
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
}
//复杂度说明基本操作为交换操作当数组按从小到大有序排列时基本操作执行次数为0从大到小有序排列时基本操作执行次数为 [n(n-1)/2]所以时间复杂度为O(n^2)