冒泡排序與快速排序

一、冒泡排序 
相信冒泡排序大家都知道,無非是每一趟排序時將相鄰兩個元素進行交換,根據從大到小或從小到大的順序,交換起來略有不同,如此進行n-1次排序即可得到整個數組有序。

1、平均時間複雜度爲O(n^2) 
2、最好情況下時間複雜度爲O(n),此時設置標誌位,元素有序時直接退出 
3、最壞之間複雜度爲O(n^2) 
4、空間複雜度爲O(1)

代碼實現:

/*
 *進行數組的冒泡排序
 */

#include<stdio.h>

#define MaxSize 100

/*
 *進行冒泡排序
 */
void bubbleSort(int a[] , int length) ;

/*
 *進行元素的交換
 */
void swap(int *a , int *b) ;

/* 
 *進行數組的顯示
 */
void displayArray(int a[] , int length) ;

void main()
{
    int a[MaxSize] , length , i ;
    printf("Please input the length of the array : \n") ;
    scanf("%d" , &length) ;

    //進行數組元素的接收
    for(i = 0 ; i < length ; i++)
    {
        scanf("%d" , &a[i]) ;
    }

    printf("Before sort... \n") ;
    displayArray(a , length) ;

    bubbleSort(a , length) ;

    printf("After sort... \n") ;
    displayArray(a , length) ;
}

void bubbleSort(int a[] , int length)
{
    int i , j ;
    int flag ;
    flag = 1 ;
    for(i = 0 ; i < length - 1 && flag ; i++)
    {
        for(j = 1 ; j < length - i ; j++)
        {
            flag = 0 ;
            if(a[j] < a[j - 1])
            {
                swap(&a[j] , &a[j - 1]) ;
                flag = 1 ;
            }
        }
    }
}

void swap(int *a , int *b)
{
    int temp ;
    temp = *a ;
    *a =  *b;
    *b = temp ;
}

void displayArray(int a[] , int length)
{
    int i ;
    for(i = 0 ; i < length ; i++)
    {
        printf("%4d" , a[i]) ;
    }
    printf("\n") ;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81

二、快速排序 
快速排序即從數組中選擇一個標誌位,將比其小的元素放置在其前面,比其大的元素放置在其後面,然後將兩邊的前面的數組與後面的數組部分做同樣的置換,直至元素有序即可。

1、平均時間複雜度爲O(nlogn) 
2、最好時間複雜度爲O(nlogn) 
3、最壞時間複雜度爲O(n^2),此時元素基本有序 
4、空間複雜度爲O(logn)

代碼實現

/*
 *進行數組的快速排序
 */

#include<stdio.h>

#define MaxSize 100

/*
 *快速排序算法
 */
void quickSort(int a[] , int  low , int high) ;

/*
 *進行一趟排序
 *返回值爲中間值的下標
 */
int quickPass(int a[] ,int low , int high) ;

/*
 *進行數組的顯示
 */
void displayArray(int a[] , int length) ;

void main()
{

    int a[MaxSize] , length , i ;
    printf("Please input the length of the array : \n") ;
    scanf("%d" , &length) ;

    //進行數組元素的接收
    for(i = 0 ; i < length ; i++)
    {
        scanf("%d" , &a[i]) ;
    }

    printf("Before sort... \n") ;
    displayArray(a , length) ;

    quickSort(a , 0 , length - 1) ;

    printf("After sort... \n") ;
    displayArray(a , length) ;
}

void quickSort(int a[] , int low , int high)
{
    int pass ;
    if(low < high)
    {
        pass = quickPass(a , low , high) ;
        quickSort(a , low , pass - 1) ;
        quickSort(a , pass + 1 , high) ;
    }
}

int quickPass(int a[] , int low , int high)
{
    int temp ;
    temp = a[low] ;
    while(low < high)
    {
        while(low < high && a[high] > temp)
        {
            high-- ;
        }
        if(low < high)
        {
            a[low] = a[high] ;
            low ++ ;
        }

        while(low < high && a[low] < temp)
        {
            low ++ ;
        }
        if(low < high)
        {
            a[high] = a[low] ;
            high-- ;
        }
    }
    a[low] = temp ;
    return low ;
}

void displayArray(int a[] , int length)
{
    int i ;
    for(i = 0 ; i < length ; i++)
    {
        printf("%4d " , a[i]) ;
    }
    printf("\n") ;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章