Skip to content

Radix Sort#55

Open
git-sumana wants to merge 4 commits intosiddeshshewde:masterfrom
git-sumana:master
Open

Radix Sort#55
git-sumana wants to merge 4 commits intosiddeshshewde:masterfrom
git-sumana:master

Conversation

@git-sumana
Copy link

import java.util.Arrays;

public class RadixSort {

// A utility function to get the maximum value in arr[]
static int getMax(int arr[]) {
    int max = arr[0];
    for (int i = 1; i < arr.length; i++)
        if (arr[i] > max)
            max = arr[i];
    return max;
}

// A function to do counting sort of arr[] according to
// the digit represented by exp.
static void countSort(int arr[], int exp) {
    int n = arr.length;
    int output[] = new int[n];
    int count[] = new int[10];
    Arrays.fill(count, 0);

    // Store count of occurrences in count[]
    for (int i = 0; i < n; i++)
        count[(arr[i] / exp) % 10]++;

    // Change count[i] so that count[i] now contains
    // actual position of this digit in output[]
    for (int i = 1; i < 10; i++)
        count[i] += count[i - 1];

    // Build the output array
    for (int i = n - 1; i >= 0; i--) {
        output[count[(arr[i] / exp) % 10] - 1] = arr[i];
        count[(arr[i] / exp) % 10]--;
    }

    // Copy the output array to arr[], so that arr[] now
    // contains sorted numbers according to the current digit
    for (int i = 0; i < n; i++)
        arr[i] = output[i];
}

// The main function to that sorts arr[] using Radix Sort
static void radixSort(int arr[]) {
    int max = getMax(arr);

    // Do counting sort for every digit. Note that instead
    // of passing digit number, exp is passed. exp is 10^i
    // where i is current digit number
    for (int exp = 1; max / exp > 0; exp *= 10)
        countSort(arr, exp);
}

// A utility function to print an array
static void printArray(int arr[]) {
    for (int i : arr) System.out.print(i + " ");
    System.out.println();
}

// Driver code
public static void main(String[] args) {
    int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
    System.out.println("Original array:");
    printArray(arr);

    radixSort(arr);

    System.out.println("\nSorted array:");
    printArray(arr);
}

}

// Original array:
170 45 75 90 802 24 2 66
// Sorted array:
2 24 45 66 75 90 170 802

### Radix Sort in Java

This repository contains a Java implementation of Radix Sort, a non-comparative integer sorting algorithm. The `RadixSort.java` file includes the implementation of the algorithm. The `main` method demonstrates the sorting process with a sample array. Radix Sort has a time complexity of O(nk), where n is the number of elements and k is the number of digits in the largest number.
Strassen's Algorithm for Matrix Multiplication
Strassen's Algorithm is an efficient algorithm for matrix multiplication, known for its recursive divide-and-conquer approach. It reduces the number of recursive calls by computing matrix products using fewer multiplications compared to the standard matrix multiplication algorithm. 

The algorithm splits the matrices into smaller submatrices, recursively computes their products using Strassen's Algorithm, and then combines the results to obtain the final product matrix. This approach reduces the number of multiplications, improving the overall efficiency of matrix multiplication.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant