Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added radix sort to your sorts in src/sortingandsearching #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/algo/sortingandsearching/radix sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package pers.jack.sort;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this package exist?



import java.util.*;


public class Radixsort {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have different file name than class name



public static void main(String[] args) {
int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 };
Print(arr);
System.out.println();
RADIXsort(arr);
Print(arr);
}

public static int getMax(int arr[]) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you dont have to make all methods static

int MAX = arr[0];
for (int i = 1; i < arr.length; i++)
if (arr[i] > MAX)
MAX = arr[i];
return MAX;
}

public static void COUNT_RAD(int arr[], int exp) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls follow java naming guide, wrong method naming here

int output[] = new int[arr.length];
int i;
int count[] = new int[10];
Arrays.fill(count, 0);

for (i = 0; i < arr.length; i++)
count[(arr[i] / exp) % 10]++;

for (i = 1; i < 10; i++)
count[i] += count[i - 1];

for (i = arr.length - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}

for (i = 0; i < arr.length; i++)
arr[i] = output[i];
}

public static void RADIXsort(int arr[]) {
int m = getMax(arr);
//////////////LEAST SIGNIFICANT///////////////////
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write notes/intentions/docs on top of method

for (int exp = 1; m / exp > 0; exp *= 10)
COUNT_RAD(arr, exp);
}

public static void Print(int arr[]) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use Arrays.toString() method

for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}

}