-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
132 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @desc 插入排序 | ||
* @param {Array} arr | ||
*/ | ||
function insertSort(arr) { | ||
var i = 1, | ||
len = arr.length, | ||
key; | ||
|
||
for (; i < len; i++) { | ||
var j = i; | ||
var key = arr[j]; | ||
while (--j > -1) { | ||
if (arr[j] > key) { | ||
arr[j + 1] = arr[j]; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
arr[j + 1] = key; | ||
} | ||
|
||
return arr; | ||
} | ||
|
||
module.exports = insertSort; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @desc 快速排序,是对冒泡排序的一种改进 | ||
* @param {Array} arr | ||
*/ | ||
function quickSort(arr) { | ||
if (arr.length <= 1) return arr; | ||
var pivotIndex = Math.floor(arr.length / 2); | ||
var pivot = arr.splice(pivotIndex, 1)[0]; | ||
|
||
var left = []; | ||
var right = []; | ||
for (i = 0; i < arr.length; i++) { | ||
if (arr[i] < pivot) { | ||
left.push(arr[i]); | ||
} else { | ||
right.push(arr[i]); | ||
} | ||
} | ||
|
||
return quickSort(left).concat([pivot], quickSort(right)); | ||
} | ||
|
||
module.exports = quickSort; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @desc 插入排序 | ||
* @param {Array} arr | ||
*/ | ||
function insertSort(arr) { | ||
var i = 1, | ||
len = arr.length, | ||
key; | ||
|
||
for (; i < len; i++) { | ||
var j = i; | ||
var key = arr[j]; | ||
while (--j > -1) { | ||
if (arr[j] > key) { | ||
arr[j + 1] = arr[j]; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
arr[j + 1] = key; | ||
} | ||
|
||
return arr; | ||
} | ||
|
||
module.exports = insertSort; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @desc 快速排序,是对冒泡排序的一种改进 | ||
* @param {Array} arr | ||
*/ | ||
function quickSort(arr) { | ||
if (arr.length <= 1) return arr; | ||
var pivotIndex = Math.floor(arr.length / 2); | ||
var pivot = arr.splice(pivotIndex, 1)[0]; | ||
|
||
var left = []; | ||
var right = []; | ||
for (i = 0; i < arr.length; i++) { | ||
if (arr[i] < pivot) { | ||
left.push(arr[i]); | ||
} else { | ||
right.push(arr[i]); | ||
} | ||
} | ||
|
||
return quickSort(left).concat([pivot], quickSort(right)); | ||
} | ||
|
||
module.exports = quickSort; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
describe("sort arithemetic", function() { | ||
describe("bubbleSort()", function() { | ||
it("g_arithemetic.bubbleSort([1,2,5,3,6]) should return [1,2,3,5,6]", function() { | ||
assert(g_arithemetic.arrayEqual(g_arithemetic.bubbleSort([1, 2, 5, 3, 6]),[1, 2, 3, 5, 6])); | ||
assert(g_arithemetic.arrayEqual(g_arithemetic.bubbleSort([1, 2, 5, 3, 6]), [1, 2, 3, 5, 6])); | ||
}); | ||
}); | ||
describe("insertSort()", function() { | ||
it("g_arithemetic.insertSort([1,2,5,3,6]) should return [1,2,3,5,6]", function() { | ||
assert(g_arithemetic.arrayEqual(g_arithemetic.insertSort([1, 2, 5, 3, 6]), [1, 2, 3, 5, 6])); | ||
}); | ||
}); | ||
describe("quickSort()", function() { | ||
it("g_arithemetic.quickSort([1,2,5,3,6]) should return [1,2,3,5,6]", function() { | ||
assert(g_arithemetic.arrayEqual(g_arithemetic.quickSort([1, 2, 5, 3, 6]), [1, 2, 3, 5, 6])); | ||
}); | ||
}); | ||
}); |