Naloga 1
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user