Skip to content

Commit 91bf1ea

Browse files
authored
Add files via upload
1 parent da1de8e commit 91bf1ea

File tree

9 files changed

+138
-0
lines changed

9 files changed

+138
-0
lines changed

Lab6/Lab6.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>

Lab6/Lab6Radzik.zip

208 KB
Binary file not shown.

Lab6/Wyniki Sortowań.docx

23.9 KB
Binary file not shown.

Lab6/Wyniki.pdf

220 KB
Binary file not shown.

Lab6/Zeszyt1.ods

9.15 KB
Binary file not shown.

Lab6/out/production/Lab6/Main.class

2.11 KB
Binary file not shown.
1.57 KB
Binary file not shown.

Lab6/src/Main.java

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.Arrays;
2+
import java.util.Random;
3+
4+
public class Main {
5+
private static final int size = 128;
6+
public static void main(String[] args) {
7+
Random rand = new Random();
8+
int[] arr = new int[size];
9+
// int[] arr = {0,1,2,3,4,5,6,7};
10+
// int[] arr = {7,6,5,4,3,2,1,0};
11+
int[] testArr;
12+
13+
for (int i = 0; i < size; i++) {
14+
int n = rand.nextInt(5000);
15+
arr[i] = n;
16+
}
17+
18+
System.out.println("TABLICA TESTOWA:");
19+
for (int i = 0; i < arr.length; i++) {
20+
System.out.print(arr[i]+", ");
21+
}
22+
23+
System.out.println();
24+
System.out.println("TABLICA PO SORTOWANIU");
25+
26+
testArr = arr.clone();
27+
SortMethods.BubbleSort(testArr);
28+
System.out.println("BUBBLE SORT: [PORÓWNANIA - PRZEPISANIA] - ["+SortMethods.getComparisions()+"-"+SortMethods.getRewrites()+"]");
29+
SortMethods.setComparisions(0);
30+
SortMethods.setRewrites(0);
31+
32+
33+
testArr = arr.clone();
34+
SortMethods.InsertionSort(testArr);
35+
System.out.println("INSERTION SORT: [PORÓWNANIA - PRZEPISANIA] - ["+SortMethods.getComparisions()+"-"+SortMethods.getRewrites()+"]");
36+
SortMethods.setComparisions(0);
37+
SortMethods.setRewrites(0);
38+
39+
testArr = arr.clone();
40+
SortMethods.SelectionSort(testArr);
41+
System.out.println("SELECTION SORT: [PORÓWNANIA - PRZEPISANIA] - ["+SortMethods.getComparisions()+"-"+SortMethods.getRewrites()+"]");
42+
SortMethods.setComparisions(0);
43+
SortMethods.setRewrites(0);
44+
45+
SortMethods.SelectionSort(arr);
46+
for (int i = 0; i < arr.length; i++) {
47+
System.out.print(testArr[i]+", ");
48+
}
49+
50+
}
51+
}

Lab6/src/SortMethods.java

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
public class SortMethods {
2+
3+
private static int comparisions = 0;
4+
private static int rewrites = 0;
5+
6+
public static int getComparisions() {
7+
return comparisions;
8+
}
9+
10+
public static int getRewrites() {
11+
return rewrites;
12+
}
13+
14+
15+
public static void setComparisions(int comparisions) {
16+
SortMethods.comparisions = comparisions;
17+
}
18+
19+
public static void setRewrites(int rewrites) {
20+
SortMethods.rewrites = rewrites;
21+
}
22+
23+
24+
public static void SelectionSort(int[] arr){
25+
int n = arr.length;
26+
for (int i = 0; i < n-1; i++) {
27+
int min_idx = i;
28+
for (int j = i + 1; j < n; j++){
29+
comparisions++;
30+
if (arr[j] < arr[min_idx])
31+
min_idx = j;
32+
rewrites++;
33+
}
34+
rewrites+=3;
35+
int temp = arr[min_idx];
36+
arr[min_idx] = arr[i];
37+
arr[i] = temp;
38+
}
39+
}
40+
41+
public static void InsertionSort(int[] arr){
42+
int n = arr.length;
43+
for (int i = 1; i < n; ++i) {
44+
rewrites++;
45+
int key = arr[i];
46+
int j = i - 1;
47+
while (j >= 0 && arr[j] > key) {
48+
rewrites++;
49+
comparisions++;
50+
arr[j + 1] = arr[j];
51+
j = j - 1;
52+
}
53+
54+
if(j>=0){
55+
comparisions++;
56+
}
57+
58+
rewrites++;
59+
arr[j + 1] = key;
60+
}
61+
}
62+
63+
public static void BubbleSort(int[] arr){
64+
int n = arr.length;
65+
for (int i = 0; i < n-1; i++)
66+
for (int j = 0; j < n-i-1; j++){
67+
comparisions++;
68+
if (arr[j] > arr[j+1]) {
69+
rewrites+=3;
70+
int temp = arr[j];
71+
arr[j] = arr[j+1];
72+
arr[j+1] = temp;
73+
}}
74+
}
75+
76+
}

0 commit comments

Comments
 (0)