[Java → bubble sort and binary searching] lazy black notes 08 (a white, please keep pointing)

Disclaimer: This note by watching [sentiment] Shangxue Tang collated, if you have any questions, please contact the authors indicate the source!

6.4 bubble sort algorithm basis

Bubble sort is common sorting algorithm, written in very common.
Algorithm repeatedly visited the number of columns to sort, compare the first two elements, if they put them in the wrong order exchange, the greater the elements would have been exchanged slowly "bubble" to the top.

step:
1, the first comparison start two adjacent elements if the first element is larger than the second, then the switch sequence.
2, do the same work for each pair of adjacent elements, from the beginning to the end of the first pair. The final element is the maximum number.
3, repeat the above steps for all the elements, except the last one.
4, in the end none of the elements you need to compare, sorting is completed.

[Below]
Bubble sort schematic
also observed following FIG dynamic connection, relatively clear, easily understood: [ https://imgconvert.csdnimg.cn/aHR0cHM6Ly9pbWFnZXMyMDE3LmNuYmxvZ3MuY29tL2Jsb2cvODQ5NTg5LzIwMTcxMC84NDk1ODktMjAxNzEwMTUyMjMyMzg0NDktMjE0NjE2OTE5Ny5naWY ]

Disclaimer: This motion picture is reproduced in
Author: yyy Susu [ https://blog.csdn.net/qq_41679818/article/details/90296399 ] this article!

[Test] bubble sort example

		//冒泡排序
		
		int[] b = {2,1,3,4,8,0,9,7,5};
		
		int temp = 0;
		
		for(int j = 0;j<b.length;j++) {
			boolean flag = true;
			for(int i = 0;i<b.length-1-j;i++) {
				/*
				 * 注意:i<b.length会出现数组溢出异常,
				 * 因为当比较到最后一位时是if(b[4]>b[5]),
				 * 但数组定义没有b[5].所以会发生错误!
				 * 
				 */
				if(b[i]>b[i+1]) {
					temp = b[i+1];
					b[i+1] = b[i];
					b[i] = temp;
				}
				System.out.println(Arrays.toString(b));
				//当只有一个循环是得到最大元素,还需要嵌套一个循环,
				//把前面的元素再重复比较

【Results of the】
01 results02 results03 results

6.4.1 bubble sort algorithm optimization

Initially, the entire sequence is disordered, as the order number of empty cycles every time when the largest element will be pushed to the bottom, but does not take into account already recirculated exhaust has good ordered sequence (observed as a result of code execution).Therefore, to judge whether or not each pass occurs exchange the array elements, if not occurred, indicating that the array has been ordered at this time, no need to compare the number of subsequent times. At this point you can abort the comparison.

Examples of optimization algorithms [test] bubble sort

//冒泡排序(优化算法)
		
		int[] b = {2,1,3,4,8,0,9,7,5};
		
		int temp = 0;
		
		for(int j = 0;j<b.length;j++) {
			boolean flag = true;
			for(int i = 0;i<b.length-1-j;i++) {
				/*
				 * 注意:i<b.length会出现数组溢出异常,
				 * 因为当比较到最后一位时是if(b[4]>b[5]),
				 * 但数组定义没有b[5].所以会发生错误!
				 * 
				 */
				
				//比较大小,换顺序
				if(b[i]>b[i+1]) {
					temp = b[i+1];
					b[i+1] = b[i];
					b[i] = temp;
					
					flag = false;
				}
				System.out.println(Arrays.toString(b));
				//当只有一个循环是得到最大元素,还需要嵌套一个循环,
				//把前面的元素再重复比较
				
			}
			if(flag) {
				System.out.println("结束!!");
				break;
			}
			System.out.println("------------");
			
		}

【Results of the】
01 results02 results03 results

6.5 a binary search (binary search)

Also known as a binary search binary search. Retrieval is a prerequisite: the elements of the array from small to large to be stored in an array of sort, given the need to retrieve the middle element of the array elements (key), and if equal, the search succeeds!

If smaller than the key, binary search is performed in the first half of the array;
if larger than the key, binary search is performed in the latter half of the array.
【schematic diagram】
schematic diagram

Examples of binary search [Test]

import java.util.Arrays;
/**
 * 二分法查找
 * @author 卟淡
 *
 */
public class ArrayBinarySeach {
	public static void main(String[] args) {
		int [] test = {15,12,16,10,17,19,18};
		Arrays.sort(test);	//排序(升序)
		
		System.out.println(Arrays.toString(test));	//输出完整数组
		
		System.out.println("该元素的位置为:" + Test(test,19));	
	}
	
	public static int Test(int[] test,int value) {
		//初始化开头结尾变量
		int low  = 0;
		int high = test.length-1;
		
		while(low <= high) {
			int mid = (low+high)/2;
			
			if(value==test[mid]) {
				return mid;
			}
			if(value > test[mid]) {	//检索的数比中间数大,则在右边取半进行检索
				low = mid +1;//中间一位加1赋回给low,再加最后一位位置数除以2,再继续向右进行折半检索。
			}
			if(value < test[mid]) {
				high = mid-1;//中间一位减1赋回给high,再加第一位元素位置(0),在继续向左折半检索。
			}
			
		}
		return -1;	//若low>high,则无法找到检索的数,返回-1值
		
	}
}

Description:

If you retrieve larger than the middle number, then retrieved the right to take half, the middle one plus one assigned back to the low, coupled with the last digit position divided by 2, and then continue binary search to the right. If the search is smaller than the number of intermediate numbers, taking the left half is performed to retrieve, forming an intermediate back to a minus high, an element plus the first position (0), the binary search continues to the left.

Released nine original articles · won praise 2 · Views 437

Guess you like

Origin blog.csdn.net/weixin_45625687/article/details/104784463