-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadixSort.java
More file actions
88 lines (57 loc) · 1.85 KB
/
RadixSort.java
File metadata and controls
88 lines (57 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.mycompany.algorithm_final_project;
import java.util.Scanner;
/**
*
* @author israkkayumchowdhury
*/
public class RadixSort {
public void radix_sort() {
Scanner s = new Scanner(System.in);
System.out.print(" Enter Array Size --> ");
int size = s.nextInt();
int[] arr = new int[size];
// array input
System.out.print(" Enter Array Value --> ");
for (int i = 0; i < size; i++) {
arr[i] = s.nextInt();
}
//Display array before sorting
System.out.println("");
System.out.print(" Elements Before Sorting --> ");
for (int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
// find maximum number --> max
int max = 0;
for (int i = 0; i < size; i++) {
if (max < arr[i]) {
max = arr[i];
}
}
// sorting operation
for (int pos = 1; max / pos > 0; pos *= 10) {
// counting sort
int[] temp = new int[size];
int[] count = new int[11];
for (int i = 0; i < size; i++) {
++count[arr[i] / pos % 10]; // counting
}
for (int i = 1; i <= 10; i++) {
count[i] = count[i] + count[i - 1]; // counting update
}
for (int i = size - 1; i >= 0; i--) {
temp[--count[((arr[i] / pos) % 10)]] = arr[i]; // sort value
}
for (int i = 0; i < size; i++) { // value copy
arr[i] = temp[i];
}
}
System.out.println("");
//Display array after sorting
System.out.print(" Elements After Sorting --> ");
for (int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
}
}