zoukankan      html  css  js  c++  java
  • JavaScript Sort

    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();
  • 相关阅读:
    学习攻略丨如何进阶为一名Web安全高手?
    审计篇丨PHPcms9.6.3后台XSS审计
    新手篇丨Python任意网段Web端口信息探测工具
    ZZZPHP1.61 代码审计-从SQL注入到Getshell
    FOFA爬虫大法——API的简单利用
    菜鸟如何反转到资深Web安全工程师
    实战经验丨CTF中文件包含的技巧总结
    一名合格的Web安全工程师之成长路径
    CTF丨2019互联网安全城市巡回赛·西安站,我们来了!
    暖春许愿季丨i春秋给你送福利
  • 原文地址:https://www.cnblogs.com/shidengyun/p/5126161.html
Copyright © 2011-2022 走看看