排序算法(直接插入、冒泡排序、选择排序、快速排序、希尔排序、堆排序、归并排序)

main函数

int main()
{
    int data[] = {1,2,6,3,4,7,7,9,8,5};
    //bubble_sort(data,10);
    //select_sort(data,10);
    Insert_Sort(data,10);
    for(int i = 0; i < 10; i++)
        cout << data[i] <<" ";
    cout <

直接插入排序 O(n^2) 稳定

//插入排序
void Insert_Sort(int *pData, int Count)
{
    int iTmp ;
    int iPos;

    for(int i = 1; i < Count; i++)
    {
        iTmp = pData[i];
        iPos = i-1;
        while(iPos>=0 && pData[iPos]>iTmp)
        {
            pData[iPos+1] = pData[iPos];
            iPos--;
        }
        pData[iPos+1] = iTmp;
    }
    return ;
}

冒泡排序 O(n^2) 稳定

//*** 冒泡排序 **//
void bubble_sort(int *pData, int count)
{
    for(int i = 0; i < count; i++)
    {
        for(int j = count-1; j >= i; j--)
        {
            if(pData[j] < pData[j-1])
            {
                int tmp;
                tmp = pData[j];
                pData[j] = pData[j-1];
                pData[j-1] = tmp;
            }
        }
    }
}

选择排序 O(n^2) 不稳定

//***选择排序***///
void select_sort(int *pData, int count)
{
    int tmp;
    int iPos;
    for(int i = 0; i < count-1; i++ )
    {
       tmp = pData[i];
       iPos = i;
       for(int j = i+1; j < count; j++)
       {
           if(pData[j] < tmp)
           {
               tmp = pData[j];
               iPos = j;
           }
       }
       pData[iPos] = pData[i];
       pData[i] = tmp;
    }

}

快速排序 O(nlog(n)) 不稳定

//快速排序  递归
void Quick_Sort(int *pData, int low, int high)
{
    int i = low;
    int j = high;
    int x = pData[i];
    if(low > high)
        return;
    while( i < j )
    {
        while( i < j &&  pData[j] >= x) 
            j--;
            pData[i] = pData[j]; 
        while( i < j &&  pData[i] <= x)  
            i++;
            pData[j] = pData[i];
    }
    pData[i] = x;
    Quick_Sort(pData, low, i-1);
    Quick_Sort(pData, i+1, high);
}


//快排  非递归
int partion(int* pData,int low,int high)
{
    int tmp = pData[low];
    while(low < high)
    {
        while(low < high && pData[high] > tmp)
            high--;
        pData[low] = pData[high];
        while(low < high && pData[low] < tmp)
            low++;
        pData[high] = pData[low];
    }
    pData[low] = tmp;
    return low;
}

void quickSort(int* pData,int low,int high)
{
    stack S;
    int k,i,j;
    if(low > high)
       return;
    if(low < high)
    {
        S.push(low);
        S.push(high);
        while(!S.empty())
        {
        //不空时,依次把左边的和右边的入栈
            j = S.top();
            S.pop();
            i=S.top();
            S.pop();
            k=partion(pData,i,j); //弹出来后进行比较
            
       /*再次将左右两边分别入栈*/
            if(i < k-1)
            {
                S.push(i);
                S.push(k-1);
            }
            if(k+1 < j)
            {
                S.push(k+1);
                S.push(j);
            }
        }
    }
}

希尔排序 O(nlog(n)) 不稳定

根据插入排序改,换步长即可

void Insert_Sort(int *pData, int Count)
{
    int iTmp ;
    int iPos;
    int gap = Count;
    do{
        gap = gap / 3  + 1; //后面一定要有+1 ,不然无法保证最后的间隔为1
        for(int i = gap - 1 ; i < Count; i++)
        {
            iTmp = pData[i];
            iPos = i-gap;
            while( iPos >= 0 && pData[iPos] > iTmp )
            {
              pData[iPos+gap] = pData[iPos];
              iPos -= gap;
            }
            pData[iPos+gap] = iTmp;
        }
    }while(gap > 1);
    return ;
}

堆排序 O(nlog(n)) 不稳定

//构造最大堆
void MaxHeapFixDown(int a[], int i, int n){
    int j = 2*i+1; //左叶子
    int temp = a[i]; //根节点
    while(j < n){ //一个子树的判断
        if(j+1 < n && a[j] < a[j+1]) // 左节点小于右节点,就继续往下走
            ++j;
        if(temp > a[j])  //当根节点大于最大的左节点时就跳出来
            break;
        else{ 
            a[i] = a[j];//否则就把大的交换
            i=j;  //j赋给i
            j=2*i+1; //j到最右边
        }
    }
    a[i]=temp; //根与最大的节点交换
}
//堆排序
void Head(int a[], int n){
    for(int i= n/2-1;i >=0;i--)
        MaxHeapFixDown(a,i,n); //找到最大的数了
    for(int i=n-1;i>=1;i--){
        swap(a[i],a[0]);  //交换最大的数与叶节点
        MaxHeapFixDown(a,0,i);
    }
}

归并排序 O(nlog(n)) 稳定

//归并排序
//将数组分治为两个有序的数组,然后传给Merge
void Merge_Sort(int *pData, int Count)
{
  if(Count > 1)
    {
       int  *list1 = pData;
       int  list1_size = Count / 2;
       int  *list2 = pData + list1_size;
       int  list2_size = Count - list1_size;

       Merge_Sort(list1,list1_size);
       Merge_Sort(list2,list2_size);
       Merge(list1,list1_size,list2,list2_size);
    }
  return;
}
//Merge将两个有序的数组合并
void Merge(int *list1, int list1_size, int *list2, int list2_size)
{
    int tmp[list1_size+list2_size];
    int i = 0;
    int j = 0;
    int k = 0;
    while (i < list1_size  && j < list2_size )
    {
        if( list1[i] < list2[j])
            tmp[k++] = list1[i++];
        else
            tmp[k++] = list2[j++];
    }

    while (i < list1_size)
        tmp[k++] = list1[i++];

    while (j < list2_size)
       tmp[k++] = list2[j++];

    int m = 0;
    for(; m < (list1_size + list2_size);m++)
        list1[m] = tmp[m];
    return;
}

 

你可能感兴趣的:(C/C++)