GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java/Programs/Games/MaximumSubArraySum.java at main · arshcoder13/Java Expand file tree/ Copy pathMaximumSubArraySum.javaMore file actions 45 lines (35 loc) · 1.52 KB / Copy pathMaximumSubArraySum.javaFile metadata and controls45 lines (35 loc) · 1.52 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 public class MaximumSubArraySum { //Maximum Subarray Sum using Divide and Conquer algorithm public static int MaximumSum(int[] Arr, int firstindex, int lastindex) { if (lastindex == firstindex) {//if the first index and the last index of array are same return Arr[ firstindex ];//it means array contains only one element //and this element equals maximum subarray sum. } // Finding middle element's index of the array int middle = ( firstindex + lastindex ) / 2; // Finding maximum subarray sum for the left subarray int leftMax = -1000000000; int sum = 0; for (int i = middle; i >= firstindex; i--) { sum += Arr[ i ];//addition for sum if (sum > leftMax) { leftMax = sum; } } // Find maximum subarray sum for the right subarray int rightMax = -1000000000;//should be minimum value sum = 0; for (int i = middle + 1; i <= lastindex; i++) { sum += Arr[ i ];//addition for sum if (sum > rightMax) { rightMax = sum; } } // Recursive part int maxLeftRight = Math.max (MaximumSum (Arr, firstindex, middle),//left part of the array MaximumSum (Arr, middle + 1, lastindex));//right part of the array // return maximum of the three return Math.max (maxLeftRight, leftMax + rightMax); } C:\Users\Ayse\Desktop\Java\Programs\Games\ MaximumSubArraySum } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |