2

Here is a function that I wrote to sort small arrays (<1000 elements). When I compared the performance to other examples online (Heap sort and merge sort). I found that mine seemed to perform as well or better. I'm curious what the official name of this type of sort is?

            function Sort(arr) {
                let out = [];
                let Max = 0;
                let Min = 0;
                let cur = 0;

                cur = arr[0];
                Max = cur;
                Min = Max;
                out.push(cur);

                for (let i = 1; i < arr.length; i++) {
                     cur = arr[i];
                    if (Min == Max) {
                        if (cur > Max) {
                            out.unshift(cur);
                            Max = cur;
                        }
                        else {
                            out.push(cur);
                            Min = cur;
                        }
                    }
                    else {
                        if (cur > Max) {
                            out.unshift(cur);
                            Max = cur;
                        }
                        else {
                            if (cur < Min) {
                                out.push(cur);
                                Min = cur;
                            }
                            else {
                                //splice into the middle
                                for (let z = 1; z < out.length; z++) {
                                    if (cur > out[z]) {
                                        out.splice(z, 0, cur);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                return out;
            }
7
  • I'm not sure about the name, but it looks like big O is around O(N^3), as soon as simplest obvious sorting algorithm is O(N^2), I doubt that somebody decided to give this one name Commented Jan 29, 2016 at 18:55
  • anyway, I see some similarities to tree sort except that you're using arrays to represent tree Commented Jan 29, 2016 at 18:59
  • What is the expected result of Sort() ? Commented Jan 29, 2016 at 19:15
  • 1
    @guest I can later today. Commented Jan 29, 2016 at 19:27
  • 1
    @guest271314 here is a link jsfiddle.net/hbzhzact/1. Hit F12 to open the console to view the elapsed times. You will need timestamps enabled. Commented Jan 29, 2016 at 20:19

1 Answer 1

3

This part is insertion sort and the rest is special cases handling to make it a bit quicker:

//splice into the middle
for (let z = 1; z < out.length; z++) {
  if (cur > out[z]) {
    out.splice(z, 0, cur);
    break;
  }
}

See animation at https://en.wikipedia.org/wiki/Insertion_sort . The difference is that in your code a new array is created instead of modifying the existing one.

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

1 Comment

Thanks! It does appear to be similar to insertion sort only not performed inline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.