设为首页 加入收藏

TOP

常用排序算法时间复杂度和空间复杂度简析(一)
2015-07-24 05:54:30 来源: 作者: 【 】 浏览:5
Tags:常用 排序 算法 时间 复杂度 空间 简析

1. preface

/****
* This article will try to explain something about:
* --Bubble sort.
* --Quick sort.
* --Merge sort.
* --Heap sort.
* To read this, some prerequisites is necessary:
* --a survive skill in C programming language.
* --a basic skill in CPP.
* --basic knowledge about Time complexity and Space complexity.
* --a generous mind for my fault because this article is free.
*
* Actually, their basic operating principle are easy to understand, but , unfortunately, the precise explication is big problem, at least for me. Here is the contents.
* --Analysis about Bubble sort.
* --Analysis about Quick sort.
* --Analysis about Merge sort.
* --Analysis about Heap sort.
* their source code can be find in fellowing articles. As we all know, for a programmer, the source code is also a good choice.
*/

2. Bubble sort

/****
* Bubble Sort
* This is a really simple algorithm. I learn it when I began to learn C. It just compare two value successively and swap them. The core source code is as fellowing:
*/

2.1 core code

bool BubbleSort::sort(void)
{
    int i,j;
    for( i=0; i<=n; i++)
        for( j=0; j< n -i; j++)
        {
            if( this->array[ j]>this->array[ j+1])
            {
                this->swap( j, j + 1);	//swap two values
            }
        }
}

2.2 Time complexity and Space complexity

/**
* For sort algorithm, it's basic operation is swap two values.So we can compute it's sentence frequency f(n):
* f(n) = n*n = n^2
* (at worst situation)
* and it's time complexity is :
* T(n) = O( f(n)) = O(n^2)
*
*
* obviously, It's space complexity is :
* S(n) = O( g(n)) = O( C) = O(1)
* because it use only constant space whatever n change.
*/

/*
* The totally example source code is here:
*
* (It maybe some fault, I will glad to your advices)
*/

3. Quick sort

/****
* Quick Sort
* This is a famous algorithm. It was developed in 1960 , but never outdated even for now. I was face a problem about sort a million of numbers. Need to say that is a

* nightmare if use bubble sort. Then I learn Quick Sort, it provide a exciting performance. I will explain this below. The principle of quicksort is "divided and process".

* In detail,
* --step1: Pick an element from the array as pivot.
* --step2: Part all elements into two areas: left area and right area.put element that's value less than pivot's value into left area,and put other elements into right area.
* --step3: Recursively do the step above to those sub-array.
*
*
*. First at all, examine it's core source code:
*/

3.1 Core Code

static void quick_sort( int array[], INDEX left, INDEX right)
{
    if( right-left>=2)
    {//core code
        int p;
        p = pivot( array, left, right);		//step1 + step2
        quick_sort( array, left, p-1);		//step3
        quick_sort( array, p+1,  right);
    }
    else if( right -left ==1)
    {//auxiliary
        if( array[left] > array[right])
        {
            swap( array + left, array + right);
        }
    }
}

static int pivot( int array[], INDEX left, INDEX right)
{
    //get povit, one of methods
    INDEX	mInd = (left + right)/2;
    //divide array into two parts
    int	i = 0;
    int	LLen = 0, RLen = 0;
    for( i=left; i<=right; i++ )
    {
        if( i==mInd)
            continue;

        if( array[i]< array[mInd] )
        {
            Arr_back[left + LLen] = array[i];
            LLen++;
        }
        else
        {
            Arr_back[right - RLen] = array[i];
            RLen++;
        }
    }
    Arr_back[left + LLen] =  array[mInd];
    memcpy( array+left, Arr_back + left, (right-left + 1)*sizeof(int));	//use a auxiliary space
    return left + LLen;
}

3.2 Time complexity

/**
* For quicksort, the basic operation is similar to swap above. So we could compute a valid sentence frequency. If there are n elements, in average situation the depth of

* recurrence is log2(n).Just as below:
*
* step1: [1.........................................................n] // n
* step2: [1......................m1] [m1.......................n] // n/2 + n/2
* step3: [1.....m2] [m2.....m1] [m1....m3] [m3......n] // n/4 + n/4 + n/4 + n/4
* .......
* stepX: [1,2][3,4]................................................
*
* and funny is that: for step N, if we want to part those arrays into sub-array, we need the nu

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇kd-tree讲解 & bzoj 2648 &am.. 下一篇HDOJ 1281 棋盘游戏

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: