Skip to content

Commit 8399a6c

Browse files
committed
algorithms about Sort and Search
1 parent b86e926 commit 8399a6c

File tree

2 files changed

+250
-0
lines changed

2 files changed

+250
-0
lines changed

Sort and Search/Search.html

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Search</title>
6+
<script type="text/javascript">
7+
function ArrayList(){
8+
var array = [];
9+
10+
this.insert = function(item){
11+
array.push(item);
12+
};
13+
this.toString = function(){
14+
return array.join();
15+
};
16+
function swap(index1, index2){
17+
var aux = array[index1];
18+
array[index1] = array[index2];
19+
array[index2] = aux;
20+
}
21+
//冒泡排序
22+
this.bubbleSort = function(){
23+
for(var i = 0; i < array.length; i++){
24+
for(var j = 0; j < array.length - 1 - i; j++){
25+
if(array[j] > array[j+1]){
26+
swap(j, j+1);
27+
}
28+
}
29+
}
30+
};
31+
32+
//顺序搜索
33+
this.sequentialSearch = function(item){
34+
for(var i = 0; i < array.length; i++){
35+
if(item === array[i]){
36+
return i;
37+
}
38+
}
39+
return -1;
40+
};
41+
42+
//二分查找
43+
this.binarySearch = function(item){
44+
this.bubbleSort();
45+
46+
var low = 0;
47+
var high = array.length - 1;
48+
var mid, element;
49+
50+
while(low <= high){
51+
mid = Math.floor((low + high) / 2);
52+
element = array[mid];
53+
54+
if(element < item){
55+
low = mid + 1;
56+
}else if(element > item){
57+
high = mid - 1;
58+
}else{
59+
return array.length - 1 - mid;
60+
}
61+
}
62+
return -1;
63+
}
64+
}
65+
66+
function createNonSortedArray(size){
67+
var array = new ArrayList();
68+
for(var i = size; i > 0; i--){
69+
array.insert(i);
70+
}
71+
return array;
72+
}
73+
74+
75+
var array = createNonSortedArray(15);
76+
console.log(array.toString());
77+
console.log(array.binarySearch(6));
78+
</script>
79+
</head>
80+
<body>
81+
82+
</body>
83+
</html>

Sort and Search/Sort.html

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Sort</title>
6+
<script type="text/javascript">
7+
function ArrayList(){
8+
var array = [];
9+
function swap(index1, index2){
10+
var aux = array[index1];
11+
array[index1] = array[index2];
12+
array[index2] = aux;
13+
}
14+
this.insert = function(item){
15+
array.push(item);
16+
};
17+
this.toString = function(){
18+
return array.join();
19+
};
20+
21+
//冒泡排序
22+
this.bubbleSort = function(){
23+
for(var i = 0; i < array.length; i++){
24+
for(var j = 0; j < array.length - 1 - i; j++){
25+
if(array[j] > array[j+1]){
26+
swap(j, j+1);
27+
}
28+
}
29+
}
30+
};
31+
32+
//选择排序
33+
this.selectionSort = function(){
34+
var indexMin;
35+
for(var i = 0; i < array.length - 1; i++){
36+
indexMin = i;
37+
for(var j = i; j < array.length; j++){
38+
if(array[indexMin] > array[j]){
39+
indexMin = j;
40+
}
41+
}
42+
if(indexMin != i){
43+
swap(i, indexMin);
44+
}
45+
}
46+
};
47+
48+
//插入排序
49+
this.insertSort = function(){
50+
var j, temp;
51+
for(var i = 0; i < array.length; i++){
52+
j = i;
53+
temp = array[i];
54+
while(j > 0 && array[j - 1] > array[j]){
55+
array[j] = array[j - 1];
56+
j--;
57+
}
58+
array[j] = temp;
59+
}
60+
};
61+
62+
//归并排序
63+
this.mergeSort = function(){
64+
array = mergeSortRec(array);
65+
};
66+
var mergeSortRec = function(array){
67+
var length = array.length;
68+
69+
if(length === 1){
70+
return array;
71+
}
72+
73+
var mid = Math.floor(length / 2);
74+
var left = array.slice(0, mid);
75+
var right = array.slice(mid, length);
76+
77+
return merge(mergeSortRec(left), mergeSortRec(right));
78+
};
79+
var merge = function (left, right) {
80+
var result = [];
81+
var il = 0;
82+
var ir = 0;
83+
while (il < left.length && ir < right.length) {
84+
if (left[il] < right[ir]) {
85+
result.push(left[il++]);
86+
} else {
87+
result.push(right[ir++]);
88+
}
89+
}
90+
while (il < left.length) {
91+
result.push(left[il++]);
92+
}
93+
while (ir < right.length) {
94+
result.push(right[ir++]);
95+
}
96+
return result;
97+
}
98+
99+
//快速排序
100+
this.quickSort = function(){
101+
quick(array, 0, array.length - 1);
102+
}
103+
104+
var quick = function(array, left, right){
105+
var index;
106+
if(array.length > 1){
107+
index = partition(array, left, right);
108+
109+
if(left < index - 1){
110+
quick(array, left, index - 1);
111+
}
112+
113+
if(index < right){
114+
quick(array, index, right);
115+
}
116+
}
117+
}
118+
119+
var partition = function(array, left, right){
120+
var pivot = array[Math.floor((right + left) / 2)];
121+
var i = left;
122+
var j = right;
123+
124+
while(i <= j){
125+
while(array[i] < pivot){
126+
i++;
127+
}
128+
129+
while(array[j] > pivot){
130+
j--;
131+
}
132+
133+
if(i <= j){
134+
swapQuickSort(array, i, j);
135+
i++;
136+
j--;
137+
}
138+
}
139+
return i;
140+
}
141+
142+
var swapQuickSort = function(array, index1, index2){
143+
var aux = array[index1];
144+
array[index1] = array[index2];
145+
array[index2] = aux;
146+
}
147+
}
148+
149+
function createNonSortedArray(size){
150+
var array = new ArrayList();
151+
for(var i = size; i > 0; i--){
152+
array.insert(i);
153+
}
154+
return array;
155+
}
156+
157+
158+
var array = createNonSortedArray(15);
159+
console.log(array.toString());
160+
array.quickSort();
161+
console.log(array.toString());
162+
</script>
163+
</head>
164+
<body>
165+
166+
</body>
167+
</html>

0 commit comments

Comments
 (0)