Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

radix sort java

// Radix Sort in Java Programming

import java.util.Arrays;

class RadixSort {

  // Using counting sort to sort the elements in the basis of significant places
  void countingSort(int array[], int size, int place) {
    int[] output = new int[size + 1];
    int max = array[0];
    for (int i = 1; i < size; i++) {
      if (array[i] > max)
        max = array[i];
    }
    int[] count = new int[max + 1];

    for (int i = 0; i < max; ++i)
      count[i] = 0;

    // Calculate count of elements
    for (int i = 0; i < size; i++)
      count[(array[i] / place) % 10]++;

    // Calculate cumulative count
    for (int i = 1; i < 10; i++)
      count[i] += count[i - 1];

    // Place the elements in sorted order
    for (int i = size - 1; i >= 0; i--) {
      output[count[(array[i] / place) % 10] - 1] = array[i];
      count[(array[i] / place) % 10]--;
    }

    for (int i = 0; i < size; i++)
      array[i] = output[i];
  }

  // Function to get the largest element from an array
  int getMax(int array[], int n) {
    int max = array[0];
    for (int i = 1; i < n; i++)
      if (array[i] > max)
        max = array[i];
    return max;
  }

  // Main function to implement radix sort
  void radixSort(int array[], int size) {
    // Get maximum element
    int max = getMax(array, size);

    // Apply counting sort to sort elements based on place value.
    for (int place = 1; max / place > 0; place *= 10)
      countingSort(array, size, place);
  }

  // Driver code
  public static void main(String args[]) {
    int[] data = { 121, 432, 564, 23, 1, 45, 788 };
    int size = data.length;
    RadixSort rs = new RadixSort();
    rs.radixSort(data, size);
    System.out.println("Sorted Array in Ascending Order: ");
    System.out.println(Arrays.toString(data));
  }
}
Comment

Radix sort java

import java.util.Arrays;
class RadixSort {
  // Using counting sort to sort the elements in the basis of significant places
  void countingSort(int array[], int size, int place) {
    int[] output = new int[size + 1];
    int max = array[0];
    for (int i = 1; i < size; i++) {
      if (array[i] > max)
        max = array[i];
    }
    int[] count = new int[max + 1];

    for (int i = 0; i < max; ++i)
      count[i] = 0;

    // Calculate count of elements
    for (int i = 0; i < size; i++)
      count[(array[i] / place) % 10]++;

    // Calculate cumulative count
    for (int i = 1; i < 10; i++)
      count[i] += count[i - 1];

    // Place the elements in sorted order
    for (int i = size - 1; i >= 0; i--) {
      output[count[(array[i] / place) % 10] - 1] = array[i];
      count[(array[i] / place) % 10]--;
    }

    for (int i = 0; i < size; i++)
      array[i] = output[i];
  }

  // Function to get the largest element from an array
  int getMax(int array[], int n) {
    int max = array[0];
    for (int i = 1; i < n; i++)
      if (array[i] > max)
        max = array[i];
    return max;
  }

  // Main function to implement radix sort
  void radixSort(int array[], int size) {
    // Get maximum element
    int max = getMax(array, size);

    // Apply counting sort to sort elements based on place value.
    for (int place = 1; max / place > 0; place *= 10)
      countingSort(array, size, place);
  }

  // Main function
  public static void main(String args[]) {
    int[] data = { 121, 432, 564, 23, 1, 45, 788 };
    int size = data.length;
    RadixSort rs = new RadixSort();
    rs.radixSort(data, size);
    System.out.println("Sorted Array: ");
    System.out.println(Arrays.toString(data));
  }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java calendar 
Java :: creating an array list in java 
Java :: float to int in java 
Java :: how to convert errorBody to pojo in retrofit 
Java :: align button to the left in JPanel 
Java :: Java Access PriorityQueue Elements 
Java :: text field mouse event java 
Java :: java opp 
Java :: could not find java; set JAVA_HOME or ensure java is in PATH 
Java :: how to change resource color to int color in android 
Java :: java return new instance of generic type 
Java :: java sort arraylist of floats 
Java :: how to set path for java in windows 10 
Java :: java require non null 
Java :: Button loginButton = new Button(this); loginButton.setText("Login"); Button register Button = new Button(this); register Button .gettext("Register"); 
Java :: find the unique element in a list java 
Java :: display two dimensional array java 
Java :: variables in java 
Java :: get host from request object java 
Java :: cannot find symbol iterator in java 
Java :: how to hash string in java 
Java :: char value java 
Java :: java random threadlocalrandom double with 2 decimal places 
Java :: javax.persistence.noresultexception: no entity found for query 
Java :: Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8 
Java :: java background color 
Java :: Java 8 merge 2 collections. 
Java :: jda documentation discord 
Java :: java interview questions 
Java :: how to take binary input in java 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =