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;
}