Naloga 1
This commit is contained in:
commit
469fa70a87
29
naloga1/.gitignore
vendored
Normal file
29
naloga1/.gitignore
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
### IntelliJ IDEA ###
|
||||
out/
|
||||
!**/src/main/**/out/
|
||||
!**/src/test/**/out/
|
||||
|
||||
### Eclipse ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
bin/
|
||||
!**/src/main/**/bin/
|
||||
!**/src/test/**/bin/
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
8
naloga1/.idea/.gitignore
generated
vendored
Normal file
8
naloga1/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
12
naloga1/.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
12
naloga1/.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@ -0,0 +1,12 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="ignoredIdentifiers">
|
||||
<list>
|
||||
<option value="tests.smoke.test_absences_sliding.TestAbsencesSliding.*" />
|
||||
</list>
|
||||
</option>
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
6
naloga1/.idea/misc.xml
generated
Normal file
6
naloga1/.idea/misc.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="homebrew-23" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
8
naloga1/.idea/modules.xml
generated
Normal file
8
naloga1/.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/naloga1.iml" filepath="$PROJECT_DIR$/naloga1.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
11
naloga1/naloga1.iml
Normal file
11
naloga1/naloga1.iml
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
11
naloga1/src/Main.java
Normal file
11
naloga1/src/Main.java
Normal file
@ -0,0 +1,11 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
int[] elementi = new int[]{-1, 2, -5, 2, -1, 2, -2, 1};
|
||||
|
||||
NajvecjePodzaporedje najvecjePodzaporedje = new NajvecjePodzaporedje(elementi);
|
||||
|
||||
najvecjePodzaporedje.deliVladaj();
|
||||
|
||||
najvecjePodzaporedje.izpisiNajvecjePodzaporedje();
|
||||
}
|
||||
}
|
122
naloga1/src/NajvecjePodzaporedje.java
Normal file
122
naloga1/src/NajvecjePodzaporedje.java
Normal file
@ -0,0 +1,122 @@
|
||||
public class NajvecjePodzaporedje {
|
||||
int[] elementi;
|
||||
int start = 0, end = 0, maxSum = Integer.MIN_VALUE;
|
||||
|
||||
NajvecjePodzaporedje(int[] elementi) {
|
||||
this.elementi = elementi;
|
||||
}
|
||||
|
||||
public void grobaSila() {
|
||||
System.out.println("groba sila");
|
||||
|
||||
int n = elementi.length;
|
||||
this.maxSum = Integer.MIN_VALUE;
|
||||
for (int i = 0; i < n; i++) {
|
||||
int sum = 0;
|
||||
for (int j = i; j < n; j++) {
|
||||
sum += elementi[j];
|
||||
if (sum >= this.maxSum) {
|
||||
this.start = i;
|
||||
this.end = j;
|
||||
this.maxSum = sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class Result {
|
||||
int sum, start, end;
|
||||
|
||||
Result(int sum, int start, int end) {
|
||||
this.sum = sum;
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
}
|
||||
}
|
||||
|
||||
public void deliVladaj() {
|
||||
System.out.println("deli vladaj");
|
||||
|
||||
Result result = najvecjeZaporedje(0, elementi.length - 1);
|
||||
this.maxSum = result.sum;
|
||||
this.start = result.start;
|
||||
this.end = result.end;
|
||||
}
|
||||
|
||||
Result najvecjeZaporedje(int minI, int maxI) {
|
||||
System.out.println("najvecje zaporedje");
|
||||
|
||||
if (minI == maxI) {
|
||||
return new Result(this.elementi[minI], minI, minI);
|
||||
}
|
||||
|
||||
int border = (minI + maxI) / 2;
|
||||
Result left = this.najvecjeZaporedje(minI, border);
|
||||
Result right = this.najvecjeZaporedje(border + 1, maxI);
|
||||
Result center = this.najvecjeSredinskoZaporedje(minI, maxI, border);
|
||||
|
||||
if (right.sum >= left.sum && right.sum >= center.sum) {
|
||||
return right;
|
||||
} else if (left.sum >= right.sum && left.sum >= center.sum) {
|
||||
return left;
|
||||
} else {
|
||||
return center;
|
||||
}
|
||||
}
|
||||
|
||||
Result najvecjeSredinskoZaporedje(int minI, int maxI, int border) {
|
||||
System.out.println("najvecje sredinsko");
|
||||
|
||||
int leftSum = Integer.MIN_VALUE;
|
||||
int rightSum = Integer.MIN_VALUE;
|
||||
|
||||
int sum = 0;
|
||||
int start = 0;
|
||||
for (int i = border; i >= minI; i--) {
|
||||
sum += elementi[i];
|
||||
if (sum > leftSum) {
|
||||
leftSum = sum;
|
||||
start = i;
|
||||
}
|
||||
}
|
||||
|
||||
sum = 0;
|
||||
int end = 0;
|
||||
for (int i = border; i <= maxI; i++) {
|
||||
sum += elementi[i];
|
||||
if (sum > rightSum) {
|
||||
rightSum = sum;
|
||||
end = i;
|
||||
}
|
||||
}
|
||||
|
||||
return new Result(Math.max(Math.max(leftSum, rightSum), leftSum + rightSum - elementi[border]), start, end);
|
||||
}
|
||||
|
||||
|
||||
public void kadanovAlgoritem() {
|
||||
System.out.println("kadanov algoritem");
|
||||
|
||||
int maxEnding = elementi[0];
|
||||
int tmpStart = 0;
|
||||
|
||||
for (int i = 1; i < elementi.length; i++) {
|
||||
if (elementi[i] > maxEnding + elementi[i]) {
|
||||
maxEnding = elementi[i];
|
||||
tmpStart = i;
|
||||
} else {
|
||||
maxEnding = maxEnding + elementi[i];
|
||||
}
|
||||
|
||||
if (maxEnding > this.maxSum) {
|
||||
this.maxSum = maxEnding;
|
||||
this.start = tmpStart;
|
||||
this.end = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void izpisiNajvecjePodzaporedje() {
|
||||
System.out.printf("%d,%d,%d\n", this.start, this.end, this.maxSum);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user