function ArrayList() {
var array = [];
this.swap = function(index1, index2) {
var aux = array[index1];
array[index1] = array[index2];
array[index2] = aux;
}
var swapQuickSort = function(arrayList, index1, index2) {
var aux = arrayList[index1];
arrayList[index1] = arrayList[index2];
arrayList[index2] = aux;
}
this.print = function() {
console.log(array.join())
}
this.insert = function(item) {
array.push(item);
}
this.toString = function() {
return array.join();
}
this.bubbleSort = function() {
var length = array.length;
for (var i = 0; i < length; i++) {
for (var j = 0; j < length - 1; j++) {
if (array[j] > array[j + 1]) {
this.swap(j, j + 1);
}
}
}
}
this.createNonSortedArrary = function(size) {
var array = new ArrayList();
for (var i = size; i > 0; i--) {
array.insert(i);
}
return array;
}
this.adBubbleSort = function() {
var length = array.length;
for (var i = 0; i < length; i++) {
for (var j = 0; j < length - 1; j++) {
if (array[j] > array[j + 1]) {
this.swap(j, j + 1);
}
}
}
}
this.selectedSort = function() {
var length = array.length,
indexMin;
for (var i = 0; i < length - 1; i++) {
indexMin = i;
for (var j = i; j < length; j++) {
if (array[indexMin] > array[j]) {
indexMin = j;
}
}
if (i !== indexMin) {
this.swap(i, indexMin);
}
}
}
this.insertionSort = function() {
var length = array.length,
j,
temp;
for (var i = 0; i < length; i++) {
j = i;
temp = array[i];
while (j > 0 && array[j - 1] > temp) {
array[j] = array[j - 1];
j--;
}
array[j] = temp;
}
}
var merge = function(left, right) {
var result = [],
il = 0,
ir = 0;
while (il < left.length && ir < right.length) {
if (left[il] < right[ir]) {
result.push(left[il++]);
} else {
result.push(right[ir++]);
}
}
while (il < left.length) {
result.push(left[il++]);
}
while (ir < right.length) {
result.push(right[ir++]);
}
return result;
}
var mergeSortRec = function(arrayList) {
var length = arrayList.length;
if (length == 1) {
return arrayList;
}
var mid = Math.floor(length / 2),
left = arrayList.slice(0, mid),
right = arrayList.slice(mid, length);
return merge(mergeSortRec(left), mergeSortRec(right));
}
this.mergeSort = function() {
array = mergeSortRec(array);
}
var parition = function(arrayList, left, right) {
var pivot = arrayList[Math.floor((right + left) / 2)],
i = left,
j = right;
while (i <= j) {
while (arrayList[i] < pivot) {
i++;
}
while (arrayList[j] > pivot) {
j--;
}
if (i <= j) {
swapQuickSort(arrayList, i, j);
i++;
j--;
}
}
return i;
}
var quick = function(arrayList, left, right) {
var index;
if (arrayList.length > 1) {
index = parition(arrayList, left, right);
if (left < index - 1) {
quick(arrayList, left, index - 1);
}
if (index < right) {
quick(arrayList, index, right);
}
}
}
this.quickSort = function() {
quick(array, 0, array.length - 1);
}
this.clear = function() {
array = [];
}
this.sequentialSearch = function(item) {
for (var i = 0; i < array.length; i++) {
if (item == array[i]) {
return i;
}
}
return -1;
}
this.binarySearch = function(item) {
this.quickSort();
var low = 0,
high = array.length - 1,
mid,
element;
while (low <= high) {
mid = Math.floor((low + high) / 2);
element = array[mid];
if (element < item) {
low = mid + 1;
} else if (element > item) {
high = mid - 1
}else{
return mid;
}
}
}
}
var insertDefaultValue = function(array) {
array.insert(10);
array.insert(15);
array.insert(20);
array.insert(0);
array.insert(2);
array.insert(1);
}
var arrayList = new ArrayList();
insertDefaultValue(arrayList);
console.log("普通冒泡")
arrayList.bubbleSort();
arrayList.print();
arrayList.clear();
console.log("高级冒泡")
insertDefaultValue(arrayList);
arrayList.adBubbleSort();
arrayList.print();
arrayList.clear();
console.log("高级冒泡")
insertDefaultValue(arrayList);
arrayList.adBubbleSort();
arrayList.print();
arrayList.clear();
console.log("选择排序")
insertDefaultValue(arrayList);
arrayList.selectedSort();
arrayList.print();
arrayList.clear();
console.log("插入排序")
insertDefaultValue(arrayList);
arrayList.insertionSort();
arrayList.print();
arrayList.clear();
console.log("归并排序")
insertDefaultValue(arrayList);
arrayList.mergeSort();
arrayList.print();
arrayList.clear();
console.log("快速排序")
insertDefaultValue(arrayList);
arrayList.quickSort();
arrayList.print();
console.log(arrayList.sequentialSearch(15));
console.log(arrayList.binarySearch(20));
arrayList.clear();

