Download presentation
Presentation is loading. Please wait.
Published byΑἴσων Αντωνοπούλου Modified over 5 years ago
1
אם גודל המערך arr גדול מ-1 בצע: מיון-מיזוג(left) מיון-מיזוג(right)
מזג את left ו-right לתוך arr
2
4 8 3 1 2 7 5
3
1 2 7 5 4 8 3
4
4 8 3 1 2 7 5
5
3 1 2 7 5 4 8
6
1 2 7 5 4 8 3
7
1 2 7 5 8 3 4
8
1 2 7 5 3 4 8
9
1 2 7 5 4 3 3 8
10
1 2 7 5 4 8 3 3
11
1 2 7 5 4 8 3 3
12
1 2 7 5 4 8 3 3
13
1 2 7 5 4 8 3
14
1 2 7 5 4 8 3
15
3 3 1 2 7 5 4 8 3
16
3 3 3 1 2 7 5 4 8
17
3 4 3 3 1 2 7 5 8
18
3 4 8 3 3 1 2 7 5
19
3 4 3 8 3 7 5 1 2
20
3 4 3 8 3 1 2 7 5
21
3 4 3 8 3 2 7 5 1
22
3 4 3 8 3 7 5 1 2
23
3 4 3 8 3 1 7 5 2
24
3 3 3 4 8 3 3 1 2 7 5
25
3 4 3 8 3 1 2 5 7
26
3 4 3 8 3 1 2 7 5
27
3 4 3 8 3 1 2 5 7
28
3 4 3 8 3 1 2 5 7
29
3 4 8 3 3 1 2 5 7
30
3 4 3 8 3 1 2 5 7
31
3 4 3 3 8 1 2 5 7
32
3 4 8 3 3 1 2 5 7
33
3 3 3 4 8 3 3 1 2 5 7
34
1 3 3 3 4 8 3 3 2 5 7
35
1 2 3 3 3 4 8 3 3 5 7
36
1 2 3 3 3 4 8 3 3 5 7
37
1 2 3 3 3 4 8 3 3 5 7
38
1 2 3 3 3 4 5 8 3 3 7
39
1 2 3 3 3 4 5 7 8 3 3
40
1 2 3 3 3 4 5 7 8 3 3
41
1 2 3 3 3 4 5 7 8 המערך ממוין 3 3
42
public static void mergeSort(int[] arr){
if (arr.length > 1){ int[] left = new int[arr.length/2]; System.arraycopy(arr, 0, left, 0, left.length); int[] right = new int[arr.length-left.length]; System.arraycopy(arr, left.length, right, 0, right.length); mergeSort(left); mergeSort(right); merge(left, right, arr); }
43
private static void merge(int[] left, int[] right, int[] whole){
int leftIndex = 0; int rightIndex = 0; int wholeIndex = 0; while (leftIndex < left.length && rightIndex < right.length){ if (left[leftIndex] < right[rightIndex]) { whole[wholeIndex] = left[leftIndex]; leftIndex++; } else { whole[wholeIndex] = right[rightIndex]; rightIndex++; wholeIndex++;
44
whole[wholeIndex] = left[leftIndex]; leftIndex++; wholeIndex++; }
while (leftIndex < left.length) whole[wholeIndex] = left[leftIndex]; leftIndex++; wholeIndex++; } while (rightIndex < right.length){ whole[wholeIndex] = right[rightIndex]; rightIndex++;
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.