Skip to content

Commit 4603442

Browse files
committed
added chapter 10
1 parent ee46969 commit 4603442

3 files changed

+104
-2
lines changed

chapter10/01-SortingAlgorithms.js

100644100755
+92
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,96 @@ function ArrayList(){
7676
array[j] = temp;
7777
}
7878
};
79+
80+
this.mergeSort = function(){
81+
array = mergeSortRec(array);
82+
};
83+
84+
var mergeSortRec = function(array){
85+
86+
var length = array.length;
87+
88+
if(length === 1) {
89+
return array;
90+
}
91+
92+
var mid = Math.floor(length / 2),
93+
left = array.slice(0, mid),
94+
right = array.slice(mid, length);
95+
96+
return merge(mergeSortRec(left), mergeSortRec(right));
97+
};
98+
99+
var merge = function(left, right){
100+
var result = [],
101+
il = 0,
102+
ir = 0;
103+
104+
while(il < left.length && ir < right.length) {
105+
106+
if(left[il] < right[ir]) {
107+
result.push(left[il++]);
108+
} else{
109+
result.push(right[ir++]);
110+
}
111+
}
112+
113+
while (il < left.length){
114+
result.push(left[il++]);
115+
}
116+
117+
while (ir < right.length){
118+
result.push(right[ir++]);
119+
}
120+
121+
return result;
122+
};
123+
124+
this.quickStort = function(){
125+
quick(array, 0, array.length - 1);
126+
};
127+
128+
var partition = function(array, left, right) {
129+
130+
var pivot = array[Math.floor((right + left) / 2)],
131+
i = left,
132+
j = right;
133+
134+
while (i <= j) {
135+
while (array[i] < pivot) {
136+
i++;
137+
}
138+
139+
while (array[j] > pivot) {
140+
j--;
141+
}
142+
143+
if (i <= j) {
144+
swap(array, i, j);
145+
i++;
146+
j--;
147+
}
148+
}
149+
150+
return i;
151+
};
152+
153+
var quick = function(array, left, right){
154+
155+
var index;
156+
157+
if (array.length > 1) {
158+
159+
index = partition(array, left, right);
160+
161+
if (left < index - 1) {
162+
quick(array, left, index - 1);
163+
}
164+
165+
if (index < right) {
166+
quick(array, index, right);
167+
}
168+
}
169+
return array;
170+
};
79171
}

chapter10/02-TestingSortingAlgorithms.html chapter10/02-UsingSortingAlgorithms.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
</head>
77
<body>
88
<script type="text/javascript" src="01-SortingAlgorithms.js"></script>
9-
<script type="text/javascript" src="02-TestingSortingAlgorithms.js"></script>
9+
<script type="text/javascript" src="02-UsingSortingAlgorithms.js"></script>
1010
</body>
1111
</html>

chapter10/02-TestingSortingAlgorithms.js chapter10/02-UsingSortingAlgorithms.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,17 @@ array = createNonSortedArray();
5454

5555
console.log(array.toString());
5656

57-
array.insertionSort();
57+
array.mergeSort();
58+
59+
console.log(array.toString());
60+
61+
console.log('********** Quick Sort **********');
62+
63+
array = createNonSortedArray();
64+
65+
console.log(array.toString());
66+
67+
array.mergeSort();
5868

5969
console.log(array.toString());
6070

0 commit comments

Comments
 (0)