-2

I’m experimenting with sorting large arrays of unique integers in JavaScript and came up with a simple approach. I’m curious if it’s a recognized pattern or if there are potential pitfalls I might be missing.

Here’s the code I’m using:

function sortArr(arr) {
    const sorted = [];
    arr.forEach(el => sorted[el] = el);
    return sorted.filter(el => el);
}

function getRandomArray(length, max = length * 10) {
    const set = new Set();
    while (set.size < length) {
        set.add(Math.floor(Math.random() * max) + 1);
    }
    return [...set];
}

const arr = getRandomArray(100000);
console.log(sortArr(arr));

How it works:

Each number from the array is placed at the index equal to its value in a new array.

Then they are filtered out of empty slots to get the sorted array.

Questions:

  1. Are there potential performance or memory issues with this approach in JavaScript, especially with large arrays or large maximum values?

  2. Would this be practical for production code, or is it more of a coding experiment?

Any feedback or references to similar approaches would be appreciated!

I was trying to sort a large array of unique integers efficiently in JavaScript. I wanted to see if placing each element at its index in a new array and then filtering empty slots would produce a correctly sorted array, and whether this approach is efficient compared to built-in sort or counting sort. I expected the code to return a sorted array in ascending order. I also expected it to run relatively fast for large arrays and use a reasonable amount of memory.

6
  • 2
    Why can't you benchmark your own code? Commented Sep 25 at 14:37
  • 1
    this will not produce correct sorted array if some numbers repeat multiple times (this is why you need counting sort), for unique numbers - it could have problems with 0 values, and then main problem is memory - for very large numbers (2^32) it can get out of memory Commented Sep 25 at 14:37
  • 1
    @IłyaBursov "this will not produce correct sorted array if some numbers repeat multiple times" The title: "Is this a valid JavaScript approach for sorting large arrays of unique integers efficiently?" Commented Sep 25 at 14:37
  • 1
    but in general, counting sort (also radix sort) are the best performance sorting algorithms, though with very strict memory limitations Commented Sep 25 at 14:38
  • 1
    sorted.filter(el => el) removes the 0. This code only works with positive integers. Are you asking for a code review, for opinions? What is the threshold for "efficient"? According to your badges, you should first read the tour, especially the part: "Don't ask about... Requests for lists of things, polls, opinions, discussions, etc." Commented Sep 25 at 14:40

1 Answer 1

3

This will work for an array with integers that are valid array indices, and are not zero. In other words, it only works for unique, strictly positive integers that are not 232−1 or greater.

Here is a test case that shows how this sorting approach does not work if those constraints are violated:

function sortArr(arr) {
    const sorted = [];
    arr.forEach(el => sorted[el] = el);
    return sorted.filter(el => el);
}

const arr = [-1, 6000000000, 5000000000, 0];
console.log(sortArr(arr));

The output is an empty array, because:

  • The first three values become string properties of sorted, but are not array indices, so they are not iterated by an array method such as filter.

  • 0 is a valid array index, but the filter excludes it because it is a falsy value.

Sign up to request clarification or add additional context in comments.

1 Comment

it is sufficient to return true in filter, because sparse items are not visited.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.