forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merge_Sort.dart
125 lines (111 loc) · 2.71 KB
/
Merge_Sort.dart
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import 'dart:io';
class MergeSort {
List<int> array;
MergeSort(this.array);
void readArray(List<int> a, int n)
{
for (int i = 0; i < n; i++)
{
print('Add number to array : ');
var input = stdin.readLineSync();
int x = int.parse(input);
assert(x is int);
a.add(x);
}
}
void printArray(List<int> a)
{
for (var i = 0; i < a.length; i++)
{
print(a[i]);
}
}
void merge(List list, int leftIndex, int middleIndex, int rightIndex)
{
int leftSize = middleIndex - leftIndex + 1; // specifying the size of left array
int rightSize = rightIndex - middleIndex; // right array size
List leftList = new List(leftSize); // make the array
List rightList = new List(rightSize);
for (int i = 0; i < leftSize; i++)
{
leftList[i] = list[leftIndex + i];
} // making the arrays
for (int j = 0; j < rightSize; j++)
{
rightList[j] = list[middleIndex + j + 1];
}
int i = 0, j = 0;
int k = leftIndex;
while (i < leftSize && j < rightSize)
{
if (leftList[i] <= rightList[j])
{
list[k] = leftList[i];
i++;
}
else
{
list[k] = rightList[j];
j++;
}
k++;
}
while (i < leftSize)
{
list[k] = leftList[i];
i++;
k++;
}
while (j < rightSize)
{
list[k] = rightList[j];
j++;
k++;
}
}
void mergeSort(List list, int leftIndex, int rightIndex)
{
if (leftIndex < rightIndex)
{
int middleIndex = (rightIndex + leftIndex) ~/ 2;
mergeSort(list, leftIndex, middleIndex);
mergeSort(list, middleIndex + 1, rightIndex);
merge(list, leftIndex, middleIndex, rightIndex);
}
}
}
void main()
{
List<int> array = []; // new empty array
MergeSort sort = MergeSort(array); // pass it as argument
print('Enter number of elements:');
var input = stdin.readLineSync();
int n = int.parse(input);
sort.readArray(sort.array, n);
sort.mergeSort(array, 0, n-1); // use the objects' array to do the sort
print('Sorted array is:');
sort.printArray(sort.array); //print out the sorted array
}
/* Sample Input and Output
Enter number of elements:
6
Add number to array :
2
Add number to array :
5
Add number to array :
4
Add number to array :
1
Add number to array :
6
Add number to array :
3
Sorted array is:
1
2
3
4
5
6
*/