-
-
Notifications
You must be signed in to change notification settings - Fork 611
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package pers.jack.sort; | ||
|
||
|
||
import java.util.*; | ||
|
||
|
||
public class Radixsort { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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[]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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/////////////////// | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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[]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] + " "); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this package exist?