📜 ⬆️ ⬇️

Sorting an array without using conditional statements

Immediately I warn you that this article will be an alternative version of this one . Yes, I can smell it straightforward as everyone rushes at once to criticize me, but, guys, the algorithm of the representations in the solution is not optimal. The comments offered even simpler algorithms on python. On python it is enough to write:

array.sort() 

And that's all. Who needs any algorithm at all? The task was set to a limit of 0 to 100, we are not lazy, we will do it in a general form, for any values ​​and with repetitions of numbers. A little thought, came here to this decision:

Code
 public class main { public static int max(int a, int b) { int i; for (i = 0; i < a - Math.abs(b);) { return a; } return Math.abs(b); } public static void main(String[] args) { int[] arrayForSort; int[] sortArray; int NUM_ELEMENT = 20, maxNum = -1000, i, j; arrayForSort = new int[NUM_ELEMENT]; for (i = 0; i < NUM_ELEMENT; i++) { arrayForSort[i] = (int) (Math.random() * 101) - 50; maxNum = max(maxNum, arrayForSort[i]); System.out.print(arrayForSort[i] + " "); } System.out.println(); sortArray = new int[maxNum * 2 + 1]; for (i = 0; i < NUM_ELEMENT; i++) { sortArray[arrayForSort[i] + maxNum]++; } for (i = 0; i <= maxNum * 2; i++) { for (j = 0; j < sortArray[i]; j++) { System.out.print(i - maxNum + " "); } } } } 


And now, let's look at what I wrote here.

In general, the problem is to find the minimum and maximum, since we can not use the comparison, then:
')
 public static int max(int a, int b) { int i; for (i = 0; i < a - Math.abs(b);) { return a; } return Math.abs(b); } 

If B modulo is more than A, then the cycle simply fails and returns. This is a fairly simple solution. Without the "great" mathematical formulas. Simple and clear. And also: why do I take on the module? This is caused by the sort method I use.

The original article uses a sort of "with a flag" (if I understood correctly). Performing such a sorting in the worst case is O (N ^ 2) (or close to it), which is not good.

This method (I don’t remember its name, it’s not lazy to look for this royal business ) decides in O (N) even though the numbers will be repeated.

 for (i = 0; i < NUM_ELEMENT; i++) { sortArray[arrayForSort[i] + maxNum]++; } 

The essence of sorting is that when the array is filled, it sorts itself. We represent the value as an index and increase the number of items in this index.

Example
We met the number 44 twice, so the sorted array at index 44 will contain 2. It's easy!

As it turned out (whether I am Krivorukov), arrays are created from 0 to N and as I did not try to make from -N to N, without success. Therefore, we make the offset, so we are looking for a maximum with the module. Then we simply reflect relative to 0 and we get a range of indices into which all elements except the boundary one exactly fit, so +1.

Explanation
We get a minimum of -48, and a maximum of 38. So we take that minimum of -48, and max 48, to simplify the algorithm. And shift so that the minimum was 0 -48 + 48

 sortArray = new int[maxNum * 2 + 1]; . . . sortArray[arrayForSort[i] + maxNum]++; . . . System.out.print(i - maxNum + " "); 

I have it all. I performed the task, while optimizing the process and presenting my vision of the solution to this problem.

Sample output
35 -29 26 17 -44 -10 31 -22 24 2 -28 17 2 -36 -30 35 39 -35 41 50
-44 -36 -35 -30 -29 -28 -22 -10 2 2 17 17 24 26 31 35 35 39 41 50

Source: https://habr.com/ru/post/270071/


All Articles