Exchange sorting (bubbling, fast sorting) (java)

Exchange sorting: Compare the key codes of two records in the sorting table, if it is contrary to the sorting requirement, exchange the two.

1. Bubble sort

1. Basic idea: To treat the sorting sequence from front to back, compare the sort codes of adjacent elements in turn, and exchange if the reverse order is found, so that the elements with larger sort codes gradually move from the front to the back.

2. Code result:

排序之前:
[9, -16, 21, 23, -30, -49, 21, 30, 30]
开始排序
[-16, 9, 21, -30, -49, 21, 23, 30, 30]
[-16, 9, -30, -49, 21, 21, 23, 30, 30]
[-16, -30, -49, 9, 21, 21, 23, 30, 30]
[-30, -49, -16, 9, 21, 21, 23, 30, 30]
[-49, -30, -16, 9, 21, 21, 23, 30, 30]
[-49, -30, -16, 9, 21, 21, 23, 30, 30]
排序之后:
[-49, -30, -16, 9, 21, 21, 23, 30, 30]

3. Graphic:

4. Efficiency analysis:

As can be seen from the original bubbling algorithm,

When the elements to be sorted are in positive order, only one loop is performed, n-1 times are compared, and the number of moving elements is 0;

When the elements to be sorted are in reverse order, it needs to loop n-1 times, compare (n^2-n)/2 times, and move the elements 3*(n^2-n)/2 times;

Therefore, the time complexity is O(n^2); because there are too many moving elements, it is the slower one in inner sorting; because bubble sorting only moves sequentially between elements, it is a stable algorithm.

 

5. Test code:

    Original code:

public class BubbleSort {
	public static void bubbleSort(int[] data) {
		System.out.println("开始排序");
		int arrayLength = data.length;
		for (int i = 0; i < arrayLength - 1; i++) {
			for (int j = 0; j < arrayLength - 1 - i; j++) {
				if (data[j] > data[j + 1]) {
					int temp = data[j + 1];
					data[j + 1] = data[j];
					data[j] = temp;
				}
			}
			System.out.println(java.util.Arrays.toString(data));
		}
	}

	public static void main(String[] args) {
		int[] data = { 9, -16, 21, 23, -30, -49, 21, 30, 30 };
		System.out.println("排序之前:\n" + java.util.Arrays.toString(data));
		bubbleSort1(data);
		System.out.println("排序之后:\n" + java.util.Arrays.toString(data));
	}
}

Optimization 1: Increase the mark. If there is no exchange between j=0 and i, the mark is true, indicating that the interval sequence is already ordered, reducing unnecessary comparisons and improving efficiency.


	//优化1
	public static void bubbleSort1(int[] data) {
		System.out.println("开始排序");
		int arrayLength = data.length;
		for (int i = 0; i < arrayLength - 1; i++) {
			boolean flag = false;
			for (int j = 0; j < arrayLength - 1 - i; j++) {
				if (data[j] > data[j + 1]) {
					int temp = data[j + 1];
					data[j + 1] = data[j];
					data[j] = temp;
					flag = true;
				}
			}
			System.out.println(java.util.Arrays.toString(data));
			if (!flag)
				break;
		}
	}

Optimization 2: Record the position of the last number of each cycle exchange, record the position, and retrieve the position next time (because the sequence from the position to the last position is already in order).

	//优化2
	public static void bubbleSort2(int[] data) {
		System.out.println("开始排序");
		int arrayLength = data.length;
		int i=arrayLength-1;
		int pos=0;
		while(i>0) {

			for (int j = 0; j <i; j++) {
				if (data[j] > data[j + 1]) {
					pos=j;
					int temp = data[j + 1];
					data[j + 1] = data[j];
					data[j] = temp;
				}
			}
			System.out.println(java.util.Arrays.toString(data));
			i=pos;
		}
	}

There are other ways to optimize, here are only the ones that are easier to understand and write.

2. Quick sort (emphasis) (partition exchange sort)

It is the fastest one among all the inline algorithms so far

1. Basic idea: Take any element in the sequence to be sorted as a standard (also called pivot point, boundary point, usually the first element), and divide the element to be sorted into two sub-sequences, left and right. The sorting code of the subsequence elements is less than the sorting code of the reference element, and the sorting code of the right subsequence is greater than or equal to the sorting code of the reference element, and then the two subsequences are divided separately until there is only one element in each sequence. The resulting sequence is an ordered sequence.

2. Icon:

3. A division process:

1.low指向待划分区域首元素,high指向待划分区域尾元素:

2.R[0]=R[low] (为了减少数据的移动将作为标准的元素暂存
到R[0]中,最后再放入最终位置)

3.high从后往前移动直到R[high].key<R[0]key;

4. R[low]=R[high], low++

5.low从前往后移动直到R[low].key>=R[0].key;

6. R[high]=R[low], high--

7. goto 3

8.直到 loW==hgh 时,R[low]=R[0](即将作为标准的元素放到
其最终位置)

概括地说,一次划分就是从表的两端交替地向中间进行扫描
将小的放到左边,大的放到右边,作为标准的元素放到中间。

4. Efficiency analysis:

5. Test code:

public class QuickSort0 {
	private static void swap(int[] data, int i, int j) {
		int temp = data[i];
		data[i] = data[j];
		data[j] = temp;
	}

	private static void subSort(int[] data, int start, int end) {
		if (start < end) {
			int base = data[start];
			int low = start;
			int high = end + 1;
			while (true) {
				while (low < end && data[++low] - base <= 0)
					;
				while (high > start && data[--high] - base >= 0)
					;
				if (low < high) {
					swap(data, low, high);
				} else {
					break;
				}
			}
			swap(data, start, high);
			
			subSort(data, start, high - 1);//递归调用
			subSort(data, high + 1, end);
		}
		System.out.println(java.util.Arrays.toString(data));
	}
	public static void quickSort(int[] data){
		subSort(data,0,data.length-1);
	}
	
	
	public static void main(String[] args) {
		int[] data = { 9, -16, 30, 23, -30, -49, 25, 21, 30 };
		System.out.println("排序之前:\n" + java.util.Arrays.toString(data));
		System.out.println("开始排序");
		quickSort(data);
		System.out.println("排序之后:\n" + java.util.Arrays.toString(data));
	}
}

 

Guess you like

Origin blog.csdn.net/qq_41048982/article/details/109336728