Skip to content

Commit c14829b

Browse files
committed
added chapter 10
1 parent 648abe9 commit c14829b

File tree

2 files changed

+57
-21
lines changed

2 files changed

+57
-21
lines changed

chapter10/01-SortingAlgorithms.js

+29-12
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,47 @@ function ArrayList(){
2525

2626
for (var i=0; i<length; i++){
2727
for (var j=0; j<length-1; j++ ){
28+
console.log('compare ' + array[j] + ' with ' + array[j+1]);
2829
if (array[j] > array[j+1]){
30+
console.log('swap ' + array[j] + ' with ' + array[j+1]);
2931
swap(j, j+1);
3032
}
3133
}
3234
}
3335
};
3436

3537
this.modifiedBubbleSort = function(){
36-
var length = array.length,
37-
swapped;
38-
39-
do {
40-
swapped = false;
41-
for (var i=0; i<length-1; i++){
42-
if (array[i] > array[i+1]){
43-
swap(i, i+1);
44-
swapped = true;
38+
var length = array.length;
39+
40+
for (var i=0; i<length; i++){
41+
for (var j=0; j<length-1-i; j++ ){
42+
console.log('compare ' + array[j] + ' with ' + array[j+1]);
43+
if (array[j] > array[j+1]){
44+
console.log('swap ' + array[j] + ' with ' + array[j+1]);
45+
swap(j, j+1);
4546
}
4647
}
47-
} while (swapped);
48+
}
4849

4950
};
5051

5152
this.selectionSort = function(){
5253
var length = array.length,
5354
indexMin;
5455

55-
for (var i=0; i<length; i++){
56+
for (var i=0; i<length-1; i++){
5657
indexMin = i;
58+
console.log('index ' + array[i]);
5759
for (var j=i; j<length; j++){
5860
if(array[indexMin]>array[j]){
61+
console.log('new index min ' + array[j]);
5962
indexMin = j;
6063
}
6164
}
62-
swap(i, indexMin);
65+
if (i !== indexMin){
66+
console.log('swap ' + array[i] + ' with ' + array[indexMin]);
67+
swap(i, indexMin);
68+
}
6369
}
6470
};
6571

@@ -69,10 +75,13 @@ function ArrayList(){
6975
for (var i=1; i<length; i++){
7076
j = i;
7177
temp = array[i];
78+
console.log('to be inserted ' + temp);
7279
while (j>0 && array[j-1] > temp){
80+
console.log('shift ' + array[j-1]);
7381
array[j] = array[j-1];
7482
j--;
7583
}
84+
console.log('insert ' + temp);
7685
array[j] = temp;
7786
}
7887
};
@@ -86,6 +95,7 @@ function ArrayList(){
8695
var length = array.length;
8796

8897
if(length === 1) {
98+
console.log(array);
8999
return array;
90100
}
91101

@@ -118,6 +128,8 @@ function ArrayList(){
118128
result.push(right[ir++]);
119129
}
120130

131+
console.log(result);
132+
121133
return result;
122134
};
123135

@@ -131,16 +143,21 @@ function ArrayList(){
131143
i = left,
132144
j = right;
133145

146+
console.log('pivot is ' + pivot + '; left is ' + left + '; right is ' + right);
147+
134148
while (i <= j) {
135149
while (array[i] < pivot) {
136150
i++;
151+
console.log('i = ' + i);
137152
}
138153

139154
while (array[j] > pivot) {
140155
j--;
156+
console.log('j = ' + j);
141157
}
142158

143159
if (i <= j) {
160+
console.log('swap ' + array[i] + ' with ' + array[j]);
144161
swapQuickStort(array, i, j);
145162
i++;
146163
j--;

chapter10/02-UsingSortingAlgorithms.js

+28-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
function createNonSortedArray(){
1+
function createNonSortedArray(size){
22
var array = new ArrayList();
33

4-
for (var i = 5; i> 0; i--){
4+
for (var i = size; i> 0; i--){
55
array.insert(i);
66
}
77

88
return array;
99
}
1010

11+
function createRandomNonSortedArray(){
12+
var array = new ArrayList();
13+
14+
array.insert(3);
15+
array.insert(5);
16+
array.insert(1);
17+
array.insert(4);
18+
array.insert(2);
19+
20+
return array;
21+
}
22+
1123
console.log('********** Bubble Sort **********');
1224

13-
var array = createNonSortedArray();
25+
var array = createNonSortedArray(5);
1426

1527
console.log(array.toString());
1628

@@ -20,7 +32,7 @@ console.log(array.toString());
2032

2133
console.log('********** Modified Bubble Sort **********');
2234

23-
array = createNonSortedArray();
35+
array = createNonSortedArray(5);
2436

2537
console.log(array.toString());
2638

@@ -30,7 +42,7 @@ console.log(array.toString());
3042

3143
console.log('********** Selection Sort **********');
3244

33-
array = createNonSortedArray();
45+
array = createNonSortedArray(5);
3446

3547
console.log(array.toString());
3648

@@ -40,7 +52,7 @@ console.log(array.toString());
4052

4153
console.log('********** Insertion Sort **********');
4254

43-
array = createNonSortedArray();
55+
array = createRandomNonSortedArray();
4456

4557
console.log(array.toString());
4658

@@ -50,7 +62,7 @@ console.log(array.toString());
5062

5163
console.log('********** Merge Sort **********');
5264

53-
array = createNonSortedArray();
65+
array = createNonSortedArray(8);
5466

5567
console.log(array.toString());
5668

@@ -59,8 +71,15 @@ array.mergeSort();
5971
console.log(array.toString());
6072

6173
console.log('********** Quick Sort **********');
62-
63-
array = createNonSortedArray();
74+
array = new ArrayList();
75+
76+
array.insert(3);
77+
array.insert(5);
78+
array.insert(1);
79+
array.insert(6);
80+
array.insert(4);
81+
array.insert(7);
82+
array.insert(2);
6483

6584
console.log(array.toString());
6685

0 commit comments

Comments
 (0)