1.冒泡排序
function bubbleSort(arr) {
var temp; //临时交换变量
let n = arr.length; //记录数组长度
let count=0; //计数,记录一共进行了多少次交换
//document.write('数组长度为:'+n+'<br />') //输出数组成都
for (let i=0; i<n; i++) { //外层循环,排序出数组的arr[i]的值
let flag=0; //交换标志
for (let j=n-1; j>i; j-- ) { //内层循环,从底往上冒泡,将小泡浮到arr[i]位置
//alert(0);
if (arr[j-1]>arr[j]) { //比较两个元素大小,并交换位置
temp=arr[j-1];
arr[j-1]=arr[j];
arr[j]=temp;
flag=flag+1; //确定交换标志
count=count+1; //记录比较元素的次数
//console.log(flag);
//console.log('共交换了:'+count+'次');
}
//console.log(arr) //输出数组
}
if (flag==0) { //跳出循环
break;
}
}
document.write('冒泡排序执行次数为:'+count+'<br />')
return arr; //返回数组
}
2.简单选择排序
function selectSort(arr) {
let temp;
let n = arr.length;
let count =0;
for (let i=0; i<n; i++) {
for (let j= i+1; j<n; j++) {
count++;
if (arr[j]<arr[i]) {
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
document.write('简单选择排序执行次数为:'+count);
return arr;
}
3.直接插入排序
function insertSort(arr) {
let i,j;
let n= arr.length;
let count = 0;
for (i=1; i<n; i++) {
if (arr[i]<arr[i-1]) {
arr[-1] = arr[i]; //将i位置的值给哨兵
for (j=i-1; arr[j]>arr[-1];j--) { //当i位置前面的值比哨兵小,后移i位置的值,并插入哨兵到原先i位置
arr[j+1] = arr[j];
arr[j] = arr[-1];
count++;
}
}
}
document.write('直接插入排序执行次数为:'+count);
return arr;
}
4.希尔排序
function shellSort(arr) {
let d = arr.length;
let i;
let temp; //暂存
do {
//设置增量
d = Math.floor(d / 3) + 1;
for (i = d ; i < arr.length; i++) {
if (arr[i] < arr[i - d]) {
temp = arr[i];
for (var j = i - d; j >= 0 && temp < arr[j]; j -=d) {
arr[j + d] = arr[j];
arr[j] = temp;
}
}
}
}
while (d > 1)
return arr;
}
5.去重算法
function unique(arr) {
let brr=[];
for (let i=0; i<arr.length; i++) {
if (brr.indexOf(arr[i]) < 0) {
brr.push(arr[i]);
}
}
return brr;
}
6.快速排序
function partition(arr,low,high) {
let temp=arr[low], //基准值
changetemp; //用于交换的临时变量
while (low<high) {
while (low<high && arr[high]>=temp) {
high--;
} //当从high往前扫时,大于基准值时,high--
//swap(arr, low, high); //当从high往前扫时,小于基准值时,交换基准值和arr[high]位置
changetemp=arr[low];
arr[low]=arr[high];
arr[high]=changetemp;
while(low<high && arr[low]<=temp) {
low++;
} //当从low往后扫时,小于基准值时,low++
//swap(arr, low, high); //当从low往后扫时,大于基准值时,交换基准值和arr[low]的位置
changetemp=arr[low];
arr[low]=arr[high];
arr[high]=changetemp;
}
return low;
}
//快速排序
function quick(arr,low,high) {
let pivot; //定义枢轴值
if (low < high) {
pivot = partition(arr,low,high); //求得第一个基准值第一次排序完的位置作为枢轴值
quick(arr,low,pivot-1); //对枢轴值前面的序列排序
document.write(pivot+'<br />');
quick(arr, pivot+1, high); //对枢轴值后面的序列排序
}
//return pivot; //返回枢轴值
return arr; //返回排序后的数组
}
7.使用JS方法并将形参减少为仅输入一个数组的快排
function quickSort (arr) {
if (arr.length<=1) {
return arr;
}
let left = [],
right = [],
pivotIndex = Math.floor(arr.length/2);
let pivot = arr.splice(pivotIndex,1)[0];
for (let 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));
}
8.JS实现归并排序
function mergeSort (arr) {
//合并两个有序数组为一个数组
function merge(left,right) {
let arr=[];
while (left.length && right.length) {
if (left[0] < right[0]) {
arr.push(left.shift());
} else {
arr.push(right.shift());
}
}
return arr.concat(left,right);
}
let len=arr.length;
if (len <= 1) {
return arr;
} else {
let index = Math.floor(len/2),
left = arr.slice(0,index),
right = arr.slice(index);
return merge(mergeSort(left),mergeSort(right));//用递归对数组进行拆分,再返回合并后的数组
}
}
9.堆排序
function heapSort(array) {
var result = array.slice(0);
function swap(array, i, j) {
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
function maxHeapify(array, index, heapSize) {
var iMax, iLeft, iRight;
while (true) {
iMax = index;
iLeft = 2 * index + 1;
iRight = 2 * (index + 1);
if (iLeft < heapSize && array[index] < array[iLeft]) {
iMax = iLeft;
}
if (iRight < heapSize && array[iMax] < array[iRight]) {
iMax = iRight;
}
if (iMax != index) {
swap(array, iMax, index);
index = iMax;
} else {
break;
}
}
}
function buildMaxHeap(array) {
var i, iParent = Math.floor(array.length / 2) - 1;
for (i = iParent; i >= 0; i--) {
maxHeapify(array, i, array.length);
}
}
function sort(array) {
buildMaxHeap(array);
for (var i = array.length - 1; i > 0; i--) {
swap(array, 0, i);
maxHeapify(array, 0, i);
}
return array;
}
return sort(result);
}
10.JS实现顺序查找
function listSearch(arr,data) {
var result;
for (var i = 0;i<arr.length; i++) {
if (arr[i] === data) {
result = i;
break;
}
}
if (result === undefined) {
result = -1;
}
return result
}
11.JS实现二分查找
function binarySearch(arr,key) {
var low = 0,
high = arr.length-1;
while (low <= high) {
var mid = parseInt((low+high)/2);
if (key < arr[mid]) {
high = mid-1;
} else if (key > arr[mid]) {
low = mid+1;
} else {
return mid;
}
}
return -1;
}
12.统计字符串中出现次数最多的字符
function count(str) {
var obj = {},
mostStr = "",
key;
for(var i = 0; i<str.length; i++) {
if (!obj[str[i]]) {
obj[str[i]] = 1;
} else {
obj[str[i]]++;
}
}
for(key in obj) {
if (mostStr === "" || obj[key] > obj[mostStr]) {
mostStr = key
}
}
return [mostStr,obj[mostStr]]
}
13.斐波拉契数列递归写法
function fibonicco(n) {
if (n ===1 || n === 2) {
return 1;
} else {
return fibonicco(n-1)+fibonicco(n-2);
}
}
14.斐波拉契数列迭代写法
function fibonicco(n) {
var a,b,res;
a = b = res =1;
for (var i =3; i<=n; i++) {
res = a+b;
a = b;
b = res;
}
return res;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。