Divide and conquer algorithm to achieve classic merge sort java implementation

content

 

1. What is the divide and conquer algorithm

divide and conquer

basic idea

2. The embodiment of divide and conquer algorithm: merge sort

merge sort

basic idea

3. Code implementation


1. What is the divide and conquer algorithm

divide and conquer

Divide and conquer, literally "divide and conquer", is to divide a complex 1 problem into two or more identical or similar sub-problems, and then divide the sub-problems into smaller sub-problems until the final sub-problem can be simply solved directly , the solution of the original problem is the combination of the solutions of the sub-problems. This idea is the basis of many efficient algorithms, such as sorting algorithms (quick sort, merge sort), Fourier transform (fast Fourier transform) and so on.

basic idea

The basic idea of ​​the divide and conquer method is to divide a big problem that is difficult to solve directly into some smaller problems of the same scale, so that each can be broken and divided and conquered.

2. The embodiment of divide and conquer algorithm: merge sort

merge sort

Merge sort ( MERGE - SORT ) is a sorting method implemented by the idea of ​​​​merging. The algorithm adopts the classic divide-and-conquer strategy (the divide and conquer method divides the problem into small problems and then solves them recursively. , and the conquer phase "patches" the answers obtained in the division phase together, that is, divide and conquer).

basic idea

Flowchart (take sorting the array [8,4,5,7,1,3,6,2] as an example)

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5rGC5LiN6ISx5Y-R,size_20,color_FFFFFF,t_70,g_se,x_16

Let's look at the governance stage again, we need to merge the two already ordered subsequences into an ordered sequence, such as the last merge in the above figure, to
[4,5,7,8] and [1,2, 3,6] Two already ordered subsequences are merged into the final sequence [1,2,3,4,5,6,7,8], let's see the implementation steps.

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5rGC5LiN6ISx5Y-R,size_20,color_FFFFFF,t_70,g_se,x_16

 3. Code implementation

package Sort;

import java.util.Arrays;
/**
 * 归并排序:
 * 
 * 利用归并的思想实现的排序方法,该算法采用经典的分治(divide-and-conquer)策略(分治法将问题分(divide)成一些小的问题然后递归求解,
 * 
 * 而治(conquer)的阶段则将分的阶段得到的各答案"修补"在一起,即分而治之)。
 * @author lenovo
 *
 */
public class MergeSort {
	public static void main(String[] args) {
		int[] a= {5,8,6,3,9,8,7,1,4,21,-8,46};
		int[] temp=new int[a.length];
		mergeSort(a, 0, a.length-1, temp);
		System.out.println(Arrays.toString(a));
	}
	public static void mergeSort(int[] arr,int left,int right,int[] temp) {
		if(left<right) {
			int mid=(left+right)/2;
			mergeSort(arr, left, mid, temp);
			mergeSort(arr, mid+1,right, temp);
			merge(arr, left, mid, right, temp);
		}
	}
	public static void merge(int[] arr,int left,int mid,int right,int[] temp) {
		int l=left;//左边序列的起始位置
		int r=mid+1;//右边序列的起始位置
		int t=0;//中间数组的当前元素下标
		while(l<=mid &&r<=right ) {//左边或右边没结束
			//那边小就将那边的元素放入到临时数组中
			if(arr[l]<=arr[r]) {
				temp[t++]=arr[l++];
			}else {
				temp[t++]=arr[r++];
			}
		}
		//while循环结束,说明有一边已经遍历完毕,将另一边剩余的元素放入到临时数组中
		while(l<=mid) {
			temp[t++]=arr[l++];
		}
		while(r<=right) {
			temp[t++]=arr[r++];
		}
		//将临时数组中的有序序列copy到原数组中
		t=0;
		int templeft=left;
		while(templeft<=right) {
			arr[templeft++]=temp[t++];
		}
	}
}

Guess you like

Origin blog.csdn.net/qq_52360069/article/details/123580334