Skip to content

Commit c0a4bbf

Browse files
committed
Initial commit
0 parents  commit c0a4bbf

14 files changed

+620
-0
lines changed

.idea/.gitignore

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/uiDesigner.xml

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BucketSort.jar

7.67 KB
Binary file not shown.

CountingSort.jar

7.87 KB
Binary file not shown.

Linear_Sorting_Algorithms.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

PA2.docx

34.2 KB
Binary file not shown.

RadixSort.jar

7.67 KB
Binary file not shown.

src/BucketSort.java

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// CS-146 - Ahror Abdulhamidov
2+
// Class for Bucket Sort
3+
4+
import java.util.ArrayList;
5+
6+
public class BucketSort {
7+
8+
// Array of double numbers
9+
private static double[] array = {8, 8, 2, 9, 4, 2, 3, 5, 4, 9, 3, 7, 4, 7, 2};
10+
11+
// Main method
12+
public static void main(String[] args) {
13+
14+
// Prepare the array of doubles
15+
DoubleNumber[] A = DoubleNumber.makeNumberArray(array);
16+
setCounts(A);
17+
System.out.println("Array of numbers and their counts before sorting:");
18+
System.out.println(DoubleNumber.getValues(A));
19+
System.out.println(DoubleNumber.getCounts(A));
20+
21+
// Bucket Sort
22+
System.out.println("\nBegin Bucket Sort:");
23+
bucketSort(A, 10);
24+
System.out.println("End of Bucket Sort");
25+
26+
// Show the sorting results and prove the stability
27+
System.out.println("\nArray of numbers and their counts after sorting:");
28+
System.out.println(DoubleNumber.getValues(A));
29+
System.out.println(DoubleNumber.getCounts(A));
30+
System.out.println("The array of the counts indicates that the Counting Sort is stable.");
31+
32+
}
33+
34+
/**
35+
* @author Ahror Abdulhamidov
36+
* Manually sets counts for each DoubleNumber in the array
37+
*/
38+
private static void setCounts(DoubleNumber[] A) {
39+
A[1].setCount(2);
40+
A[5].setCount(2);
41+
A[8].setCount(2);
42+
A[9].setCount(2);
43+
A[10].setCount(2);
44+
A[12].setCount(3);
45+
A[13].setCount(2);
46+
A[14].setCount(3);
47+
}
48+
49+
/**
50+
* @author Ahror Abdulhamidov
51+
* Does Bucket Sort for the specified array by creating n specified buckets
52+
*/
53+
private static void bucketSort(DoubleNumber[] A, int n) {
54+
DoubleNumber.normalize(A, n);
55+
ArrayList<DoubleNumber>[] B = new ArrayList[n];
56+
for (int i = 0; i < n; i++) {
57+
B[i] = new ArrayList<>();
58+
}
59+
for (int i = 0; i < A.length; i++) {
60+
insert(B[(int) (n * A[i].getValue())], A[i]);
61+
System.out.println(getBuckets(B));
62+
}
63+
ArrayList<DoubleNumber> list = concatenateLists(B);
64+
for (int i = 0; i < A.length; i++) {
65+
A[i] = list.get(i);
66+
}
67+
DoubleNumber.unNormalize(A, n);
68+
}
69+
70+
/**
71+
* @author Ahror Abdulhamidov
72+
* Inserts a DoubleNumber into the bucket in the right position
73+
* Style of inserting the element is similar to Insertion Sort
74+
*/
75+
private static void insert(ArrayList<DoubleNumber> bucket, DoubleNumber number) {
76+
if (bucket.size() == 0)
77+
bucket.add(number);
78+
else {
79+
int i = bucket.size();
80+
while ((bucket.get(i - 1).getValue() > number.getValue()) && (i != 1)) {
81+
i--;
82+
}
83+
bucket.add(i, number);
84+
}
85+
}
86+
87+
/**
88+
* @author Ahror Abdulhamidov
89+
* Concatinates ArrayLists of DoubleNumbers into one list and returns it
90+
*/
91+
private static ArrayList<DoubleNumber> concatenateLists(
92+
ArrayList<DoubleNumber>[] B) {
93+
ArrayList<DoubleNumber> list = new ArrayList<>();
94+
for (ArrayList<DoubleNumber> doubleNumbers : B) {
95+
list.addAll(doubleNumbers);
96+
}
97+
return list;
98+
}
99+
100+
/**
101+
* @author Ahror Abdulhamidov
102+
* Returns String of buckets of DoubleNumbers
103+
*/
104+
private static String getBuckets(ArrayList<DoubleNumber>[] B) {
105+
StringBuilder sb = new StringBuilder();
106+
sb.append("Buckets: [");
107+
sb.append(B[0]);
108+
for (int i = 1; i < B.length; i++) {
109+
sb.append(", ").append(B[i]);
110+
}
111+
sb.append("]");
112+
return sb.toString();
113+
}
114+
115+
}
116+

