/**
* @author liaojiamin
* @Date:Created in 16:38 2021/1/29
*/
public class MaxSumRec {
public static int[] getArrayData(int size) {
int[] arrayData = new int[size];
Random random = new Random();
for (int i = 0; i < size; i++) {
int temp = random.nextInt(50);
if (temp > 25) {
arrayData[i] = temp;
} else {
int value = temp - 2 * temp;
arrayData[i] = value;
}
}
return arrayData;
}
/**
* fun1 双循环 时间复杂度O(N^2)
* */
public static int getMaxSumRec(int[] arrayData){
if(arrayData == null || arrayData.length <= 0){
return -1;
}
if(arrayData.length == 1){
return arrayData[0] > 0 ? arrayData[0] : -1;
}
int max = 0;
for (int i = 0; i < arrayData.length; i++) {
int sum = 0;
for(int j=i; j< arrayData.length; j++){
sum +=arrayData[j];
if(sum > max){
max = sum;
}
}
}
return max;
}
public static void main(String[] args) {
int[] arraydata = getArrayData(20);
for (int i = 0; i < arraydata.length; i++) {
System.out.print(arraydata[i] + ", ");
}
System.out.println();
System.out.println(getMaxSumRec(arraydata));
}
}
/**
* @author liaojiamin
* @Date:Created in 16:38 2021/1/29
*/
public class MaxSumRec {
public static int[] getArrayData(int size) {
int[] arrayData = new int[size];
Random random = new Random();
for (int i = 0; i < size; i++) {
int temp = random.nextInt(50);
if (temp > 25) {
arrayData[i] = temp;
} else {
int value = temp - 2 * temp;
arrayData[i] = value;
}
}
return arrayData;
}
/**
* 分治算法
* */
public static int getMaxSumRec_1(int[] arrayData){
return getMaxSumRecDivide(arrayData, 0, arrayData.length -1);
}
public static int getMaxSumRecDivide(int[] arrayData, int left, int right){
if(left == right){
return arrayData[left] > 0 ? arrayData[left] : -1;
}
int center = (left + right)/2;
int maxLeft = getMaxSumRecDivide(arrayData, left, center);
int maxRight = getMaxSumRecDivide(arrayData, center + 1, right);
int maxLeftBordeSum =0;
int leftBordeSum = 0;
for(int i = center; i>= left; i--){
leftBordeSum+=arrayData[i];
if(maxLeftBordeSum < leftBordeSum){
maxLeftBordeSum = leftBordeSum;
}
}
int maxRightBordeSum = 0;
int rightBordeSum = 0;
for(int i = center+1; i<=right;i++){
rightBordeSum+=arrayData[i];
if(maxRightBordeSum < rightBordeSum){
maxRightBordeSum = rightBordeSum;
}
}
return Math.max(Math.max(maxLeft, maxRight), maxLeftBordeSum+maxRightBordeSum);
}
public static void main(String[] args) {
int[] arraydata = getArrayData(20);
for (int i = 0; i < arraydata.length; i++) {
System.out.print(arraydata[i] + ", ");
}
System.out.println();
System.out.println(getMaxSumRec_1(arraydata));
}
}
T(1) = 1
T(N) = 2T(N/2) +O(N)
public static int getMaxSumRec_2(int[] arrayData){
int maxSum = 0, thisSum = 0;
for (int i = 0; i < arrayData.length; i++) {
thisSum+=arrayData[i];
if(thisSum > maxSum){
maxSum = thisSum;
}else if (thisSum < 0){
//如果之前项累计不大于0, 则情况之前项和,从小计数
thisSum = 0;
}
}
return maxSum;
}
/**
* @author liaojiamin
* @Date:Created in 12:06 2021/2/1
*/
public class DivideAndConquerGreat {
public static void main(String[] args) {
int[] beginArrayData = getArrayData(30);
System.out.println("------------------");
int[] arrayData = quickSort(beginArrayData);
for (int i = 0; i < arrayData.length; i++) {
System.out.println(arrayData[i]);
}
}
public static int[] quickSort(int[] arrayData) {
if (arrayData == null || arrayData.length <= 1) {
return arrayData;
}
return quickSort(arrayData, 0, arrayData.length - 1);
}
public static int[] quickSort(int[] arrayData, int left, int right) {
if(Math.abs(left - right) <= 20){
insertionSort(arrayData, left, right);
}else {
if (left < right) {
int position = swap(arrayData, left, right);
quickSort(arrayData, left, position - 1);
quickSort(arrayData, position + 1, right);
}
}
return arrayData;
}
/**
* 快排主体实现
*/
public static int swap(int[] arrayData, int left, int right) {
int position = median3(arrayData, left, right);
int i = left ;
int j = right - 1;
while (i < j) {
while (i < j && arrayData[i] <= position) {
i++;
}
while (i < j && arrayData[j] >= position) {
j--;
}
if (i < j) {
swapElement(arrayData, i, j);
}
}
//position初始位置是right-1
swapElement(arrayData, i, right - 1);
return i;
}
/**
* 数据交换
*/
public static void swapElement(int[] arrayData, int i, int j) {
int temp = arrayData[i];
arrayData[i] = arrayData[j];
arrayData[j] = temp;
}
/**
* 三数中值获取
*/
public static int median3(int[] arrayData, int left, int right) {
int center = (left + right) / 2;
if (arrayData[center] < arrayData[left]) {
swapElement(arrayData, center, left);
}
if (arrayData[right] < arrayData[left]) {
swapElement(arrayData, right, left);
}
if (arrayData[right] < arrayData[center]) {
swapElement(arrayData, right, center);
}
swapElement(arrayData, center, right - 1);
return arrayData[right - 1];
}
/**
* 插入排序
*/
public static int[] insertionSort(int[] arraydata, int left, int right) {
if (arraydata == null || arraydata.length <= 1) {
return arraydata;
}
for (int i = 0; i <= right; i++) {
for (int j = i; j > left; j--) {
if (arraydata[j - 1] > arraydata[j]) {
int temp = arraydata[j - 1];
arraydata[j - 1] = arraydata[j];
arraydata[j] = temp;
}
}
}
return arraydata;
}
/**
* 随机生成数列
*/
public static int[] getArrayData(int size) {
int[] arrayData = new int[size];
Random random = new Random();
for (int i = 0; i < size; i++) {
int temp = random.nextInt(10);
if (temp > 0) {
arrayData[i] = temp;
} else {
int value = temp - 2 * temp;
arrayData[i] = value;
}
System.out.println(arrayData[i]);
}
return arrayData;
}
}
上一篇:数据结构与算法–贪婪算法2