求助一个最优算法(C#实现) 两个int数组,无序,可能有重复的要求合并为一个数组并排序而且去掉重复的不能用.net类库,array,list之类的一律不准用谢谢大家。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你用C#就已经用了.net类库了,你让我们怎么写,用.net类库的话最方便最好实现的也就是array,list之类的,你在自己写方法之前你要想自己写的东西有MS写的效率、安全、封装的好吗? System.Array是所有数组的基类用数组就要用Array 这种问题就是专门用于显摆的了,实际情况中肯定不会这样做,不是丢到数据库去处理,就是类方法搞定。但是为了满足某些人的虚荣心,有时候知道回字的4种写法也是无可奈何的事情。根据有两个数组,推测需要的答案是1.对A数组实行冒泡的变形算法,冒泡的同时除重。之后获得有序的无重复的A数组2.B数组针对A数组实行插入排序,同时除重。插入位置的查找使用2分法。 引用一篇文章的给你看看,好像没有说重复的值。。public static int[] Func(int[] SLm, int[] SLn){ if (SLm == null || SLn == null) { throw new ArgumentException("传入数组不能为空"); } int[] result = new int[SLm.Length + SLn.Length]; int mIndex = 0; int nIndex = 0; for (int index = 0; index < result.Length; index++) { if (mIndex >= SLm.Length && nIndex >= SLn.Length) { break; } if (mIndex >= SLm.Length) { result[index] = SLn[nIndex++]; continue; } if (nIndex >= SLn.Length) { result[index] = SLm[mIndex++]; continue; } if (SLm[mIndex] < SLn[nIndex]) { result[index] = SLm[mIndex++]; } else if (SLm[mIndex] > SLn[nIndex]) { result[index] = SLn[nIndex++]; } else { result[index] = SLm[mIndex++]; nIndex++; } } return result;} 今天文思的笔试算法题http://topic.csdn.net/u/20100315/18/2ec73a6f-da36-4116-b2a5-ed727022f7fd.html 刚刚写了下... Int32[] arr1 = new Int32[] { 3,1,9,5,3}; Int32[] arr2 = new Int32[] { 45,34,23,7,3}; Int32 temp; for (Int32 ii = 1; ii < arr1.Length; ii++) { for (Int32 jj = 0; jj < arr1.Length - ii; jj++) { if (arr1[jj] > arr1[jj + 1]) { temp = arr1[jj]; arr1[jj] = arr1[jj + 1]; arr1[jj + 1] = temp; } } } for (Int32 ii = 1; ii < arr2.Length; ii++) { for (Int32 jj = 0; jj < arr2.Length - ii; jj++) { if (arr2[jj] > arr2[jj + 1]) { temp = arr2[jj]; arr2[jj] = arr2[jj + 1]; arr2[jj + 1] = temp; } } } Int32 [] tempA = new Int32[arr1.Length + arr2.Length]; Int32 count = 0; Int32 i = 0, j = 0 ,k; for (k = 0; k < tempA.Length; k++) { if (i >= arr1.Length || j >= arr2.Length) break; if (arr1[i] < arr2[j]) { if (k > 0 && tempA[k - 1] == arr1[i]) { i++; k--; } else { tempA[k] = arr1[i]; i++; count++; } } else { if (k > 0 && tempA[k - 1] == arr2[j]) { j++; k--; } else { tempA[k] = arr2[j]; j++; count++; } } } if (i == arr1.Length) { while (j < arr2.Length) { if (arr2[j] != tempA[k - 1]) { tempA[k] = arr2[j]; k++; count++; } j++; } } if (j == arr2.Length) { while (i < arr1.Length) { if (arr1[i] != tempA[k - 1]) { tempA[k] = arr1[i]; k++; count++; } i++; } } Int32[] result = new Int32[count]; for (i = 0; i < result.Length; i++) result[i] = tempA[i]; foreach (Int32 item in result) Console.WriteLine(item); 呵呵,不能用.net类库,array,list之类的一律不准用请问 你如何存放数据,难道用string存放数据,那char[],string算不算用net类库了呢?? using System; namespace SelectionSorter { public class SelectionSorter { private int min; public void Sort(int [] list) { for(int i=0;i< list.Length-1;i++) { min=i; for(int j=i+1;j< list.Length;j++) { if(list[j]< list[min]) min=j; } int t=list[min]; list[min]=list; list=t; } } } public class MainClass { public static void Main() { int[] iArrary = new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47}; SelectionSorter ss=new SelectionSorter(); ss.Sort(iArrary); for (int m=0;m< iArrary.Length;m++) Console.Write("{0} ",iArrary[m]); Console.WriteLine(); } } } 不知道linq是否可以,参考linq to object 晕死,不用ArrayList,麻烦很多private void button10_Click(object sender, EventArgs e){ int[] a = new int[] {1,3,3,5,9,5,2 }; int[] b = new int[] {6,2,5,4,8,0,5 }; int[] c=new int[a.Length+b.Length]; a.CopyTo(c,0); b.CopyTo(c, a.Length); //快速排序 QuickSort(c, 0, c.Length - 1); int nCount = 0;//重复元素个数 for (int i = 1; i < c.Length;i++) { if(c[i]==c[i-1]) { nCount = nCount + 1; } } int[] result = new int[c.Length - nCount]; int tmp = 0; for (int i = 1; i < c.Length;i++) { if(c[i]!=c[i-1]) { result[tmp] = c[i]; Console.Write(result[tmp] + " "); tmp++; } } //结果:1 2 3 4 5 6 8 9}public void QuickSort(int[] a,int low,int high){ if(low>=high) { return; } int pivot = 0; pivot = Partition(a, low, high); QuickSort(a, 0, pivot - 1); QuickSort(a, pivot + 1, high);}public int Partition(int[] a,int low,int high){ int tmp = a[low];//基准值 while(low<high) { //从右开始找第一个,小于基准值的数。如果大于基准值的话,向左移high-- if (low < high && a[high]>=tmp) { high=high -1; } a[low] = a[high]; if (low < high && a[low]<=tmp) { low=low + 1; } a[high] = a[low]; } a[low]=tmp;//在这里 low=hight return low;} //上面的忽然发现第一项没了 修改了一下private void button10_Click(object sender, EventArgs e){ int[] a = new int[] {1,3,3,5,9,5,2 }; int[] b = new int[] {6,2,5,4,8,0,5 }; int[] c=new int[a.Length+b.Length]; a.CopyTo(c,0); b.CopyTo(c, a.Length); //快速排序 QuickSort(c, 0, c.Length - 1); int nCount = 0;//重复元素个数 for (int i = 1; i < c.Length;i++) { if(c[i]==c[i-1]) { nCount = nCount + 1; } } int[] result = new int[c.Length - nCount]; int tmp = 0; for (int i = 1; i < c.Length;i++) { if(i==1 && c[0]!=c[1])//单独处理c[0],否则第一项就没了 { result[tmp] = c[0]; Console.Write(result[tmp] + " "); tmp++; } if(c[i]!=c[i-1]) { result[tmp] = c[i]; Console.Write(result[tmp] + " "); tmp++; } } //结果: 0 1 2 3 4 5 6 8 9} 对架构上的迷惑,有实际应用的帮解释下 Framework v1.1.4322安装问题 如何提高asp.net网站的访问速度 在服务器端添加客户端教本....?? 我写了一个图片采集,来截取网页里图片下载到本地,一个网页有里多个图怎么办? 请教.为什么装了.Net FrameWork后asp页不能显示了? 关于datalist这几句什么意思啊??? 在IE中托管.NET Windows Forms 100 批量生成10W组账号 ASP.NET中如何让一页产即过期? 求一个正则表达式,提取href内容 C#读取word2003的书签内容
用数组就要用Array
{
if (SLm == null || SLn == null)
{
throw new ArgumentException("传入数组不能为空");
} int[] result = new int[SLm.Length + SLn.Length];
int mIndex = 0;
int nIndex = 0;
for (int index = 0; index < result.Length; index++)
{
if (mIndex >= SLm.Length && nIndex >= SLn.Length)
{
break;
} if (mIndex >= SLm.Length)
{
result[index] = SLn[nIndex++];
continue;
} if (nIndex >= SLn.Length)
{
result[index] = SLm[mIndex++];
continue;
} if (SLm[mIndex] < SLn[nIndex])
{
result[index] = SLm[mIndex++];
}
else if (SLm[mIndex] > SLn[nIndex])
{
result[index] = SLn[nIndex++];
}
else
{
result[index] = SLm[mIndex++];
nIndex++;
}
}
return result;
}
Int32[] arr2 = new Int32[] { 45,34,23,7,3};
Int32 temp; for (Int32 ii = 1; ii < arr1.Length; ii++)
{
for (Int32 jj = 0; jj < arr1.Length - ii; jj++)
{
if (arr1[jj] > arr1[jj + 1])
{
temp = arr1[jj];
arr1[jj] = arr1[jj + 1];
arr1[jj + 1] = temp;
}
}
} for (Int32 ii = 1; ii < arr2.Length; ii++)
{
for (Int32 jj = 0; jj < arr2.Length - ii; jj++)
{
if (arr2[jj] > arr2[jj + 1])
{
temp = arr2[jj];
arr2[jj] = arr2[jj + 1];
arr2[jj + 1] = temp;
}
}
} Int32 [] tempA = new Int32[arr1.Length + arr2.Length];
Int32 count = 0;
Int32 i = 0, j = 0 ,k;
for (k = 0; k < tempA.Length; k++)
{
if (i >= arr1.Length || j >= arr2.Length)
break;
if (arr1[i] < arr2[j])
{
if (k > 0 && tempA[k - 1] == arr1[i])
{
i++;
k--;
}
else
{
tempA[k] = arr1[i];
i++;
count++;
}
}
else
{
if (k > 0 && tempA[k - 1] == arr2[j])
{
j++;
k--;
}
else
{
tempA[k] = arr2[j];
j++;
count++;
}
}
}
if (i == arr1.Length)
{
while (j < arr2.Length)
{
if (arr2[j] != tempA[k - 1])
{
tempA[k] = arr2[j];
k++;
count++;
}
j++;
}
}
if (j == arr2.Length)
{
while (i < arr1.Length)
{
if (arr1[i] != tempA[k - 1])
{
tempA[k] = arr1[i];
k++;
count++;
}
i++;
}
} Int32[] result = new Int32[count];
for (i = 0; i < result.Length; i++)
result[i] = tempA[i]; foreach (Int32 item in result)
Console.WriteLine(item);
请问 你如何存放数据,难道用string存放数据,那char[],string算不算用net类库了呢??
{
public class SelectionSorter
{
private int min;
public void Sort(int [] list)
{
for(int i=0;i< list.Length-1;i++)
{
min=i;
for(int j=i+1;j< list.Length;j++)
{
if(list[j]< list[min])
min=j;
}
int t=list[min];
list[min]=list;
list=t;
}
}
} public class MainClass
{
public static void Main()
{
int[] iArrary = new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
SelectionSorter ss=new SelectionSorter();
ss.Sort(iArrary);
for (int m=0;m< iArrary.Length;m++)
Console.Write("{0} ",iArrary[m]);
Console.WriteLine();
}
}
}
参考linq to object
{
int[] a = new int[] {1,3,3,5,9,5,2 };
int[] b = new int[] {6,2,5,4,8,0,5 };
int[] c=new int[a.Length+b.Length];
a.CopyTo(c,0);
b.CopyTo(c, a.Length); //快速排序
QuickSort(c, 0, c.Length - 1); int nCount = 0;//重复元素个数
for (int i = 1; i < c.Length;i++)
{
if(c[i]==c[i-1])
{
nCount = nCount + 1;
}
}
int[] result = new int[c.Length - nCount];
int tmp = 0;
for (int i = 1; i < c.Length;i++)
{
if(c[i]!=c[i-1])
{
result[tmp] = c[i];
Console.Write(result[tmp] + " ");
tmp++;
}
}
//结果:1 2 3 4 5 6 8 9
}public void QuickSort(int[] a,int low,int high)
{
if(low>=high)
{
return;
}
int pivot = 0;
pivot = Partition(a, low, high);
QuickSort(a, 0, pivot - 1);
QuickSort(a, pivot + 1, high);
}
public int Partition(int[] a,int low,int high)
{
int tmp = a[low];//基准值
while(low<high)
{
//从右开始找第一个,小于基准值的数。如果大于基准值的话,向左移high--
if (low < high && a[high]>=tmp)
{
high=high -1;
}
a[low] = a[high];
if (low < high && a[low]<=tmp)
{
low=low + 1;
}
a[high] = a[low];
}
a[low]=tmp;//在这里 low=hight
return low;
}
//上面的忽然发现第一项没了 修改了一下private void button10_Click(object sender, EventArgs e)
{
int[] a = new int[] {1,3,3,5,9,5,2 };
int[] b = new int[] {6,2,5,4,8,0,5 };
int[] c=new int[a.Length+b.Length];
a.CopyTo(c,0);
b.CopyTo(c, a.Length); //快速排序
QuickSort(c, 0, c.Length - 1); int nCount = 0;//重复元素个数
for (int i = 1; i < c.Length;i++)
{
if(c[i]==c[i-1])
{
nCount = nCount + 1;
}
}
int[] result = new int[c.Length - nCount];
int tmp = 0;
for (int i = 1; i < c.Length;i++)
{
if(i==1 && c[0]!=c[1])//单独处理c[0],否则第一项就没了
{
result[tmp] = c[0];
Console.Write(result[tmp] + " ");
tmp++;
}
if(c[i]!=c[i-1])
{
result[tmp] = c[i];
Console.Write(result[tmp] + " ");
tmp++;
}
}
//结果: 0 1 2 3 4 5 6 8 9
}