我的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:
<!DOCTYPE html>
<html xml:lang="en-US">
<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>
我可以添加什么以便它生成随机字符串然后对它们进行排序?任何帮助表示赞赏,thx:)
答案 0 :(得分:0)
首先创建一个字符串,作为您想要选择的完整字符集。
var arr = [];
var s1 = 'A'.charCodeAt(0);
var e1 = 'Z'.charCodeAt(0) + 1;
var s2 = 'a'.charCodeAt(0);
var e2 = 'z'.charCodeAt(0) + 1;
// Push chars A-Z
for (var i = s1, l = e1; i < l; i++)
arr.push(String.fromCharCode(i));
// Push chars a-z
for (var i = s2, l = e2; i < l; i++)
arr.push(String.fromCharCode(i));
// Join array into string
var charSet = arr.join('');
然后创建一个简单函数,计算min(包含)和max(不包括)之间的随机数。
function randInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
最后,我们有一个字符串charSet
,它就像一个序列,以及一种从中选择随机索引的方法。我们现在可以编写一个从该数组生成随机字符串的函数。
// Generate a random string of length n from charSet
function randString(n, charSet) {
var arr = [];
var charSetLength = charSet.length;
for (var i = 0; i < n; i++)
arr.push(charSet[randInt(0, charSetLength)]);
return arr.join('');
}
// Log a random string of length 20 from charSet [A-Za-z]
console.log(randString(20, charSet));
现在,您可以创建一个数组,其中包含从上面的函数生成的元素,并将其传递给quickSort
。
最后一步是将事件监听器附加到按钮。事件监听器将监听click事件并在用户单击时调用您的回调函数。回调函数将创建随机字符串数组并将其传递给sort()
。
HTML:
<button id="string-sort">Sort a random list of strings!</button>
JS:
// select button by ID
var stringSort = document.getElementById('string-sort');
// Add an event listener to the button.
stringSort.addEventListener('click', function() {
// Create and populate array of 100 random strings.
var randStrings = [];
for (var i = 0; i < 100; i++)
randStrings.push(randString(10, charSet));
console.log(randStrings);
// Pass randStrings to sort function here.
});
var arr = [];
var s1 = 'A'.charCodeAt(0);
var e1 = 'Z'.charCodeAt(0) + 1;
var s2 = 'a'.charCodeAt(0);
var e2 = 'z'.charCodeAt(0) + 1;
// Push chars A-Z
for (var i = s1, l = e1; i < l; i++)
arr.push(String.fromCharCode(i));
// Push chars a-z
for (var i = s2, l = e2; i < l; i++)
arr.push(String.fromCharCode(i));
// Join array into string
var charSet = arr.join('');
function randInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
// Generate a random string of length n from charSet
function randString(n, charSet) {
var arr = [];
var charSetLength = charSet.length;
for (var i = 0; i < n; i++)
arr.push(charSet[randInt(0, charSetLength)]);
return arr.join('');
}
// Log a random string of length 20 from charSet [A-Za-z]
console.log(randString(20, charSet));
// select button by ID
var stringSort = document.getElementById('string-sort');
// Add an event listener to the button.
stringSort.addEventListener('click', function() {
// Create and populate array of 100 random strings.
var randStrings = [];
for (var i = 0; i < 100; i++)
randStrings.push(randString(10, charSet));
console.log(randStrings);
// Pass randStrings to sort function here.
});
&#13;
<button id="string-sort">Sort a random list of strings!</button>
&#13;
答案 1 :(得分:0)
我将为您提供一个生成随机字符串的函数 -
alphabet=["a","b","c"]; // Fill out possible characters here
function getRandomInt(t,a){return Math.floor(Math.random()*(a-t+1))+t}function rdstring(t){for(var a="",n=0;t>n;n++)a+=alphabet[getRandomInt(1,alphabet.length)];return a}
使用可能的字符填写alphabet
数组,然后运行rdstring( int length )