-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
super fast sort algorithm in js #1
Labels
super fast sort algorithm
super fast sort algorithm
Comments
var arrayLength = 1000;
var iterations = 200;
var min = 0;
var max = 1000000;
/**
* Bubble sort
*/
function swap(items, firstIndex, secondIndex){
var temp = items[firstIndex];
items[firstIndex] = items[secondIndex];
items[secondIndex] = temp;
}
function bubbleSort(items){
var len = items.length,
i, j, stop;
for (i=0; i < len; i++){
for (j=0, stop=len-i; j < stop; j++){
if (items[j] > items[j+1]){
swap(items, j, j+1);
}
}
}
return items;
}
/**
* Selection sort
*/
function selectionSort(items){
var len = items.length,
min;
for (i=0; i < len; i++){
//set minimum to this position
min = i;
//check the rest of the array to see if anything is smaller
for (j=i+1; j < len; j++){
if (items[j] < items[min]){
min = j;
}
}
//if the minimum isn't in the position, swap it
if (i != min){
swap(items, i, min);
}
}
return items;
}
/**
* Quicksort
*/
function partition(items, left, right) {
var pivot = items[Math.floor((right + left) / 2)],
i = left,
j = right;
while (i <= j) {
while (items[i] < pivot) {
i++;
}
while (items[j] > pivot) {
j--;
}
if (i <= j) {
swap(items, i, j);
i++;
j--;
}
}
return i;
}
function quickSort(items, left, right) {
var index;
if (items.length > 1) {
left = typeof left != "number" ? 0 : left;
right = typeof right != "number" ? items.length - 1 : right;
index = partition(items, left, right);
if (left < index - 1) {
quickSort(items, left, index - 1);
}
if (index < right) {
quickSort(items, index, right);
}
}
return items;
}
function sort(unsorted) {
console.log(bubbleSort(unsorted));
console.log(selectionSort(unsorted));
console.log(quickSort(unsorted));
}
function sortAll() {
for (var i = 2; i <= iterations; ++i) {
var unsorted = [];
for (var j = 0; j < arrayLength; j++) {
var element = Math.floor(Math.random() * (max - min + 1)) + min;
unsorted.push(element);
}
sort(unsorted);
}
}
var sortButton = document.getElementById("sort");
sortButton.addEventListener("click", sortAll, false);var arrayLength = 1000;
var iterations = 200;
var min = 0;
var max = 1000000;
/**
* Bubble sort
*/
function swap(items, firstIndex, secondIndex){
var temp = items[firstIndex];
items[firstIndex] = items[secondIndex];
items[secondIndex] = temp;
}
function bubbleSort(items){
var len = items.length,
i, j, stop;
for (i=0; i < len; i++){
for (j=0, stop=len-i; j < stop; j++){
if (items[j] > items[j+1]){
swap(items, j, j+1);
}
}
}
return items;
}
/**
* Selection sort
*/
function selectionSort(items){
var len = items.length,
min;
for (i=0; i < len; i++){
//set minimum to this position
min = i;
//check the rest of the array to see if anything is smaller
for (j=i+1; j < len; j++){
if (items[j] < items[min]){
min = j;
}
}
//if the minimum isn't in the position, swap it
if (i != min){
swap(items, i, min);
}
}
return items;
}
/**
* Quicksort
*/
function partition(items, left, right) {
var pivot = items[Math.floor((right + left) / 2)],
i = left,
j = right;
while (i <= j) {
while (items[i] < pivot) {
i++;
}
while (items[j] > pivot) {
j--;
}
if (i <= j) {
swap(items, i, j);
i++;
j--;
}
}
return i;
}
function quickSort(items, left, right) {
var index;
if (items.length > 1) {
left = typeof left != "number" ? 0 : left;
right = typeof right != "number" ? items.length - 1 : right;
index = partition(items, left, right);
if (left < index - 1) {
quickSort(items, left, index - 1);
}
if (index < right) {
quickSort(items, index, right);
}
}
return items;
}
function sort(unsorted) {
console.log(bubbleSort(unsorted));
console.log(selectionSort(unsorted));
console.log(quickSort(unsorted));
}
function sortAll() {
for (var i = 2; i <= iterations; ++i) {
var unsorted = [];
for (var j = 0; j < arrayLength; j++) {
var element = Math.floor(Math.random() * (max - min + 1)) + min;
unsorted.push(element);
}
sort(unsorted);
}
}
var sortButton = document.getElementById("sort");
sortButton.addEventListener("click", sortAll, false); |
demos
type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
type Fn = (value: JSONValue) => number
function sortBy(arr: JSONValue[], fn: Fn): JSONValue[] {
return arr.sort((a, b) => fn(a) - fn(b) > 0 ? 1 : -1);
};
// arr = [5, 4, 1, 2, 3]
// arr.sort((a,b) => a - b > 0 ? 1 : -1);
//升序 ascending https://leetcode.com/problems/sort-by/?envType=study-plan-v2&envId=30-days-of-javascript |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
super fast sort algorithm in js
https://www.cnblogs.com/xgqfrms/p/13090521.html
The text was updated successfully, but these errors were encountered: