冒泡排序的思想--依次比較相鄰的兩個數(shù),將小的數(shù)放在前面的大數(shù)后面娄徊,其時間復(fù)雜度為O(n^2)
void Bubble(int*p,int len){
for(int i=0; i<len-1瀑罗;i++){
? ? ? ? ? ? ? for(int j=0; j<len-i-1胸嘴;j++){
? ? ? ? ? ? ? ? ? ? ? ? ? if(*(p+j)>*(p+j+1)) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? swap(p+j);//交換大小數(shù)
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? }
? ? ? ? }
}
void? swap(int*p){
? ? ? ? ? ? int temp=*p;
? ? ? ? ? ? *p=*(p+1);
? ? ? ? ? ? *(p+1)=temp;
}
int main() {
? ? ? ? ? ? ? ? ? int arr[LEN]={13,12,16,15,17};
? ? ? ? ? ? ? ? ? Bubble(arr, LEN);
? ? ? ? ? ? ? ? ? ? for(inti=0; i<LEN; i++) {
? ? ? ? ? ? ? ? ? ? printf("%d\n",arr[i]);
? ? ? ? ? ? ? }
}