JavaScript Sort的更多相关文章

  1. javascript sort()与reverse()

    javascript 中提供了两个对数据进行排序的方法,即sort()和reverse() 在理解的时候犯了一个非常低级的错误,现记录如下: reverse()不包括排序的功能,只是把原来的数组反转. ...

  2. javascript sort 用法

    <html> <head> <title></title> <script type="text/javascript" sr ...

  3. JavaScript sort() 方法

    定义和用法 sort() 方法用于对数组的元素进行排序. 语法 arrayObject.sort(sortby) 参数 描述 sortby 可选.规定排序顺序.必须是函数. 返回值 对数组的引用.请注 ...

  4. javascript sort方法容易犯错的地方

    sort方法用来对数组排序非常方便.但是sort(func)这个func参数的构造却很容易混淆. sort判断func的返回值是判断正负,而不是ture和false.所以务必保证返回值要么负数要么正数 ...

  5. Javascript:sort()方法快速实现对数组排序

    定义和用法: sort() 方法用于对数组的元素进行排序. 语法: arrayObject.sort(sortby) 注释:sortby,可选,规定排序顺序,必须是函数. 说明: 如果调用该方法时没有 ...

  6. javascript sort排序

    var arr = [5,32,28,66,2,15,3]; arr.sort(function(a1,a2){ return a1-a2; //a2-a1 输入倒序 }); console.log( ...

  7. JavaScript sort()方法比较器

    当我们想把一个由数字组成的数组进行简单的排序时,可能会想到sort()方法: var arr = [2 , 3, -1, -107, -14, 1]; console.log(arr.sort()) ...

  8. JavaScript sort() 方法详解

    定义和用法 sort() 方法用于对数组的元素进行排序. 语法 arrayObject.sort(sortby) 参数 描述 sortby 可选.规定排序顺序.必须是函数. 返回值 对数组的引用.请注 ...

  9. Javascript sort方法

    sort()方法用于对数组的元素进行排序 语法:array.Object.sort(sortBy) sortBy:可选.规定排序顺序.必须是函数 返回值:对数组的引用.数组在原数组上进行排序,不生成副 ...

  10. javascript sort 函数用法

    sort 函数 博客地址:https://ainyi.com/41 简单的说,sort() 在没有参数时,返回的结果是按升序来排列的.即字符串的Unicode码位点(code point)排序 [5, ...

随机推荐

  1. 移动端300ms延迟原理,穿透、遮罩层滑动导致下面滑动总结

    遮罩层滑动导致下面滑动 1,阻止弹层滑动,使用默认事件,使用这种方式弹层不能滑动 document.getElementById("model").addEventListener ...

  2. .net 项目中应用Web Services(vs2012)

    一.在asp.net项目中添加Web services1.新建一个asp.net项目(目前尚未验证是否可以在MVC项目中添加)2.在项目名上右击,选择添加→新建项→Web服务,输好名称后确定即可 二. ...

  3. CentOS7 设置电源选项,待机、睡眠、挂起

    设置装有 CentOS7 的笔记本合盖后黑屏进入睡眠模式 systemd 能够处理某些电源相关的 ACPI事件,你可以通过从 /etc/systemd/logind.conf 以下选项进行配置: Ha ...

  4. 基于cdn方式的vue+element-ui的单页面架构

    一.下载vue2.x,下载element-ui.js以及css 二.html文件 <!DOCTYPE html> <html> <head> <meta ch ...

  5. less: 变量

    在Less中声明变量方式是使用@符号 @test_width: 300px; .box { width: @test_width; height: @test_width; background-co ...

  6. [CF] 8C Looking for Order

    状压模板题 CF难度2000? 我得好好了解一下CF的难度机制了 反正CF的难度比洛谷真实就好了 Code #include<algorithm> #include<iostream ...

  7. 反射getDeclaredFields()

    public static void main(String[] args) { // 获取所有属性值 Field[] fields = People.class.getDeclaredFields( ...

  8. 在子组件中触发事件,传值给父组件-vue

    1.通过$emit触发事件 在子组件<x-test>中触发事件: <button @click="toSearchProduct()">搜索</but ...

  9. redis不重启之rdb数据切换到aof数据

    温馨提示: 要实现不重启从rdb切换到aof,redis的版本必须要在2.2以上. [root@pyyuc /data ::]#redis-server -v Redis server v= sha= ...

  10. Conda 中安装 Keras

    conda create -n keras python=3.5 ipykernel activate keras python -m ipykernel install --user --name ...