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. 输入某人出生日期(以字符串方式输入,如1987-4-1)使用DateTime和TimeSpan类,(1)计算其人的年龄;(2)计算从现在到其60周岁期间,总共多少天。

    http://blog.csdn.net/w92a01n19g/article/details/8764116 using System;using System.Collections.Generi ...

  2. IMAP协议学习笔记(一)

    IMAP IMAP(Internet Mail Access Protocol,Internet邮件访问协议)以前称作交互邮件访问协议(Interactive Mail Access Protocol ...

  3. NodeJS、npm安装步骤和配置(windows版本)

    https://jingyan.baidu.com/article/48b37f8dd141b41a646488bc.html 上面这个链接很详细了,怕它没了自己记一遍.我的简洁一点. 1. 打开no ...

  4. vue函数防抖和节流

    Vue函数防抖和节流https://zhuanlan.zhihu.com/p/72363385 <template> <div> <input type='text' v ...

  5. 箭头函数的this指向问题

    this指向的固定化,并不是因为箭头函数内部有绑定this的机制,实际原因是箭头函数根本没有自己的this,导致内部的this就是外层代码块的this.正是因为它没有this,所以也就不能用作构造函数 ...

  6. openstack stein部署手册 4. glance

    # 建立数据库用户及权限 create database glance; grant all privileges on glance.* to glance@'localhost' identifi ...

  7. 分布式锁的实现【基于ZooKeeper】

    引言 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提 ...

  8. phpStorm 配置PHP_CodeSniffer自动检查代码

    环境 ubuntu18.4 phpstorm php7.2 最正确安装方法 sudo apt-get install php-codesniffer 一.composer安装PHP_CodeSniff ...

  9. [BZOJ] 聚会

    问题描述 Y 岛风景美丽宜人,气候温和,物产丰富.Y 岛上有 N 个城市,有 N-1 条城市间的道路连接着它们.每一条道路都连接某两个城市.幸运的是,小可可通过这些道路可以走遍 Y岛的所有城市. 神奇 ...

  10. vue的响应接口

    Vue 可以添加数据动态响应接口. 例如以下实例,我们通过使用 $watch 属性来实现数据的监听,$watch 必须添加在 Vue 实例之外才能实现正确的响应. 实例中通过点击按钮自动加 1.set ...