C#归并排序算法

C#归并排序算法

归并排序算法

        
         public JArray MergeSort(JArray left, JArray right)
         {
            JArray result = new JArray();
            int i = 0;
            int j= 0;
            
            while (i < left.Count && j < right.Count)
            {
                long ii = long.Parse(left[i]["createtimestamp"].ToString());    //left[i]["createtimestamp"].ToString();获取数组节点的值

                long jj = long.Parse(right[j]["createtimestamp"].ToString());

                if (ii > jj)
                {
                    result.Add(left[i++]);
                }
                else
                {
                    result.Add(right[j++]);
                }
            }
            while (i < left.Count)
            {
                result.Add(left[i++]);
            }
            while (j < right.Count)
            {
                result.Add(right[j++]);
            }
            return result;
        }
        

调用归并排序算法

        
        //str为Newtonsoft.Json.Linq.JValue类型的对象
        public JArray GetResult(string str1, string str2, string str3, string str4)
        {
            JArray result = new JArray();

            if (string.IsNullOrWhiteSpace(str1))//str1为空
            {
                if (string.IsNullOrWhiteSpace(str2))//str2为空
                {
                    if (string.IsNullOrWhiteSpace(str3))//str3为空
                    {
                        if (string.IsNullOrWhiteSpace(str4))//str4为空
                        {
                            result = null;//4个string都为空  1
                        }
                        else if(!string.IsNullOrWhiteSpace(str4))//str4不为空
                        {
                            JArray array4 = JArray.Parse(str4);//Newtonsoft.Json.Linq转化为数组
                            result = array4;//只有str4不为空  2
                        }
                    }
                    else if(!string.IsNullOrWhiteSpace(str3))//str3不为空
                    {
                        if (string.IsNullOrWhiteSpace(str4))//str4为空
                        {
                            JArray array3 = JArray.Parse(str3);//Newtonsoft.Json.Linq转化为数组
                            result = array3;//只有str3不为空  3
                        }
                        else if(!string.IsNullOrWhiteSpace(str4))//str4不为空
                        {
                            JArray array3 = JArray.Parse(str3);//Newtonsoft.Json.Linq转化为数组

                            JArray array4 = JArray.Parse(str4);//Newtonsoft.Json.Linq转化为数组

                            result = MergeSort(array3, array4);//调用归并排序算法  str3,str4不为空  4
                        }
                    }
                }
                else if(!string.IsNullOrWhiteSpace(str2))//str2不为空
                {
                    JArray array2 = JArray.Parse(str2);//Newtonsoft.Json.Linq转化为数组

                    if (string.IsNullOrWhiteSpace(str3))//str3为空
                    {
                        if (string.IsNullOrWhiteSpace(str4))//str4为空
                        {
                            result = array2;//只有str2不为空  5
                        }
                        else if(!string.IsNullOrWhiteSpace(str4))//str4不为空
                        {
                            JArray array4 = JArray.Parse(str4);//Newtonsoft.Json.Linq转化为数组

                            result = MergeSort(array2, array4);//调用归并排序算法  str2,str4不为空  6
                        }
                    }
                    else if(!string.IsNullOrWhiteSpace(str3))//str3不为空
                    {
                        JArray array3 = JArray.Parse(str3);//Newtonsoft.Json.Linq转化为数组
                        JArray getresult1 = MergeSort(array2, array3);//调用归并排序算法

                        if (string.IsNullOrWhiteSpace(str4))//str4为空
                        {
                            result = getresult1;//str2,str3不为空  7
                        }
                        else if(!string.IsNullOrWhiteSpace(str4))//str4不为空
                        {
                            JArray array4 = JArray.Parse(str4);//Newtonsoft.Json.Linq转化为数组
                            result = MergeSort(getresult1, array4);//调用归并排序算法 str2,str3,str4不为空  8
                        }
                    }
                }

            }
            else if(!string.IsNullOrWhiteSpace(str1))//str1不为空
            {
                JArray array1 = JArray.Parse(str1);//Newtonsoft.Json.Linq转化为数组

                if (string.IsNullOrWhiteSpace(str2))//str2为空
                {
                    if (string.IsNullOrWhiteSpace(str3))//str3为空
                    {
                        if (string.IsNullOrWhiteSpace(str4))//str4为空
                        {
                            result = array1;//只有str1不为空  9
                        }
                        else if(!string.IsNullOrWhiteSpace(str4))//str4不为空
                        {
                            JArray array4 = JArray.Parse(str4);//Newtonsoft.Json.Linq转化为数组
                            result = MergeSort(array1, array4);//调用归并排序算法 str1,str4不为空  10
                        }
                    }
                    else if(!string.IsNullOrWhiteSpace(str3))//str3不为空
                    {
                        JArray array3 = JArray.Parse(str3);//Newtonsoft.Json.Linq转化为数组

                        JArray getresult1 = MergeSort(array1, array3);//调用归并排序算法

                        if (string.IsNullOrWhiteSpace(str4))//str4为空
                        {
                            result = getresult1;// str1,str3不为空  11
                        }
                        else if(!string.IsNullOrWhiteSpace(str4))//str4不为空
                        {
                            JArray array4 = JArray.Parse(str4);//Newtonsoft.Json.Linq转化为数组

                            result = MergeSort(getresult1, array4);//调用归并排序算法  str1,str3,str4不为空  12
                        }
                    }
                }
                else if(!string.IsNullOrWhiteSpace(str2))//str2不为空
                {
                    JArray array2 = JArray.Parse(str2);//Newtonsoft.Json.Linq转化为数组
                    JArray getresult1 = MergeSort(array1, array2);//调用归并排序算法

                    if (string.IsNullOrWhiteSpace(str3))//str3为空
                    {
                        if (string.IsNullOrWhiteSpace(str4))//str4为空
                        {
                            result = getresult1;//str1,str2不为空  13
                        }
                        else if(!string.IsNullOrWhiteSpace(str4))//str4不为空
                        {
                            JArray array4 = JArray.Parse(str4);//Newtonsoft.Json.Linq转化为数组
                            result = MergeSort(getresult1, array4);//调用归并排序算法 str1,str2,str4不为空  14
                        }
                    }
                    else if(!string.IsNullOrWhiteSpace(str3))//str3不为空
                    {
                        JArray array3 = JArray.Parse(str3);//Newtonsoft.Json.Linq转化为数组
                        JArray getresult2 = MergeSort(getresult1, array3);//调用归并排序算法

                        if (string.IsNullOrWhiteSpace(str4))//str4为空
                        {
                            result = getresult2;//str1,str2,str3不为空  15
                        }
                        else if(!string.IsNullOrWhiteSpace(str4))//str4不为空
                        {
                            JArray array4 = JArray.Parse(str4);//Newtonsoft.Json.Linq转化为数组
                            result = MergeSort(getresult2, array4);//调用归并排序算法 str1,str2,str3,str4都不为空  16
                        }
                    }
                }
            }
            return result;
        }
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值