Algorithm_2_Merge_Sort

  In the card-playing motif,suppose we have two piles of cards face up on a table.Each pile is sorted,with the smallest cards on top.We wish to merge the two piles into a single sorted output pile,which is to be face down on the table.Our basic step consists of choosing the smaller of the two cards on top of of the face_up piles,removing it from its pile(which expsoe a new top card),and placing this card face down onto the output pile.We repeat this step until one input pile is empty,at which time we just take the remaining input pile and place it face down onto the output pile.

  Let’s show it.

/// <summary>
        
/// 合并.得到:数组从区间[firstIndex,lastIndex]按照大小排列
        
/// </summary>
        
/// <param name="arr">已知数组,元素从区间[firstIndex,middleIndex]
        
/// 和[middleIndex+1,lastIndex]按照数值大小排列</param>
        
/// <param name="firstIndex">索引</param>
        
/// <param name="middleIndex">索引</param>
        
/// <param name="lastIndex">索引</param>

        public static void Merge(Int32[] arr, Int32 firstIndex, Int32 middleIndex, Int32 lastIndex)
        
{
            Int32 n1 
= middleIndex - firstIndex + 1;//left half,contain arr[middle]
            Int32 n2 = lastIndex - middleIndex;//right
            Int32[] left = new Int32[n1];
            Int32[] right 
= new Int32[n2];
            
for (Int32 i = 0; i < n1; i++)
                left[i] 
= arr[firstIndex + i];
            
for (Int32 j = 0; j < n2; j++)
                right[j] 
= arr[middleIndex + 1 + j];
            Int32 ii 
= 0;
            Int32 jj 
= 0;
            
for (Int32 k = firstIndex; k <= lastIndex; k++)
            
{
                
if (ii < n1 && jj < n2)
                
{
                    
if (left[ii] <= right[jj])
                        arr[k] 
= left[ii++];
                    
else
                        arr[k] 
= right[jj++];
                }

                
else if(ii==n1 && jj<n2)
                
{
                    arr[k] 
= right[jj++];//left is empty
                }

                
else if (jj == n2 && ii < n1)
                
{
                    arr[k] 
= left[ii++];//right is empty
                }

            }

        }

 

So,we get the merge function.Now sort the array is easy.We use the Merge peocedure as a subroutine in the merge sort algrithm.

        public static void MergeSort(Int32[] arr, Int32 p, Int32 r)
        
{
            
if (p < r)
            
{
                Int32 q 
= (p + r) / 2;
                MergeSort(arr, p, q);
                MergeSort(arr, q 
+ 1, r);
                Merge(arr, p, q, r);
            }

        }

   If p
>=r,the subarray has at most one element and is therefore already sorted.

The Merge sorts running time is big theta n*lgn.

### One Third Merge Sort 实现与解释 One Third Merge Sort 是一种基于传统归并排序(Merge Sort)的变体。它通过将数组划分为三个部分而不是两个来优化分割过程,从而可能减少递归调用次数和提高性能。 #### 基本原理 传统的归并排序会将输入数组分成两半,分别对每一半进行排序后再合并它们。而 One Third Merge Sort 则尝试将数组划分成三份,每一份大约占总长度的三分之一[^1]。这种策略可以降低某些情况下递归树的高度,进而提升效率。 以下是该算法的主要步骤描述: 1. 将待排序数组 `arr` 分割为三个子数组:左部 (`left`)、中部 (`middle`) 和右部 (`right`)。 2. 对这三个子数组分别应用相同的排序逻辑直到达到基本情况为止。 3. 使用自定义的三路合并函数把已排序好的三个子数组重新组合起来形成最终有序序列。 下面给出 Python 中的一个简单实现版本: ```python def one_third_merge_sort(arr): if len(arr) <= 1: return arr n = len(arr) # Divide into three parts approximately equal size. third = n // 3 two_thirds = (n * 2) // 3 left = one_third_merge_sort(arr[:third]) middle = one_three_merge_sort(arr[third:two_thirds]) right = one_third_merge_sort(arr[two_thirds:]) return merge_three_parts(left, middle, right) def merge_three_parts(left, middle, right): result = [] i = j = k = 0 while i < len(left) and j < len(middle) and k < len(right): smallest = min((left[i], 'L'), (middle[j], 'M'), (right[k], 'R'))[0] if smallest == left[i]: result.append(left[i]); i += 1 elif smallest == middle[j]: result.append(middle[j]); j += 1 else: result.append(right[k]); k += 1 # Append any remaining elements from all lists. result.extend(left[i:]) result.extend(middle[j:]) result.extend(right[k:]) return result ``` 上述代码展示了如何利用分支界定法(branch and bound)[^2] 来解决更复杂的最优化问题的同时也适用于这里的一般化分治思路。注意这里的 `merge_three_parts()` 函数实现了同时比较来自三个列表中的最小值并将之加入到结果集中去的功能。 对于实际应用场景而言,如果数据集较大或者存在特定模式,则此方法可能会带来一定优势;但对于小型随机分布的数据来说,其表现未必优于标准二叉归并方式。 另外值得注意的是,在具体编码过程中还需要考虑边界条件以及异常处理等问题以确保程序健壮性和鲁棒性。 最后附上获取神经网络相关内容资源命令作为补充参考资料[^3]: ```bash git clone https://github.com/mnielsen/neural-networks-and-deep-learning.git ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值