📜 ⬆️ ⬇️

Quick sort byte array in java

For current tasks, it was necessary to sort large arrays of bytes, both signed (signed) and unsigned. The size of the array in my case was about 10 megabytes, this is not so much, that is, you can use sorting in memory.

At first I began to use java.util.Arrays.sort (byte []) ... Unfortunately, this solution was unacceptable, because:
- Arrays.sort allows you to sort only signed values ​​... very strange that the JDK developers limited themselves to this;
- Arrays.sort uses a universal method (tuned qsort), but for a number of tasks, such as the current one, this is not optimal.

As a result, I paid attention to the so-called sorting by counting, which in this case would be optimal. The implementation was also quite simple.
')
Explanation of the sorting method:

1. Initialize the model, which is an array of int [256]. Each i-th element of the model is Ni (the number of elements of the initial array is equal to i).

2. Fill the resulting array, while sequentially writing to it every i-th element of the Ni model once.

Everything, it is enough, very fast sorting is made in one pass!
Moreover, this method works for both signed and unsigned sorting, the only difference is that for unsigned, the resulting array is filled with elements in the range from 0 to 255, and for signed ones, in the range from -128 to 127.

Here is the class code (I deleted the comments to take up less space, enough to understand the principle of this code):

public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  1. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  2. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  3. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  4. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  5. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  6. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  7. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  8. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  9. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  10. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  11. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  12. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  13. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  14. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  15. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  16. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  17. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  18. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  19. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  20. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  21. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  22. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  23. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  24. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  25. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  26. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  27. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  28. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  29. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  30. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
  31. public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .
public class ByteArraySorter { private static final int BYTE_MODEL_SIZE = 256; private static final int BYTE_MASK = 0xFF; private static final int BYTE_SIGNED_MIN_VALUE = -128; private static final int BYTE_SIGNED_MAX_VALUE = 127; private static final int BYTE_UNSIGNED_MIN_VALUE = 0; private static final int BYTE_UNSIGNED_MAX_VALUE = 255; public static void sort( byte [] buffer) { sort(buffer, BYTE_SIGNED_MIN_VALUE, BYTE_SIGNED_MAX_VALUE); } public static void sortUnsigned( byte [] buffer) { sort(buffer, BYTE_UNSIGNED_MIN_VALUE, BYTE_UNSIGNED_MAX_VALUE); } private static void sort( byte [] buffer, int fromValue, int toValue) { if (buffer == null ) { return ; } int length = buffer.length; if (length == 0) { return ; } int [] model = new int [BYTE_MODEL_SIZE]; for ( int i = 0; i < length; i++) { model[buffer[i] & BYTE_MASK]++; } int index = 0; for ( int i = fromValue; i <= toValue; i++) { int toIndex = index + model[i & BYTE_MASK]; while (index < toIndex) { buffer[index] = ( byte )i; index++; } } } } * This source code was highlighted with Source Code Highlighter .

Measurement results for an array of 10 megabytes:

Arrays.sort (byte []):
- array filled with random values: 0.703 sec
- array filled with already sorted values: 0.231 sec
- array filled with only zeroes: 0.060 sec

ByteArraySort.sort (byte []):
- array filled with random values: 0.032 sec
- array filled with already sorted values: 0. 032 sec
- array filled with only zeroes: 0.047 sec

Each test was run 100 times, the minimum value was taken (the average differs from the minimum by only a few milliseconds, so the error in this case can be neglected).

The benefit of sorting by counting is clearly visible.

Note 1. This type of sorting is ineffective for small arrays, that is, if you need to sort, say, less than 1000 elements (1000 is obtained experimentally, but if anyone has time and desire, you can calculate mathematically, more accurately), then it is better to use other types of sorting. If the array contains more than 1000 elements, then there are practically no competitors in sorting by counting.

Note 2. This algorithm uses additional memory for the model, namely 256 * 4 = 1024 bytes. Although it is quite a bit, but still it is potentially possible to get an OutOfMemoryError, it is advisable to take this into account.

If someone offers a more optimal option, will say what can be improved, or points out possible shortcomings, I will be very grateful.

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


All Articles