Bonsoir tout le monde,
j'aimerais comprendre le fonctionnement de la récursivité multiple utilisé dans l'algorithme de tri par fusion.
voici un code java implantant le tri par fusion(merge sort):
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 
package sort;
public class MergeSort {
	 int data[];
 
    public MergeSort() {
    	data= new int []{7,5,3,2,0,6,1,4};
	}
 
	public void sort(){
		sortArray(0,data.length-1);
	}
	private void sortArray(int low, int high){
		if((high - low)>=1){
			int m = (low + high)/2;
			sortArray(low,m);
			sortArray(m+1,high);
			merge(low,m,high);
 
		}
 
	}
 
	private void merge(int l,int m, int h ){
		int [] temp= new int[data.length];
		int left = l;
		int right=m+1;
		int index =l;
		//System.out.println("merging");
		while((left<=m)&&(right <= h) ){
		if(data[right]<data[left]){
			temp[index++]=data[right++];
		}
		else{
			temp[index++]=data[left++];
 
		}
		}
		if(left==m+1){
		while(right<=h)
			temp[index++]=data[right++];
		}
		else
		{
			while(left<=m)
				temp[index++]=data[left++];
		}
 
 
		for(int i=l;i<=h;i++)
			data[i]=temp[i];
	}
 
 
	public void afficher(int l, int h){
		for(int k=l;k<h;k++)
		System.out.print(data[k]+" ");
		System.out.println();
	}
 
	public static void main(String[] args) {
		MergeSort ms = new MergeSort();
		ms.afficher(0,ms.data.length);
		ms.sort();
		System.out.println("fin tri ...!");
		ms.afficher(0,ms.data.length);
		}
 
 
}
j'ai fait l'exécution pas à pas (debug), mais malheureusement je n'arrive pas à comprendre les appels récursives. si quelqu'un à une idée comment dérouler ce programme.
merci d'avance