-
Notifications
You must be signed in to change notification settings - Fork 300
Java counting sort #377
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
Closed
Closed
Java counting sort #377
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
79b88fb
Counting sort algorithm implemented in java
gdhameeja d133706
Adhered to coding guidelines
gdhameeja 8f1a886
Converted tabs to spaces throughout
gdhameeja e456b07
Removed trailing whitespace
gdhameeja 491cae9
Counted 56 spaces on line 28
gdhameeja efbc389
Removed trailing space
gdhameeja a6f8475
Used online beautifier
gdhameeja c256672
4 indent online beautify
gdhameeja a4603f3
Added classname and filename same
gdhameeja fa3ee8f
merging 4 commits into one
gdhameeja a29c8d3
Merge branch 'java-counting-sort' of https://github.com/gdhameeja/Alg…
gdhameeja db84341
merging 11 commits into 1
gdhameeja 6e335ad
Removed trailing whitespace
gdhameeja 8fd7639
something is going on
gdhameeja ed85ced
Starting again
gdhameeja 426397e
4 spaces
gdhameeja 573f95e
going to pull all
gdhameeja cbc41ac
watching youtube for git squash
gdhameeja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
||
class count_sort { | ||
/** | ||
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. Line contains following spacing inconsistencies:
Origin: SpaceConsistencyBear, Section: The issue can be fixed by applying the following patch: --- a/tmp/tmpwep5ebii/counting_sort/Java/count_sort.java
+++ b/tmp/tmpwep5ebii/counting_sort/Java/count_sort.java
@@ -25,7 +25,7 @@
*/
int maximum_number = getMax(arr); // get the max number in the array in order to declare count array.
int[] count_arr = new int[maximum_number + 1]; // maximum_number + 1 as we need to correctly
- //map each index with the number in the array
+ //map each index with the number in the array
for (int i = 0; i < count_arr.length; i++) {
count_arr[i] = 0; |
||
* Function to get maximum number in an array of integers. | ||
* @param arr the array from which maximum number is to be obtained. | ||
*/ | ||
static int getMax(int[] arr) { | ||
int max = Integer.MIN_VALUE; | ||
for (int each_num: arr) { | ||
if (each_num > max) { | ||
max = each_num; | ||
} | ||
} | ||
return max; | ||
} | ||
|
||
static int[] countSort(int[] arr) { | ||
/** | ||
* Function to sort the arrays based on the Counting sort alogrithm. | ||
* This algorithm works by maintaining the count of each number from the array | ||
* and then placing it at the right position in the resulting array | ||
* @param arr the array to be sorted. | ||
*/ | ||
int maximum_number = getMax(arr); // get the max number in the array in order to declare count array. | ||
int[] count_arr = new int[maximum_number + 1]; // maximum_number + 1 as we need to correctly | ||
//map each index with the number in the array | ||
|
||
for (int i = 0; i < count_arr.length; i++) { | ||
count_arr[i] = 0; | ||
} | ||
|
||
for (int each_number: arr) { | ||
count_arr[each_number] += 1; // get the count of each number in the original array. | ||
} | ||
|
||
for (int i = 1; i < count_arr.length; i++) { | ||
count_arr[i] += count_arr[i - 1]; // rolling sum gets count of numbers less than current index in the original array | ||
} | ||
|
||
int[] sorted_arr = new int[arr.length]; // declare and initialize sorted array. | ||
for (int i = 0; i < sorted_arr.length; i++) { | ||
sorted_arr[i] = 0; | ||
} | ||
|
||
for (int each_num: arr) { | ||
sorted_arr[count_arr[each_num] - 1] = each_num; //place the arrays at apt position on the basis of value in count array | ||
count_arr[each_num] -= 1; | ||
} | ||
return sorted_arr; | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] array = { | ||
4, | ||
2, | ||
1, | ||
55, | ||
22, | ||
1, | ||
77 | ||
}; // tester code | ||
int[] sorted_arr = countSort(array); | ||
for (int each_num: sorted_arr) { | ||
System.out.print(each_num + " "); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Line contains following spacing inconsistencies:
Origin: SpaceConsistencyBear, Section:
all.pyjava
.The issue can be fixed by applying the following patch:
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.
@sangamcse Hey Sangam, I am unable to apply patches.
These are the errors I get ->
error: counting_sort/Java/CountSort.java: No such file or directory
error: patch failed: counting_sort/Java/count_sort.java:37
error: counting_sort/Java/count_sort.java: patch does not apply
error: counting_sort/Java/CountSort.java: No such file or directory
Can you please help me with this?