I've been playing around and reading up on some sorting algorithms, and I've decided to try writing my own. It turned out to be quite fast (compared to the others shown in the image below).
I'm very curious to know a little bit about it:
- Does my algorithm already have a name?
- What are the downsides to it (other than it being really stupid to sort { 900000, 1 } with it)?
- Why is it not a good solution in general?
- Any suggestions for optimisation?
static int[] MySort(int[] input) { int minValue = input.Min() - 1; int[] tempArr = new int[input.Max() - minValue]; //{1,3,3,5} becomes: //{1,0,2,0,1} one 1's, two 3's, one 5's. for (int i = 0; i < input.Length; i++) tempArr[input[i] - minValue - 1] += 1; //AddNumbers to old array. and add minValue. int g = 0; for (int j = 0; j < tempArr.Length; j++) { if (tempArr[j] != 0) { for (int i = 0; i < tempArr[j]; i++) { input[g] = j + minValue + 1; g++; } } } return input; }
Performance screenshot: