Java [sort]

Bubble Sort []
1. Principle
adjacent elements pairwise comparison, the next big release, after the first relatively complete, the maximum it has lined up last.
Source array: {} 3,8,15,6,2
first sort: 8 and Comparative 3 3 <8, the position of the invariant 3
8 8 and 15 to <15, 15 so that the position of the same
15 and 6 than 15> 6, 15 and 6 so that the exchange position
15 and then switched to 15 2> 24,15 and a second position
results mechanized sorting the first time is obtained: {3,8,6,2,15}
second sorting as above, the result is obtained: {3,6,2,8,15}
third Sort: {3,2,6,8,15}
fourth Sort: {2,3,6,8, 15}
2. law: pairwise comparison, put back large, gradually decrements the number of comparisons.

public class Bubble {
	public static void main(String[] args) {
		int[] a = {3,8,15,6,2};
		for(int n = 1;n <= a.length - 1;n++) {//一共要比较数组长度减1次
		for(int i = 0;i < a.length -n;i++) {
			//如果前一个元素比后一个元素大,那么就需要交换着两个元素的位置
			if(a[i] > a[i + 1]) {
				int temp = a[i];
				a[i] = a[i + 1];
				a[i + 1] = temp;
				}
			}
		}
		for(int i = 0;i < a.length ;i++) {
			System.out.println(a[i]);
		}
	}
}

Select [Sort]
1. Principle:
The first trip to first find the array, the index of the smallest element,
then it and the first element exchange position,
and then find the index of the smallest element in the rest of the elements, the exchange position again.
Be sorted array: {} 3,8,15,6,2
first sort: minimum data is 2, the two first, i.e. the exchange 2 and 3 positions
sorted result: {2,8,15,6 , 3}
the second sort: the data other than the {2} sort 8,15,6,3, minimum 3, 3 and 8 exchange position of the
sorted result: {} 2,3,15,6,8
of three sort: the data other than 2, 3 (15,6,8) to sort, minimum 6, the 6 position of the exchange 15
sorting results: {2,3,6,15,8}
fourth Sort: 2,3,6 data other than the (15,8) to sort, 8 the smallest, the 8 position of the exchange 15
results Sort: {2,3,6,8,15}
Note: each sort to obtain the minimum number of methods were compared for loop

public class Select {
	public static void main(String[] args) {
		int[] a = {6,4,2,7,32,12,45};//定义一个数组进行排序
		/*
		 * 遍历一次数组,保存查找最小元素的索引
		 */
		for(int i = 0;i < a.length - 1;i++) {
			int min = i;
		/*
		 * 再遍历一次数组,再次找出最小元素的索引
		 */
			for(int j = a.length - 1;j > i;j--) {
				if(a[j] < a[min]) {
					min = j;
				}
			}
			int temp = a[i];
			a[i] = a[min];
			a[min] = temp;
		}
		for(int i = 0;i < a.length;i++) {
		System.out.print(a[i] + " ");
		}	
	}
}

Insertion sort
1. Insert sort similar to the process we usually play poker, we feel every time cards, will contrast the hands of the existing cards, this card is then placed in the appropriate location.
2. The core idea:
to be the sort of n elements as an ordered sequence and disorderly series, beginning, ordered sequence only one element, the number of columns in the disorder have n - 1 elements;
each from random number sequences take the first element, which is inserted into the ordered sequence to make it a new ordered list, repeated n- 1 time to complete the ordering process.
Be sorted array: {} 48,32,2,54,23
(1) is inserted first: First, {48} as an ordered array, 32,2,54,23 {}, the first step requires do is the first element 32 is inserted into unordered array to an ordered array, the result is obtained from the first sort 32,48,2,54,23 {};
(2) second insertion: the {32, 48} as a sorted array, {2,54,23} as a disordered array, but the first element 2 is inserted into the ordered array disordered array
above operation is repeated: can be found ordered array index after last element is not inserted into the end of the first will increase by one.

public class 插入排序2 {

	public static void main(String[] args) {
		int[] a = { 48,32,2,54,23};
		/*
		 * 声明一个变量index来保存有序数组最后一个元素的索引
		 */
		for(int index = 0;index < a.length - 1;index++){
		/*
		 * 再声明一个变量来保存无序数组需要插入到有序数组的元素值
		 * 也就是需要插入的元素值
		 * 这里的index + 1 表示初始准备插入位置的索引
		 * 这个索引就是有序数组最后一个位置的索引+1
		 * 具体插在那个地方需要进行比较
		 */
		int temp = a[index + 1];
		/*
		 * 插入元素的索引位置
		 */
		int insert_index = index + 1;
		/*
		 * 寻找插入位置
		 * 从有序数组最后一个元素开始向前面做比较
		 */
		for(int i = index;i >= 0;i--) {
			if(temp < a[i]) {
				insert_index = i;
			}else {
				break;
			}
		}
		/*
		 * 找到插入位置之后,需要将这个位置之后的元素依次向后移动
		 */
		for(int i = index;i >= insert_index;i-- ) {
			a[i + 1] = a[i];
		}
		a[insert_index] = temp;
		}
		for(int i = 0;i < a.length;i++) {
			System.out.print(a[i] + " ");
		}
	}

}

Guess you like

Origin blog.csdn.net/weixin_44084434/article/details/90139334