diff --git a/02. Algorithms/01. Arrays/03. Sorting/06. RadixSort/RadixSort.java b/02. Algorithms/01. Arrays/03. Sorting/06. RadixSort/RadixSort.java new file mode 100644 index 0000000..7582ec0 --- /dev/null +++ b/02. Algorithms/01. Arrays/03. Sorting/06. RadixSort/RadixSort.java @@ -0,0 +1,86 @@ + +import java.util.ArrayList; + +public class RadixSort { + + public static void sort(int arr[],int div) //function to store based on their digit + { + ArrayList> array = new ArrayList>(); // array contains list which has elemnets according to their digit + for(int i=0;i<10;i++) //size of array taken as 10 i.e. 0 to 9 + { + ArrayList tempList = new ArrayList(); + array.add(tempList); + } + for(int i=0;i tempList =new ArrayList(); + tempList.add(arr[i]); + array.add((arr[i]/div)%10, tempList); + } + else // if size of list at desired index of array is not zero then remove old list put new list at that index + { + ArrayList tempList =new ArrayList(); + tempList = array.get((arr[i]/div)%10); + array.remove((arr[i]/div)%10); + tempList.add(arr[i]); + array.add((arr[i]/div)%10, tempList); + } + } + + int k=0; //put sorted values in the original array + for(int i=0;i0;div=div*10) //loop runs as equal number of times as there are total number of digits in maximum integer + { + sort(arr,div); + } + } + + + public static int max(int arr[]) //maximum number is found to calculate how many times loop will run + { + int max = arr[0]; + for(int i=1;imax) + { + max=arr[i]; + } + } + return max; + } + + public static void print(int arr[]) //function to print final sorted array + { + for(int i=0;i