-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This java program gives the counting sort
- Loading branch information
1 parent
2dad4b5
commit 22e5971
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,42 @@ | ||
import java.util.Scanner; | ||
|
||
public class CountSort { | ||
|
||
static void countSort(char arr[]) | ||
{ | ||
int n = arr.length; | ||
char output[] = new char[n]; | ||
|
||
int count[] = new int[256]; | ||
|
||
for (int i=0; i<256; ++i) | ||
count[i] = 0; | ||
|
||
for (int i=0; i<n; ++i) | ||
++count[arr[i]]; | ||
|
||
for (int i=1; i<=255; ++i) | ||
count[i] += count[i-1]; | ||
|
||
|
||
for (int i = n-1; i>=0; i--) | ||
{ | ||
output[count[arr[i]]-1] = arr[i]; | ||
--count[arr[i]]; | ||
} | ||
|
||
|
||
for (int i = 0; i<n; ++i) | ||
arr[i] = output[i]; | ||
} | ||
|
||
public static void main(String...args) | ||
{ | ||
Scanner s=new Scanner(System.in); | ||
System.out.println("Enter the string you want to sort"); | ||
String str=s.nextLine(); | ||
char arr[]=str.toCharArray(); | ||
countSort(arr); | ||
System.out.println("Result:"+new String(arr)); | ||
} | ||
} |