Skip to content

Commit 835f94a

Browse files
committed
update
1 parent b46ac1f commit 835f94a

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

README.md

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,16 +1676,16 @@ console.log(myNums.toString());
16761676
```JavaScript
16771677
16781678
function bubbleSort() {
1679-
var numElements = this.dataStore.length;
1680-
var temp;
1681-
for (var outer = numElements; outer >= 2; --outer) {
1682-
for (var inner = 0; inner <= outer - 1; ++inner ) {
1683-
if (this.dataStore[inner] > this.dataStore[inner + 1]) {
1684-
swap(this.dataStore,inner,inner + 1);
1679+
var numElements = this.dataStore.length; //获取数组长度
1680+
var temp;// 定义临时对象
1681+
for (var outer = numElements; outer >= 2; --outer) {//对元素遍历,直到最后剩2个数字
1682+
for (var inner = 0; inner <= outer - 1; ++inner ) {//从第零个开始,直到不满足
1683+
if (this.dataStore[inner] > this.dataStore[inner + 1]) {//如果,前一个大于后一个,
1684+
swap(this.dataStore,inner,inner + 1);//进行交换
16851685
}
16861686
}
16871687
}
1688-
}
1688+
}//通过比较把最大的放到最右面
16891689
```
16901690

16911691
* 使用bubbleSort()对10个数字排序
@@ -1708,10 +1708,46 @@ console.log(mynums.toString);
17081708

17091709
* 选择排序会用到嵌套循环。外循环从数组的第一个元素一定到倒数第二个元素;内循环从第二个数组元素移动到最后一个元素,查找比当前外循环
17101710

1711-
<p align="center"><img src="http://oe51jhwvd.bkt.clouddn.com/3.gif" /></p>
1711+
<p align="center"><img src="http://www.2cto.com/uploadfile/Collfiles/20160918/20160918092144584.gif" /></p>
17121712

17131713
```JavaScript
1714+
function selectionSort() {
1715+
var min,temp;
1716+
for (var outer = 0; outer <= this.dataStore.length-2; ++outer)//循环
1717+
min = outer;//定义第一个为最小
1718+
for (var inner = outer + 1; i < this.dataStore.length-1; ++inner) {//查找下一个中
1719+
if (this.dataStore[inner]<this.dataStore[min]) {//如果数组内的第inner项小于最小项
1720+
min = inner;//最小项设置为inner
1721+
}
1722+
swap(this.dataStore,outer,min);//交换最小值和outer
1723+
}
1724+
}
1725+
}
1726+
//找到数组中剩余的最小值,如果更小outer进行交换
1727+
```
1728+
1729+
## 08-04
1730+
### JavaScript算法-插入排序
1731+
1732+
* 插入排序
17141733

1734+
* 插入排序有两个循环,外循环将数组元素挨个移动,而内循环则对外循环中选中的元素及它后面的那个元素进行比较。如果外循环中选中的元素比内循环中选中的元素小,那么数组会向右移动,为内循环中的这个元素腾出位置。
1735+
1736+
<p align="center"><img src="http://www.2cto.com/uploadfile/Collfiles/20160918/20160918092144589.gif" /></p>
1737+
1738+
```JavaScript
1739+
function insertionSort() {
1740+
var temp,inner;
1741+
for (var outer = 1; outer <= this.dataStore.length - 1 ; ++outer) {
1742+
temp = this.dataStore[outer];
1743+
inner = outer;
1744+
while (inner>0 && (this.dataStore[inner-1] >= temp)) {
1745+
this.dataStore[inner] = this.dataStore[inner - 1];
1746+
--inner;
1747+
}
1748+
this.dataStore[inner] = temp;
1749+
}
1750+
}
17151751
```
17161752

17171753
## 09-01

0 commit comments

Comments
 (0)