美文网首页
排序之归并排序

排序之归并排序

作者: OritimaHost | 来源:发表于2015-03-13 21:29 被阅读33次

    快速排序的自底向上

    Merger Sort
     void mergearray(int numbers[],int first,int mid,int last,int temp[])
    {
            int start = first;
            int end = mid + 1;
            int middle = mid;
            int index = 0;
            int i = 0;
            while(start <= middle && end <= last)
            {   
                    if(numbers[start] <  numbers[end])
                    {   
                            temp[index++] = numbers[start++];
                    }   
                    else
                    {   
                            temp[index++] = numbers[end++];
                    }   
            }   
            while(start <= middle)
            {   
                    temp[index++] = numbers[start++];
            }   
            while(end <= last)
            {   
                    temp[index++] = numbers[end++];
            }   
            for(i = 0; i < index ; i++)
            {   
                    numbers[i] = temp[i];
            }   
    }
    /* merge sort */
    int merge_sort(int numbers[] , int first ,int last,int temp[])
    {
            if(first < last)
            {   
                    int mid = (last + first) / 2;
                    /* left of array is sort */
                    merge_sort(numbers,first,mid,temp);
                    /* right of array is sort */ 
                    merge_sort(numbers,mid + 1,last,temp);
                    /* merge the left and rigth of array */
                    mergearray(numbers,first,mid,last,temp);
            }
    }
    

    相关文章

      网友评论

          本文标题:排序之归并排序

          本文链接:https://www.haomeiwen.com/subject/yigvxttx.html