排序算法

简单选择排序:

void SelectionSort ( int *Array, int len )
{
    int i, min, j, temp;
    
    for( i = 0; i < len - 1; i++ )
    {
        min = i;                  /*假定起始位置为最小记录的位置*/
  
        for( j = i + 1; j < len; j++ )      /*查找最小记录*/
        {
            if( Array[j] < Array[min] )
            min = j;
        } 
        if(min != i)               /*如果i不是假定位置,则交换*/
        {
            temp = Array[i];
            Array[i] = Array[min];
            Array[min] = temp;   
        }       
    }
}

冒泡排序:

void BubbleSort ( int *Array, int len ) 
{
    int limit;
    int temp;

    for ( limit = len - 1; limit > 0; limit-- )
    {
        int j, swapped;

        /* On each pass, sweep largest element to end of array */
        swapped = 0;
        for ( j = 0; j < limit; j++ )
        {
            if ( Array[j] > Array[j+1] )
            {        
                temp = Array[j];
                Array[j] = Array[j+1];
                Array[j+1] = temp;
                swapped = 1;
            }
        }

        if ( !swapped )
            break;                     /* if no swaps, we have finished */
    }
}

直接插入排序:

void InsertionSort ( int *Array, int len )
{
    int step;
    int temp;

    /* Look at 2nd thru Nth elements, putting each in place */
    for (step = 1; step < len; step++)
    {
        int i;

        /* Now, look to the left and find our spot */
        temp = Array[step];
        for ( i = step; i > 0; i-- )
        {
            if ( Array[i-1] > temp )
            {
                /* Not there yet, so make room */
                Array[i] = Array[i-1];
            }
            else /* Found it! */
                break;
        }
        /* Now insert original value from Array[step] */
        Array[i] = temp;

    }
}

希尔排序:

void ShellSort ( int *Array, int len )
{
    int step, h, temp;

    /* Find starting h */
    for ( h = 1; h <= len / 9; h = 3*h + 1 )
                ;

    /* Now loop thru successively smaller h's */
    for ( ; h > 0; h /= 3 )
    {
        /* Look at hth thru Nth elements */
        for ( step = h; step < len; step++ )
        {
            int i;

            /* Now, look to the left and find our spot */
            temp = Array[step];
            for ( i = step; i >= h; i -= h )    //[0 h]、[1 h+1]、[2 h+2]、[3 h+3].....[h 2h]、[h+1 2h+1].
            {
                if ( temp < Array[i-h] )
                {
                    /* Not there yet, so make room */
                    Array[i] = Array[i-h];
                }
                else /* Found it! */
                    break;
            }
            /* Now insert original value from Array[step] */
            Array[i] = temp;
        }
    }
}

堆排序:

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

void heapdown(int *a,int i,int N)//自顶向下调整				
{
    int child,temp;
    
    for(temp = a[i];(2*i+1) < N;i = child)
    {
        child = 2*i + 1;
        if(child != N-1 && a[child+1] > a[child])
            child++;
        if(temp < a[child])
            a[i] = a[child];
        else
            break;    
    }        
    a[i] = temp;
}
void heapsort(int *a,int N)//根节点存于a[0]中
{
    int i;
    for(i = N/2;i >= 0;i--)							//BuildHeap
        heapdown(a,i,N);
    
    for(i = N-1;i > 0 ;i--)							//DeleteMax
    {
        swap(&a[0],&a[i]);
        heapdown(a,0,i);    
    }    
}

快速排序:

void swap(int *a,int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;    
}
void Qsort(int *a,int left,int right)
{
    int i,j,pivot,temp;
	if(left >= right)
		return; 
	temp = (left+right)/2;
	swap(&a[left],&a[temp]);
    pivot = a[left];
    i = left;
    j = right + 1;
    for(;;)
    {
        do i++;while(i <= right && a[i] < pivot);
        do j--;while(a[j] > pivot);
        
        if(i < j)
            swap(&a[i],&a[j]);
         else
            break;            
    }    
    swap(&a[left],&a[j]);   
  
    Qsort(a,left,j-1);
    Qsort(a,j+1,right);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值