# For my quickSort algorithm, how can I make it generate random strings?

My quickSort algorithm is perfect and can sort numbers and strings at the push of the buttons. The code is:

```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);
}

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">

<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>
<html>
```

What can I add so that it generates random strings and THEN sorts them? Any help is appreciated, thx :)

I'll provide you with a function that generates a random string -

```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}
```

Fill out the alphabet array with possible characters, and run rdstring( int length )

First create a string of characters that acts as the full set of characters you want to choose from.

```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('');
```

Then create a simple function which computes a random number between min (inclusive), and max (exclusive).

```function randInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
```

Finally, we have a string charSet which acts like a sequence, and a way of choosing random indices from it. We can now compose a function which generates a random string from that array.

```// 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));
```

Now you can create an array with elements generated from that function above and pass it to quickSort.

The final step is to attach the event listener to the button. The event listener will listen to the click event and call your callback function when the user clicks. The callback function will create the array of random strings and pass it to 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.
// 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.
// 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.
});```
`<button id="string-sort">Sort a random list of strings!</button>`