http://www.cnblogs.com/skywang12345/p/3603935.html
c/c++/java什么的太麻烦。我用JavaScript实现了一部分。
1. suanfa.js 2.排序.html 3.bfs-dfs.html
/// 数据结构
/// 算法
//单链表 节点对象
var Node = function(newData){
this.next = null;
this.data = newData;
}
function linkList(){
this.head = null;
this.size = 0;
}
linkList.prototype.Add = function(newData){
this.size += 1;
var newNode = new Node(newData);
var tempNode = null;
if(this.head == null){
this.head = newNode;
}
else{
tempNode = this.head;
while(tempNode.next != null){
tempNode = tempNode.next;
}
tempNode.next = newNode;
}
}
linkList.prototype.AddAt = function(pos, newData){
var tempNode = this.head;
var t = new Node(newData);
var p = null;
if(pos > this.size || pos < 0){
return null;
}
this.size += 1;
if(pos == 0){
t.next = this.head;
this.head = t;
return null;
}
for(var i=pos-1;i > 0;i--){
tempNode = tempNode.next;
}
if(tempNode.next != null){
t.next = tempNode.next;
}
tempNode.next = t;
}
linkList.prototype.GetData = function(pos){
var tempNode = this.head;
if(pos >= this.size || pos < 0){
return null;
}
else{
for(i=pos;i > 0;i--){
if(tempNode.next != null){
tempNode = tempNode.next;
}
else{
return null;
}
}
return tempNode.data;
}
}
linkList.prototype.RemoveAt = function(pos){
var tempNode = this.head;
if(pos >= this.size || pos < 0){
return null;
}
this.size -= 1;
if( pos == 0 ){
this.head = this.head.next;
return this.head;
}
for(var i = pos; i > 0;i--){
tempNode = tempNode.next;
}
if(tempNode.next != null){
tempNode.next = tempNode.next.next;
}
else{
tempNode.next = null;
}
return tempNode.next;
}
linkList.prototype.Print = function(){
var tempNode = this.head;
while(tempNode != null){
console.log(tempNode.data);
tempNode = tempNode.next;
}
console.log("end!");
}
linkList.prototype.Reverse = function(){
var first = null, second =null, third =null;
if(this.head == null || this.head.next == null){
return null;
}
first = this.head;
second = first.next;
while(null != second){
third = second.next;
second.next = first;
first = second;
second = third;
}
this.head.next = null;
this.head = first;
}
linkList.prototype.Destroy = function(){
var tempNode = this.head;
var t = null;
if(tempNode == null){
return null;
}
while(tempNode.next !=null){
this.size--;
t = tempNode.next;
tempNode = tempNode.next;
t = null;
}
this.head = null;
this.size--;
}
//深度优先搜索 广度优先搜索
//栈类
function Stack(){
this.st = [];
}
Stack.prototype.push = function(i){
this.st.push(i);
}
Stack.prototype.pop = function(){
return this.st.pop();
}
Stack.prototype.peek = function(){
return this.st[this.st.length-1];
}
Stack.prototype.isEmpty = function(){
return this.st.length==0;
}
//队列类 先进先出 FIFO
function Queue(){
this.queueArr = [];
}
Queue.prototype.insert = function(i){
this.queueArr.push(i);
}
Queue.prototype.remove = function(){
return this.queueArr.shift();
}
Queue.prototype.isEmpty = function(){
return this.queueArr.length==0;
}
//顶点类
function TopPoint(o){
this.label = o;
this.isVisited = false;
}
//图类
function Graph(){
this.MAX_TopPoint = 20;
this.TopPoint = [];//TopPoint[]
this.adjMat = [[]];
this.nTopPoint = 0;
this.Stack = new Stack();
this.Queue = new Queue();
}
//增加一个顶点
Graph.prototype.addTopPoint = function(o){
this.TopPoint.push(new TopPoint(o));
this.nTopPoint++;
this.adjMat.push([]);
for(var i=0;i < this.adjMat.length;i++){
this.adjMat[i].push([]);
}
}
//增加一条边
Graph.prototype.addEdge = function(start,end){
this.adjMat[start][end]=1;
this.adjMat[end][start]=1;
}
Graph.prototype.print = function(i){
console.log(this.TopPoint[i]);
}
Graph.prototype.getAdjUnVisitedPoint = function (i){
for(var j=0;j < this.nTopPoint;j++){
if(this.adjMat[i][j]==1 && this.TopPoint[j].isVisited==false){
return j;
}
}
return -1;
}
//深度优先搜索
Graph.prototype.dfs = function (){
var v = null;
this.TopPoint[0].isVisited = true;
this.print(0);
this.Stack.push(0);
while(!this.Stack.isEmpty()){
v = this.getAdjUnVisitedPoint(this.Stack.peek());
if(v==-1){
this.Stack.pop();
}
else{
this.TopPoint[v].isVisited = true;
this.print(v);
this.Stack.push(v);
}
}
for(var i=0;i < this.nTopPoint;i++){
this.TopPoint[i].isVisited = false;
}
}
//广度优先搜索
Graph.prototype.bfs = function (){
var v1 = null, v2 =null;
this.TopPoint[0].isVisited = true;
this.print(0);
this.Queue.insert(0);
while(!this.Queue.isEmpty()){
v1 = this.Queue.remove();
while((v2=this.getAdjUnVisitedPoint(v1))!=-1){
this.TopPoint[v2].isVisited = true;
this.print(v2);
this.Queue.insert(v2);
}
}
for(var j=0;j < this.nTopPoint;j++){
this.TopPoint[j].isVisited = false;
}
}
//冒泡排序
Array.prototype.BubbleSort = function(){
var n = this.length, temp = null, i=0, j=0;
for(i=0;i < n;i++){
for(j=i;j < n;j++){
if(this[i] > this[j]){
temp = this[i];
this[i]=this[j];
this[j]=temp;
}
}
}
}
//快速排序
Array.prototype.QuickSort = function(left,right){
var key = this[left],
low = left,
high= right;
if(left < right){
while(low < high){
while(low < high && this[high] > key){
high--;
}
this[low] = this[high];
while(low < high && this[low] < key){
low++;
}
this[high] = this[low];
}
this[low] = key;
this.QuickSort(left,low-1);
this.QuickSort(low+1,right);
}
}
//简单选择排序
Array.prototype.SelectSort = function(){
var n = this.length, i=0, j=0, temp=0, minPos = 0;
for(;i < n-1;++i){
minPos = i;
//查找最小值
for(j=i+1;j < n; j++){
if(this[j] < this[minPos]) minPos = j;
}
if(minPos!=i){
temp = this[i];
this[i]=this[minPos];
this[minPos]=temp;
}
}
}
//直接插入排序
Array.prototype.InsertSort = function(){
var n = this.length, temp = 0, j = 0, i=0;
for(i=1;i < n;i++){
temp = this[i];
for(j=i-1; j >=0 && temp < this[j];j--){
this[j+1] = this[j];
}
this[j+1]=temp;
}
}
//希尔排序
Array.prototype.ShellSort = function(){
var n = this.length, i, j, temp, d;
d = n/2;
//分成n/2组 1 2 3 4 5 6 n=3
while(d >= 1){
//对每组进行直接插入排序
for(i=d;i < n;i++){
temp = this[i];
for(j=i-d;j >= 0 && temp < this[j];j-=d){
this[j+d]=this[j];
}
this[j+d]=temp;
}
d= Math.floor(d/2);
}
}
Array.prototype.print = function(){
for(var i=0;i < this.length;i++){
console.log(this[i]);
}
}
//堆排序
Array.prototype.HeapAdjust = function(root, n){
var child = 2*root + 1, rightChild = 0, temp = 0;
//左孩子
if(child <= n - 1){
//有左孩子
rightChild = child + 1;
if(rightChild <= n -1){
//有右孩子
if(this[child] < this[rightChild]) child = rightChild;
}
if(this[root] < this[child]){
temp = this[child];
this[child] = this[root];
this[root] = temp;
this.HeapAdjust(child, n);
}
}
}
Array.prototype.HeapSort = function(){
var n = this.length, i = 0, temp = 0;
for(i = Math.floor(n/2) - 1;i >= 0;i--){
this.HeapAdjust(i, n);
}
for(i = n -1; i > 0; i--){
temp = this[0];
this[0] = this[i];
this[i] = temp;
this.HeapAdjust(0, i);
}
}
排序.html
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<meta charset="UTF-8" />
<meta name="Author" content="阮家友">
<meta name="Keywords" content="HTML,model,test">
<meta name="Description" content="special effect">
<meta name="time" content="2015-9-27 10:41:48">
<link rel="stylesheet" href="css/reset.css" type="text/css"/>
<style type="text/css">
#center {width:800px;margin:0 auto;padding:50px;}
</style>
</head>
<body>
<div id="center">
<p>模板</p>
<p>用于测试</p>
</div>
<script type="text/javascript" src="suanfa.js"></script>
<script type="text/javascript">
var arr1 = [6,7,2,3,8];
//arr1.BubbleSort();//ok
//arr1.print();
//arr1.SelectSort();//ok
//arr1.print();
//arr1.InsertSort();//ok
//arr1.print();
//arr1.ShellSort(); // failed d/=2 d是增量要取整
//arr1.print();
//arr1.QuickSort(0,arr1.length-1);//ok
//arr1.print();
arr1.HeapSort();
arr1.print();
</script>
</body>
</html>
3. bfs-dfs.html
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<meta charset="UTF-8" />
<meta name="Author" content="阮家友">
<meta name="Keywords" content="HTML,model,test">
<meta name="Description" content="special effect">
<meta name="time" content="2016-4-12 23:17:22">
<style type="text/css">
#center {width:800px;margin:0 auto;padding:50px;}
</style>
</head>
<body>
<div id="center">
</div>
<script type="text/javascript" src="suanfa.js"></script>
<script type="text/javascript">
var g1 = new Graph();
/*
-->b-->f-->h
a-->c
-->d-->g-->i
-->e
*/
g1.addTopPoint("a");
g1.addTopPoint("b");
g1.addTopPoint("c");
g1.addTopPoint("d");
g1.addTopPoint("e");
g1.addTopPoint("f");
g1.addTopPoint("g");
g1.addTopPoint("h");
g1.addTopPoint("i");
g1.addEdge(0,1);g1.addEdge(1,5);g1.addEdge(5,7);
g1.addEdge(0,2);
g1.addEdge(0,3);g1.addEdge(3,6);g1.addEdge(6,8);
g1.addEdge(0,4);
//深度优先搜索
//g1.dfs();
//广度优先搜索
g1.bfs();
</script>
</body>
</html>