(sort two) merge sorting java code plus time complexity analysis

tags: Recursive

Merging sort time complexity analysis:

Induction:

T(n)<2 T(n/2)+cn 

T(n)<4 T(n/4)+2 cn  

T(n)<8 T(n/8)+3 cn

n numbers, layered in order as follows:

If n is 16,

Divided into left and right, traversal times when merging: n (8 + 8)

When divided into 4 parts (4, 4, 4, 4), when merging, first 4 + 4, 4 + 4 once, then 8 + 8 once, then 2 * n times

When divided into 8 parts (2, 2, 2, 2, 2, 2, 2, 2), when merging (2+2, 2+2, 2+2, 2+2) then (4+4, 4+ 4) Then 8+8 is 3*n times

When n=2^k

T(n)<2^K*T(n/(2^k))+k c n =n T(1)+c n log2n=Θ(nlogn)

Can also be understood as:

16+ 8+8 +4+4+4+4 +2+2+2+2+2+2+2+2 (no need to subdivide after two numbers per layer, recursively meet a number directly return I met the two numbers and compared them directly. It is already the bottom layer.)

The actual operation is not traversed layer by layer, but is similar to traversing the binary tree (the order of traversing in order)

The extra space complexity is O(n) with the help of an auxiliary array

Merge sort code:

Recursive part:

    public static void mergeSort(int[] arr) {
		if (arr == null || arr.length < 2) {
			return;
		}
		mergeSort(arr, 0, arr.length - 1);
	}
	public static void mergeSort(int[] arr, int l, int r) {
		if (l == r) {
			return;
		}
		int mid = l + ((r - l) >> 1);
		mergeSort(arr, l, mid);
		mergeSort(arr, mid + 1, r);
		merge(arr, l, mid, r);
	}

Merge code:

The two arrays are combined into one array: the range of the arr array is l, r. Divided into left array is l, mid, right array is mid+1, r,

Create a new array to traverse the merge, the space complexity is O(n), the time complexity of the constant term is O(n), and the traversal is copied back to the original array arr, which is equivalent to using two O(n) (so From this point of view, slightly lost to fast row)

public static void merge(int[] arr, int l, int m, int r) {
		int[] help = new int[r - l + 1];
		int i = 0;
		int p1 = l;
		int p2 = m + 1;
		while (p1 <= m && p2 <= r) {
			help[i++] = arr[p1] < arr[p2] ? arr[p1++] : arr[p2++];
		}
		while (p1 <= m) {
			help[i++] = arr[p1++];
		}
		while (p2 <= r) {
			help[i++] = arr[p2++];
		}
/ / Copy back to the original array
		for (i = 0; i < help.length; i++) {
			arr[l + i] = help[i];
		}
	}

Main function:

public static void main(String[] args) {
		int []a= {1,6,3,5,9,3,4,8,4,0};
		mergeSort(a,0,a.length-1);
		for(int i=0;i<a.length;i++)
		System.out.println(a[i]);
		
	}

Merging and sorting can achieve stability. In addition, the extra space complexity of the merge sort can be reduced to O(1) but it is difficult

(fast row and heap sorting can not achieve stability)

Intelligent Recommendation

Analysis of the code analysis of the merge algorithm sorting in c and its stability and time complexity

** Merge sort: ** Merge sort code: Merge sort is not the previous sorting mode of sorting from front to back or from back to front in order, but a merged way to sort. It uses the idea of ​​divide and ...

Sorting Algorithm-Merge Sorting Implementation and Time Complexity Analysis

Merge sort Merge sort is a divide and conquer sorting algorithm. The division step is simple: divide the current array into two halves (if N is an even number, it is completely equal, or if N is an od...

Sorting algorithm (two) merge sort Java implementation and analysis

1. Merge Sort The merge operation (merge), also called merge algorithm, refers to the method of merging two sequential sequences into one sequential sequence. Such as   has a number sequence {6, 202, ...

Rapid sorting code implementation and time complexity analysis

Principle analysis Quick sort (bubble sorted upgrade version) Rapid Sorting Principle Analysis: First select a reference element (Pivot), change the array to the left elements on the left side of the ...

More Recommendation

Inner Sorting and Time Complexity Analysis - Insert Sort &amp; Select Sort &amp; Swap Sort &amp; merge sort &amp; assign and index sort comparison...

basic concept What is sorting? Sort Arrange the records in the sequence in order of sorting code The value of the sort code field has an order of no reduction (or no increase) Inner sorting The entire...

Detailed time and space complexity of sorting algorithm plus code

One, select sort Principle: Each time the smallest number is selected from the list and placed in the position from left to right. A list of length n has n positions. For the list [2,3,1], the steps f...

Detailed explanation of merge sorting with code time complexity derivation

Merge sort has a merge() method, which merges two sorted arrays into an ordered array. The time complexity can be stabilized at O(NlogN), and the time complexity is proved as shown in the figure below...

Merge sort of principle and time complexity

Merge sort of the definition Merge sort algorithm uses a divide and conquer algorithm, i.e., the two (or more) into a new sorted list of the ordered list, i.e. the sequence to be sorted into a number ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top