归并

public class HelloWorld {

public static <Display> void main(String[] args) {
// TODO Auto-generated method stub

System.out.println(“Hello world!”);
int a[] = {2,4,3,1,7,78,8,92,1,99};
MergeSort(a,10);

for (int i = 0; i < 10; i++)
System.out.println(“i :” + i + ” : “+a[i] );

int[] d = {1,3,5,7,9};
int[] e = {2,4,6,8,10};
int []f = arrangeArray(d,5,e,5);
for (int i = 0; i < 10; i++)
System.out.println(“f :” + i + ” : “+f[i] );

}

public static void mergeArray(int[] a,int first,int mid ,int last, int[] temp)
{

int i = first, j = mid + 1;
int m = mid, n = last;
int k = 0;

while (i <= m && j <= n)
{
if (a[i] <= a[j])
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}

while (i <= m)
temp[k++] = a[i++];

while (j <= n)
temp[k++] = a[j++];

for (i = 0; i < k; i++)
{
// System.out.println(“i :” + i + ” : “+temp[i] );
a[first + i] = temp[i];
}
}
public static void mergesort(int [] a, int first, int last, int [] temp)
{
//Console.WriteLine(” f :{0} : last : {1}”, first, last);
if (first < last)
{
int mid = (first + last) / 2;
mergesort(a, first, mid, temp); //左边有序
mergesort(a, mid + 1, last, temp); //右边有序

mergeArray(a, first, mid, last, temp); //再将二个有序数列合并
}
}
public static Boolean MergeSort(int []a, int n)
{
int[] p = new int[n];
if (n <= 0)
return false;
mergesort(a, 0, n – 1, p);

return true;
}

public static int[] arrangeArray(int[] a,int m,int[] b,int n)
{
int []c = new int[m+n];

int j=0,k=0;
int i = 0;

while(j<m && k <n)
{
if(a[j]<b[k])
{
c[i++] = a[j++];
}
else
{
c[i++] = b[k++];
}
}

while (j < m)
c[i++] = a[j++];

while (k < n)
c[i++] = b[k++];
return c;
}

}

 

 

//////////////////////////////

#include<iostream> using namespace std; /*合并有序数组,first-mid和mid-last。num为两个数组的总长度*/ void Merge_Array(int a[],int first,int mid,int last) { int num = last – first +1; int* temp = new int[num]; int i,j,k; i=first; j=mid+1; k = 0; while(i<=mid && j <=last) { if(a[i] > a[j]) temp[k++] = a[j++]; else temp[k++] = a[i++]; } while(i<=mid) temp[k++] = a[i++]; while(j<=last) temp[k++] = a[j++]; for(i=first;i<first+k;i++) { a[i] = temp[i-first]; } delete[]temp; } void Merge_Sort(int a[],int p,int q) { if(p < q) { int mid = (p+q)/2; Merge_Sort(a,p,mid); Merge_Sort(a,mid+1,q); Merge_Array(a,p,mid,q); } } void main() { int a[] = {57,68,59,52,72,28,96,33,24}; int count = sizeof(a) / sizeof(a[0]); Merge_Sort(a,0,count-1); for(int i=0;i<count;i++) { cout<<a[i]<<” “; } cout<<endl; }

Unity yield WaitForSeconds(1);

IEnumerator WaitFunction (float delay) {
float timer = Time.time + delay;
while (Time.time < timer) {
yield return null;
}
}
IEnumerator Start () {
yield return StartCoroutine(WaitFunction (1.0f));
print (“1”);
yield return StartCoroutine(WaitFunction (2.0f));
print (“2”);

}