770 likes | 970 Views
Mergesort. Mergesort: Algorithm. void mergesort( Elem[] a, Elem[] temp, int Ieft, int right ) { int I, j, k, mid = (left+right)/2 if( left == right ) return mergesort( a, temp, left, mid); mergesort( a, temp, mid+1, right); // do the merge operation
E N D
Mergesort: Algorithm void mergesort( Elem[] a, Elem[] temp, int Ieft, int right ) { int I, j, k, mid = (left+right)/2 if( left == right ) return mergesort( a, temp, left, mid); mergesort( a, temp, mid+1, right); // do the merge operation for( i = left; i <= mid; i++ ) temp[i] = a[i] for( j = 1; j <= right-mid; j++ ) temp[ right-j+1 ] = a[ j+mid ] // merge sublists back to array for( i=left, j=right, k=left; k<=right; k++ ) { if( temp[ i ] < temp[ j ] ) a[k] = temp[ i++ ] else a[k]=temp[ j-- ] } }
Mergesort: Illustration 85 24 63 45 17 31 96 50
Mergesort: Illustration 85 24 63 45 17 31 96 50
Mergesort: Illustration 17 31 96 50 85 24 63 45
Mergesort: Illustration 17 31 96 50 63 45 85 24
Mergesort: Illustration 17 31 96 50 63 45 85 24
Mergesort: Illustration 17 31 96 50 24 63 45 85
Mergesort: Illustration 17 31 96 50 24 63 45 85
Mergesort: Illustration 17 31 96 50 24 85 63 45
Mergesort: Illustration 17 31 96 50 24 85 63 45
Mergesort: Illustration 17 31 96 50 24 85 63 45
Mergesort: Illustration 17 31 96 50 24 85 63 45
Mergesort: Illustration 17 31 96 50 24 85 63 45
Mergesort: Illustration 17 31 96 50 24 85 45 63
Mergesort: Illustration 17 31 96 50 24 85 45 63
Mergesort: Illustration 17 31 96 50 24 85 45 63
Mergesort: Illustration 17 31 96 50 24 85 45 63
Mergesort: Illustration 17 31 96 50 24 85 45 63
Mergesort: Illustration 17 31 96 50 2485 4563
Mergesort: Illustration 24 17 31 96 50 85 4563
Mergesort: Illustration 24 17 31 96 50 85 4563
Mergesort: Illustration 24 45 17 31 96 50 85 63
Mergesort: Illustration 2445 17 31 96 50 85 63
Mergesort: Illustration 2445 63 17 31 96 50 85
Mergesort: Illustration 244563 17 31 96 50 85
Mergesort: Illustration 24456385 17 31 96 50
Mergesort: Illustration 24 45 63 85 17 31 96 50
Mergesort: Illustration 24 45 63 85 17 31 96 50
Mergesort: Illustration 24 45 63 85 17 31 96 50
Mergesort: Illustration 24 45 63 85 96 50 17 31
Mergesort: Illustration 24 45 63 85 96 50 17 31
Mergesort: Illustration 24 45 63 85 17 96 50 31
Mergesort: Illustration 24 45 63 85 17 96 50 31
Mergesort: Illustration 24 45 63 85 17 31 96 50
Mergesort: Illustration 24 45 63 85 17 31 96 50
Mergesort: Illustration 24 45 63 85 17 31 96 50
Mergesort: Illustration 24 45 63 85 17 31 96 50
Mergesort: Illustration 24 45 63 85 17 31 96 50
Mergesort: Illustration 24 45 63 85 17 31 50 96
Mergesort: Illustration 24 45 63 85 17 31 50 96
Mergesort: Illustration 24 45 63 85 17 31 50 96
Mergesort: Illustration 24 45 63 85 17 31 50 96
Mergesort: Illustration 24 45 63 85 17 31 50 96
Mergesort: Illustration 24 45 63 85 1731 5096
Mergesort: Illustration 24 45 63 85 17 31 5096
Mergesort: Illustration 24 45 63 85 17 31 5096
Mergesort: Illustration 24 45 63 85 17 31 5096
Mergesort: Illustration 24 45 63 85 1731 5096
Mergesort: Illustration 24 45 63 85 173150 96