Common sorting algorithms

Keywords: Java Algorithm data structure

1. Bubble sorting

thought

Traverse the array. Compare and adjust the two adjacent numbers from front to back for all the numbers in the range that have not been ordered at present. The larger number sinks back and the smaller number rises forward. Each traverse will sink the largest number. The sunk numbers do not need to participate in the comparison, so the number of comparisons in each round is gradually reduced.

realization

public class Demo {
	
	public static void main(String[] args) {
		int[] a = {3,-1,9,7,-5,4,13,6,2};
		System.out.println(Arrays.toString(a));
		//bubble(a);
		bubble2(a);
		System.out.println(Arrays.toString(a));
	}

	// (1) From front to back, most of them sink
	private static void bubble(int[] a) {
		for(int i=0;i<a.length-1;i++) {
			for(int j=0;j<a.length-1-i;j++) {
				if(a[j]>a[j+1]) {
					swap(a,j,j+1);
				}
			}
		}
	}
	
	// (2) From the back to the front, decimals rise
	private static void bubble2(int[] a) {
		for(int i=0;i<a.length-1;i++) {
			for(int j=a.length-1;j>i;j--) {
				if(a[j]<a[j-1]) {
					swap(a, j, j-1);
				}
			}
		}
	}
	
	/**
	 * Swap the values of two index positions in the array
	 */
	private static void swap(int[] a, int m, int n) {
		int t = a[m];
		a[m] = a[n];
		a[n] = t;
	}
}

Time complexity

  • Comparison times: n(n-1)/2
  • Time complexity: O(n^2) square order

2. Select sort

thought

First, find the smallest element in the array and exchange its position with the first element in the array; Again, find the smallest element in the remaining elements, exchange it with the second element in the array, and repeat until the whole array is in order.

realization

public class Demo {
	
	public static void main(String[] args) {
		int[] a = {3,-1,9,7,-5,4,13,6,2};
		System.out.println(Arrays.toString(a));
		selection(a);
		System.out.println(Arrays.toString(a));
	}

	private static void selection(int[] a) {
		for(int i=0;i<a.length-1;i++) {
			// Assume the i th smallest
			int min = i;
			// Traversal array and minimum value comparison
			for(int j=i+1;j<a.length;j++) {
				if(a[j]<a[min])
					// Record the index of the minimum value
					min = j;
			}
			// Optimization: if the original assumption is the minimum value, there is no need to exchange
			if(min!=i)
				swap(a, min, i);
		}
	}

	/**
	 * Swap the values of two index positions in the array
	 */
	private static void swap(int[] a, int m, int n) {
		int t = a[m];
		a[m] = a[n];
		a[n] = t;
	}
}

Time complexity

  • Exchange N times
  • Compare n(n-1)/2 times
  • Time complexity O(n^2) square order

3. Insert sort

thought

In the elements to be arranged, assuming that the number of n-1 in front is already ordered, insert the N number into the ordered sequence and adjust the position, so that the sequence of insertion n is ordered.

realization

public class Demo {
	
	public static void main(String[] args) {
		int[] a = {3,-1,9,7,-5,4,13,6,2};
		System.out.println(Arrays.toString(a));
		insertion(a);
		System.out.println(Arrays.toString(a));
	}

	private static void insertion(int[] a) {

		for(int i=1;i<a.length;i++) {
			// Start position of target element
			int index = i;
			for(int j=i-1;j>=0;j--) {
				if(a[index]>=a[j])
					break;
				else {
					swap(a, j, index);
					// After the exchange, the position of the target element changes
					index = j;
				}
			}
		}
	}

	/**
	 * Swap the values of two index positions in the array
	 */
	private static void swap(int[] a, int m, int n) {
		int t = a[m];
		a[m] = a[n];
		a[n] = t;
	}
}

Time complexity

  • In the case of array order: compare n-1 times, exchange 0 times, time complexity O(n)
  • Array clutter: O(n^2)
  • Time complexity: O(n) ~ O(n^2)

4. Quick sort

thought

The sorting algorithm of divide and conquer takes a segmented element (generally the first or last element), then scans right from the left end of the array until an element greater than or equal to it is found, and then scans left from the right end of the array until an element less than or equal to it is found. Exchange the elements at the left and right pointer positions, and continue scanning at both ends until the left and right pointers meet, Put the segmented element at this position, recursively segment the arrays on both sides of the element, and repeat the above steps.

realization

Time complexity

5. Merge sort

thought

realization

Time complexity

Posted by drorgo on Fri, 03 Sep 2021 22:14:33 -0700