排序

冒泡排序

思路:将相邻的两个数比较,将较小的数调到前头;有n个数就要进行n-1趟比较,第一次比较中要进行n-1次两两比较,在第j趟比较中,要进行n-j次两两比较

c语言实现



void bubble(int arry[], int count){
    int i, j, temp;

    for(i = 0; i < count-1; i++){ //比较n-1次
            for(j = 0; j < count-i-1; j++){
                if (arry[j] > arry[j+1]){ // 把值较大的数沉底
                        temp = arry[j];
                        arry[j] = arry[j+1];
                        arry[j+1] = temp;
                }
        }      
    }
}

void bubble_sort(int *a, int n)
{
int i, j, temp, k;
int flag;

for (i = 0; i < n; i++)
{   
    flag = 0;
    for (j = i + 1; j < n; j++)
    {   
        if (a[i] > a[j])
        {   
            flag = 1;
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }   
    }   
    for (k = 0; k < n; k++)
        printf("%d, ", a[k]);
    printf("flag=%d \n", flag);
    if (flag == 0)
        break;
    }   
}

python实现


def bubble(alist):
    for i in range(len(alist)-1):
        for j in range(len(alist)-i-1):
            if alist[j] > alist[j+1]:
                alist[j], alist[j+1] = alist[j+1], alist[j]

插入排序

思路:在得到要排序的数组以后,讲数组分为两个部分,数组的第一个元素为一个部分,剩下的元素为一部分,然后从数组的第二个元素开始,和该元素以前的所有元素比较,如果之前的元素没有比该元素大的,那么该元素的位置不变,如果有元素的值比该元素大,那么记录他所在的位置;*该算法利用该元素前面是排好顺序的*

c语言实现



void insert_sort(int arry[], int count){
    int i, j, k;

    for(i = 1; i < count; i++){
        k = arry[i];
        for(j = i; j > 0 && arry[j-1] > k; j--){
            arry[j] = arry[j-1];
        }
        arry[j] = k;
    }
}

python实现



def insert_sort(alist):
    for i in range(1, len(alist)):
        k = alist[i]
        j = i
        while j > 0 and alist[j-1] > k:
            alist[j] = alist[j-1]
            j -= 1

        alist[j] = k 

快速排序

思路:以第一个数为基准(key),先从high(数组末端)开始往回循环, 当找到比key小的交换, 然后从low(数组开始)循环, 找到比可以大的交换, 这是一次循环
然后在递归调用可以的左边和右边

c语言实现



void qsort(int *a, int left,u int right)
{
    int low = left;
    int high = right;
    int key = a[left];

    if (low >= high)return;
    while(low < high){
        while(low < high && a[high] > key)high--;
        if (low < high){
           a[low] = a[high];
           low++
        }
        while(low < high && a[low] < key)low++;
        if (low < high){
            a[high] = a[low];
            high--;
        }
    }
    a[low] = key;
    qsort(a, left, low-1);
    qsort(a, low+1, right);
}

python实现



def quicksort(alist, left, right):
    if left >= right:
        return
    low, high = left, right
    key = alist[(left+right)//2]

    while 1:
        while alist[low] < key:low += 1
        while alist[high] > key:high -= 1

        if high <= low or alist[low] == alist[high]:
            break
        alist[low], alist[high] = alist[high], alist[low]
        print alist, low, high, key 

    quicksort(alist, left, low-1)
    quicksort(alist, high+1, right)

if __name__ == '__main__':
    alist = [0, 0, -1, 0, 2]
    print alist
    quicksort(alist, 0, len(alist)-1)
    #bubble_sort(alist)
    print alist

def qsort(L):
    if len(L) <=1 : 
        return L
    return qsort([lt for lt in L[1:] if lt < L[0]]) + L[0:1] + qsort([ge for ge in L[1:] if ge >= L[0]])

你可能感兴趣的:(排序)