Skip to content

Commit 0af51b6

Browse files
authored
Add files via upload
1 parent 91bf1ea commit 0af51b6

21 files changed

+667
-0
lines changed

Lab7/Jakub Radzik Wyniki.pdf

318 KB
Binary file not shown.

Lab7/Lab7.iml

+11
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>

Lab7/Lab7JakubRadzik.zip

324 KB
Binary file not shown.

Lab7/Results.docx

26.3 KB
Binary file not shown.

Lab7/Wyniki.ods

3.44 KB
Binary file not shown.

Lab7/Wyniki.xlsx

23.5 KB
Binary file not shown.

Lab7/out/production/Lab7/Main.class

2.26 KB
Binary file not shown.
2.5 KB
Binary file not shown.

Lab7/src/Main.java

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import java.util.Random;
2+
3+
public class Main {
4+
5+
6+
public static void displayTab(int[] tab){
7+
System.out.print('[');
8+
for (int i = 0; i < tab.length; i++) {
9+
System.out.print(tab[i]+", ");
10+
}
11+
System.out.println(']');
12+
}
13+
14+
private static final int size = 100000;
15+
16+
public static void main(String[] args) {
17+
long start = 0;
18+
long stop = 0;
19+
Random rand = new Random();
20+
int[] tab = new int[size];
21+
// int[] tab = {8,7,6,5,4,3,2,1};
22+
23+
for (int i = 0; i < size; i++) {
24+
int n = rand.nextInt(5000);
25+
tab[i] = n;
26+
}
27+
28+
int[] testArr = tab.clone();
29+
// System.out.println("TABLICA PRZED SORTOWANIEM");
30+
// displayTab(testArr);
31+
32+
System.out.println("długosc: "+size);
33+
//QUICK SORT===========================================================================================
34+
SortMethods1.setRewrites(0);
35+
SortMethods1.setComparisons(0);
36+
System.out.println("QUICK SORT");
37+
38+
start=System.nanoTime();
39+
SortMethods1.quickSort(testArr,0,testArr.length-1);
40+
stop=System.nanoTime();
41+
42+
System.out.println("Czas wykonania:"+(stop-start));
43+
System.out.println("Porównań / Przepisań - "+SortMethods1.getComparisons()+" / "+SortMethods1.getRewrites());
44+
45+
//MERGE SORT===========================================================================================
46+
47+
SortMethods1.setRewrites(0);
48+
SortMethods1.setComparisons(0);
49+
System.out.println("MERGE SORT");
50+
51+
start=System.nanoTime();
52+
SortMethods1.mergeSort(testArr,testArr.length);
53+
stop=System.nanoTime();
54+
55+
System.out.println("Czas wykonania:"+(stop-start));
56+
System.out.println("Porównań / Przepisań - "+SortMethods1.getComparisons()+" / "+SortMethods1.getRewrites());
57+
58+
//HEAP SORT===========================================================================================
59+
SortMethods1.setRewrites(0);
60+
SortMethods1.setComparisons(0);
61+
testArr = tab.clone();
62+
System.out.println("HEAP SORT");
63+
64+
start=System.nanoTime();
65+
SortMethods1.heapSort(testArr);
66+
stop=System.nanoTime();
67+
68+
System.out.println("Czas wykonania:"+(stop-start));
69+
70+
}
71+
}

Lab7/src/SortMethods1.java

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
public class SortMethods1 {
2+
3+
private static int comparisons;
4+
private static int rewrites;
5+
6+
public static int getComparisons() {
7+
return comparisons;
8+
}
9+
10+
public static void setComparisons(int comparisons) {
11+
SortMethods1.comparisons = comparisons;
12+
}
13+
14+
public static int getRewrites() {
15+
return rewrites;
16+
}
17+
18+
public static void setRewrites(int rewrites) {
19+
SortMethods1.rewrites = rewrites;
20+
}
21+
22+
//merge
23+
public static void mergeSort(int[] a, int n) {
24+
if (n < 2) {
25+
return;
26+
}
27+
int mid = n / 2;
28+
int[] l = new int[mid];
29+
int[] r = new int[n - mid];
30+
31+
for (int i = 0; i < mid; i++) {
32+
rewrites++;
33+
l[i] = a[i];
34+
}
35+
for (int i = mid; i < n; i++) {
36+
rewrites++;
37+
r[i - mid] = a[i];
38+
}
39+
mergeSort(l, mid);
40+
mergeSort(r, n - mid);
41+
42+
merge(a, l, r, mid, n - mid);
43+
}
44+
45+
public static void merge(
46+
int[] a, int[] l, int[] r, int left, int right) {
47+
48+
int i = 0, j = 0, k = 0;
49+
while (i < left && j < right) {
50+
comparisons++;
51+
if (l[i] <= r[j]) {
52+
rewrites++;
53+
a[k++] = l[i++];
54+
} else {
55+
rewrites++;
56+
a[k++] = r[j++];
57+
}
58+
}
59+
60+
while (i < left) {
61+
rewrites++;
62+
a[k++] = l[i++];
63+
}
64+
while (j < right) {
65+
rewrites++;
66+
a[k++] = r[j++];
67+
}
68+
}
69+
70+
//quick sort
71+
public static void quickSort(int arr[], int begin, int end) {
72+
if (begin < end) {
73+
int partitionIndex = partition(arr, begin, end);
74+
75+
quickSort(arr, begin, partitionIndex - 1);
76+
quickSort(arr, partitionIndex + 1, end);
77+
}
78+
}
79+
80+
private static int partition(int arr[], int begin, int end) {
81+
rewrites++;
82+
int pivot = arr[end];
83+
int i = (begin - 1);
84+
85+
for (int j = begin; j < end; j++) {
86+
comparisons++;
87+
if (arr[j] <= pivot) {
88+
i++;
89+
90+
rewrites += 3;
91+
int swapTemp = arr[i];
92+
arr[i] = arr[j];
93+
arr[j] = swapTemp;
94+
}
95+
}
96+
97+
rewrites += 3;
98+
int swapTemp = arr[i + 1];
99+
arr[i + 1] = arr[end];
100+
arr[end] = swapTemp;
101+
102+
return i + 1;
103+
}
104+
105+
//HEAP SORT
106+
public static void heapSort(int arr[]) {
107+
int n = arr.length;
108+
109+
for (int i = n / 2 - 1; i >= 0; i--)
110+
heapify(arr, n, i);
111+
112+
for (int i = n - 1; i > 0; i--) {
113+
int temp = arr[0];
114+
arr[0] = arr[i];
115+
arr[i] = temp;
116+
117+
heapify(arr, i, 0);
118+
}
119+
}
120+
121+
private static void heapify(int arr[], int n, int i) {
122+
int largest = i; // Initialize largest as root
123+
int l = 2 * i + 1; // left = 2*i + 1
124+
int r = 2 * i + 2; // right = 2*i + 2
125+
126+
if (l < n && arr[l] > arr[largest])
127+
largest = l;
128+
129+
if (r < n && arr[r] > arr[largest])
130+
largest = r;
131+
132+
if (largest != i) {
133+
int swap = arr[i];
134+
arr[i] = arr[largest];
135+
arr[largest] = swap;
136+
137+
heapify(arr, n, largest);
138+
}
139+
}
140+
141+
}
142+

Lab8/Lab8.iml

+11
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>
1.84 KB
Binary file not shown.
Binary file not shown.
390 Bytes
Binary file not shown.
2.78 KB
Binary file not shown.
6.45 KB
Binary file not shown.

Lab8/src/Lab8Radzik.zip

3.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)