如何使用我的quickSort工作用户输入的数字和/或字符串?

时间:2015-12-06 03:08:50

标签: javascript html css quicksort

我的quickSort的设计使得只需按两个按钮,随机数就会生成和排序自己以及我在代码中输入的字符串。我想这样做,以便我可以在网站上创建一个框,供用户输入多个数字和/或名称,quickSort将对其进行排序并显示它。我怎么能这样做呢?谢谢。这是代码:

function quickSort(array, left, right){
   var len = array.length, 
   pivot,
   partitionIndex;


  if(left < right){
    pivot = right;
    partitionIndex = partition(array, pivot, left, right);

   quickSort(array, left, partitionIndex - 1);
   quickSort(array, partitionIndex + 1, right);
  }
  return array;
}

function partition(array, pivot, left, right){
   var pivotValue = array[pivot],
       partitionIndex = left;


   for(var i = left; i < right; i++){
    if(array[i] < pivotValue){
      swap(array, i, partitionIndex);
      partitionIndex++;
    }
  }

  swap(array, right, partitionIndex);
  return partitionIndex;
}


function swap(array, i, j){
   var temp = array[i];
   array[i] = array[j];
   array[j] = temp;
}


function RandNum(array, quantity) {
    var num;
    for (var i = 0; i < quantity; i++) {
        num = Math.floor(Math.random() * (100 - 50 + 1)) + 10;
        if (num !== array[i - 1]) {
            array.push(num);
        } else {
            i--;
        }
    }
}

function sort(array){
    quickSort(array,0,array.length - 1);
 document.getElementById("QuickTimes").insertAdjacentHTML("beforeend",array+"<br/>");
}

function string(array){
    var test_array = ["Santa", "Arrow", "God", "Solo"];
    var sorted_array = sort(test_array, function(a,b) { return a<b; });
console.log(array);
}

HTML:

    

<head>

  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Quicksort Test</title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="Puravsquick.js"></script>
    <button onclick="var a = []; RandNum(a, 9); sort(a);" "sort()">Quicksort Commence!</button>
    <button onclick="string()" "sort()">String Quicksort Commence!</button>
    <div id="QuickTimes">
    </div>
<head>
<html>

0 个答案:

没有答案