0% found this document useful (0 votes)
4 views2 pages

Algo 11

Uploaded by

Yousif Hazim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Algo 11

Uploaded by

Yousif Hazim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Algorithms

Analysis and Design


from scratch
‫ ﻣﻦ ﺗﺤﺖ‬Algorithms ‫ﲓ‬ ‫ﺗﺤﻠﻴﻞ وﺗ‬

‫أ حمد تم ول ي‬
arr start end
void mergeSort(int[] arr, int start, int end) { [9, 5, 1, 4] 0 3
if (start < end) {
int midpoint = (start + end) / 2; midpoint arr start end
[9, 5, 1, 4] 0 1
mergeSort(arr, start, midpoint); 1
mergeSort(arr, midpoint + 1, end); midpoint
0
merge(arr, start, midpoint, end); arr start end
[9, 5, 1, 4] 0 0
}
}
void merge(int[] arr, int start, int midpoint, int end) { arr start end
int i, j, k; [9, 5, 1, 4] 1 1
int left_length = midpoint - start + 1;
int right_length = end - midpoint; arr=[9, 5, 1, 4] start=0 mid=0 end=2
int[] LeftArray = new int[left_length]; left_length = 1 L=[5] R=[9]
int[] RightArray = new int[right_length]; right_length = 1 arr`=[5,9,1,4]

for (i = 0; i < left_length; i++) {


LeftArray[i] = arr[start + i];
} arr start end
[5, 9, 1, 4] 2 3
for (j = 0; j < right_length; j++) {
RightArray[j] = arr[midpoint + 1 + j]; midpoint
} 2 arr start end
i = 0; j = 0; k = start; [5, 9, 1, 4] 2 2
while (i < left_length && j < right_length) {
if (LeftArray[i] <= RightArray[j]) { arr start end
arr[k] = LeftArray[i]; i++; [5, 9, 1, 4] 3 3
} else {
arr[k] = RightArray[j]; j++;
arr=[5, 9, 1, 4] start=2 mid=2 end=3
}k++; left_length = 1 L=[1] R=[4]
} right_length = 1 arr`=[5,9,1,4]
while (i < left_length) {
arr[k] = LeftArray[i]; i++; k++;
}
while (j < right_length) { arr=[5, 9, 1, 4] start=0 mid=1 end=3
arr[k] = RightArray[j]; j++; k++; left_length = 2 L=[5,9] R=[1,4]
} right_length = 2 arr`=[1,4,5,9]
}

‫أ حمد تم ول ي‬ Algorithms Analysis and Design from scratch ‫ ﻣﻦ ﺗﺤﺖ‬Algorithms ‫ﲓ‬ ‫ﺗﺤﻠﻴﻞ وﺗ‬

You might also like