程序博客网 > tc编程
来源:互联网 发布:tc编程 编辑:程序博客网 时间:2024/06/05 03:07
冒泡排序算法:
c:
#include<stdio.h>void swap(int* a, int* b){int temp;temp = *a;*a = *b;*b = temp;}void bubble_sort(int *a, int length){ int i,j; for(i =length-1; i >0; --i) { for(j = 0; j < length-1; ++j) { if(a[j] > a[j+1]) swap (&a[j],&a[j+1]); } } }int main(){ int i; int array[] = {10,8,4,2,7,6,9,3,1,5}; bubble_sort(array,10); for(i = 0; i < 10; ++i) printf("%d/t",array[i]); return 0;}
python:
#!/usr/bin/env pythondef bubble_sort(lst):for i in range(len(lst)-1,0,-1):for j in range(0,len(lst)-1):if lst[j] > lst[j+1]:lst[j],lst[j+1] = lst[j+1],lst[j]if __name__ == "__main__":list1 = [9,3,7,4,1,6,5,0,8,2]bubble_sort(list1)print list1
haskell:
bubbleSort []=[]bubbleSort x= (iterate swapPass x) !! ((length x)-1) where swapPass [x]=[x] swapPass (x:y:zs) | x>y = y:swapPass (x:zs) | otherwise = x:swapPass (y:zs)