0

I am working in a program that handles data. However because i want my code efficient I would like a sorting algorithm that its running time does not depend on the number of inversions that the array has so i can sort it in ascending order. The order of the arrays is always:

  • n = array's size

  • list = (1,2,3,...,(n/2 -1), (n/2),(n/2 + 1),...3,2,1

I know the arrays has a total of inversions equal to:

  • (n/2 -1) + (n/2 -2) + (n/2 - 3) +...+ 1

I think that is a lot of inversions for a symmetrical array and because I know the order of the array is always like that I would like an algorithm to sort them in O(n) time. I know Insertion Sort's complexity is dependent on the number of inversions the array has but I am not sure what is the number of inversions of the array dependent on n

Ric
  • 41
  • 1
  • 1
  • 5
  • 2
    if you know the array is symmetrical and you know each side is sorted (in opposite directions), I don't see the problem (?) – Weyland Yutani Mar 22 '17 at 16:36
  • sorry i forgot to mention that i want it sort it in ascending order. I will edit it right now – Ric Mar 22 '17 at 16:43
  • i might misunderstand and be wrong, but can you not go through the first half of the array from the start and just feed everything out twice? I.e create an output array the same size as the input one. Then go through the input array up to the halfway point. At each element you encounter put it into the output array twice. – Weyland Yutani Mar 22 '17 at 17:39
  • Thanks man that helps alot :D. Is that an existing algorithm? I now is easy to implement but I am also writing a documentation for my code and if the algorithm exist it would be easy to explain. Thanks again – Ric Mar 22 '17 at 17:55
  • i don't think it has a name, the situation is very specific to have an array in a certain shape at the start. I don't think it happens often enough that anyone has given it a name – Weyland Yutani Mar 23 '17 at 10:05

1 Answers1

0

As your numbers are in a fixed range (0, n/2-1), you can use any of the range sorting algorithms like Counting Sort. See Non-comparision_sorts