src/CountingSort.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// CS-146 - Ahror Abdulhamidov
2+
// Class for Counting Sort
3+
4+
import java.util.Arrays;
5+
6+
public class CountingSort {
7+
8+
// Array of numbers
9+
private static int[] array = {8, 8, 2, 9, 4, 2, 3, 5, 4, 9, 3, 7, 4, 7, 2};
10+
11+
// Main method
12+
public static void main(String[] args) {
13+
14+
// Prepare the array of numbers
15+
Number[] A = Number.makeNumberArray(array);
16+
setCounts(A);
17+
System.out.println("Array of numbers and their counts before sorting:");
18+
System.out.println(Number.getValues(A));
19+
System.out.println(Number.getCounts(A));
20+
21+
// Counting Sort
22+
System.out.println("\nBegin Counting Sort:");
23+
Number[] B = Number.makeNumberArray(15);
24+
countingSort(A, B, 9);
25+
System.out.println("End of Counting Sort");
26+
27+
// Show the sorting results and prove the stability
28+
System.out.println("\nArray of numbers and their counts after sorting:");
29+
System.out.println(Number.getValues(B));
30+
System.out.println(Number.getCounts(B));
31+
System.out.println(
32+
"The array of the counts indicates that the Counting Sort is stable."
33+
);
34+
35+
}
36+
37+
/**
38+
* @author Ahror Abdulhamidov
39+
* Manually sets counts for each Number in the array
40+
*/
41+
private static void setCounts(Number[] A) {
42+
A[1].setCount(2);
43+
A[5].setCount(2);
44+
A[8].setCount(2);
45+
A[9].setCount(2);
46+
A[10].setCount(2);
47+
A[12].setCount(3);
48+
A[13].setCount(2);
49+
A[14].setCount(3);
50+
}
51+
52+
/**
53+
* @author Ahror Abdulhamidov
54+
* Does Counting Sort for the array of Numbers A[] and stores the sorted array in B[]
55+
* Auxiliary array for the Counting Sort is of specified size k
56+
*/
57+
private static void countingSort(Number[] A, Number[] B, int k) {
58+
System.out.println("Array B: " + Number.getValues(B));
59+
int[] C = new int[k + 1];
60+
for (int i = 0; i <= k; i++)
61+
C[i] = 0;
62+
for (Number number : A)
63+
C[number.getValue()]++;
64+
for (int i = 1; i <= k; i++)
65+
C[i] += C[i - 1];
66+
System.out.println("Array C: " + Arrays.toString(C));
67+
for (int j = A.length - 1; j >= 0; j--) {
68+
C[A[j].getValue()]--;
69+
B[C[A[j].getValue()]] = A[j];
70+
System.out.println("\nArray B: " + Number.getValues(B));
71+
System.out.println("Array C: " + Arrays.toString(C));
72+
}
73+
}
74+
75+
}

0 commit comments

Comments
 (0)