Forum Informatica Unict

LAUREA TRIENNALE (D.M. 270/04) => Programmazione 2, 9 CFU => Topic started by: Daréios89 on 05-10-2010, 22:57:19



Title: Problema Merge-Sort
Post by: Daréios89 on 05-10-2010, 22:57:19
Dove sbaglio nell'algoritmo?

Code:
public class MergeSort
{

public static void main(String [] args)
{
int [] a={3,6,5,7,9,8,0,5};
MSort(a,0,a.length-1);
for(int i=0; i<a.length; i++)
System.out.print(a[i]+" ");

}


public static void merge(int [] a, int left,int center, int right)
{
int [] S=new int[a.length];
int i= left;
int j=center+1;
int k=0;


while(i<=center && j<=right)
{
if(a[i]<=a[j])
{
S[k++]=a[i++];
}
else{
S[k++]=a[j++];
}

}

while(i<=center)
{
S[k++]=a[i++];
}

while(j<=right)
{
S[k++]=a[j++];
}

k=left;
while(k<right)
a[k++]=S[k-left];

}



public static void MSort(int [] a,int left, int right)
{
if(left<right){
int center=(left+right)/2;
MSort(a,left,center);
MSort(a,center+1,right);
merge(a, left,center,right);
}


}
}


Title: Re:Problema Merge-Sort
Post by: Daréios89 on 12-10-2010, 16:02:17
Qualcuno sa aiutarmi?