JavaSE - day10 sorting method: selection and bubbling and binary search, etc.

The idea of ​​selection sort: (arrange the minimum value first)
      Selection sort is to compare the first number with the following one. In each comparison, if the 0 index is greater than the latter index, the 0 index is exchanged with the value of the comparison index. In the next comparison, it is only necessary to start from the 1 index and compare with all the following indexes. If the 1 index is greater than the latter index, the 1 index and the value of the comparison index are exchanged. ...until we've compared everything, and the resulting new array is the largest to smallest array.

Code:
//selection sort
public class Demo1 {

	public static void main(String[] args) {
		int[] arr = {23,12,54,67,32,10,2,41};
		System.out.println("Before sorting: ");
		print(arr);
		selectArr(arr);
		System.out.println("After sorting:");
		print(arr);
		
	}
	//traverse
	public static void print(int[] arr) {
		for(int x  = 0 ;x <arr.length;x++) {
			System.out.print(arr[x]+ " ");
		}
		System.out.println();
	}
	//selection sort
	public static void selectArr(int[] arr) {
		for(int y = 0 ;y <arr.length-1;y++) {			
			for(int x = y+1 ; x < arr.length ; x++) {
				if(arr[y]>arr[x]) {
					int temp = arr[y];
					arr[y] = arr[x];
					arr[x] = temp;				
				}
			}	
		}
	}
}


The idea of ​​bubble sort: (arrange the maximum value first)
     Bubble sort compares two adjacent indices, and if the former is greater than the latter, the two positions are swapped. After completing a round of swapping positions, the maximum value appears at the maximum index out. After completing the second round of swapping positions, the second largest value appears at the second largest index. After length-1 rounds of comparison, we have the sorted array.

Code:

public class Demo2 {

	public static void main(String[] args) {
		int[] arr = { 12, 34, 22, 9, 20 };
		System.out.println("Before Bubble Sort: ");
		print(arr);
		bubbling(arr);
		System.out.println("After bubble sort: ");
		print(arr);
	}

	// traverse
	public static void print(int[] arr) {
		for (int x = 0; x < arr.length; x++) {
			System.out.print(arr[x] + " ");
		}
		System.out.println();
	}

	// bubbling
	public static void bubbling(int[] arr) {

		for (int x = 0; x < arr.length - 1; x++) {
			for (int y = 0; y < arr.length - 1 - x; y++) {
				if (arr[y] > arr[y + 1]) {
					int temp = arr[y];
					arr[y] = arr[y + 1];
					arr[y + 1] = temp;
				}
			}
		}
	}

}

The idea of ​​binary search method:
       The premise of using the binary search method is that the array must be ordered . You need to define the maximum index and minimum index of this array first, and the intermediate index. Compare the value to be compared with the intermediate index. If it is greater than the intermediate index, assign the intermediate index to the minimum index; if it is smaller than the intermediate index, assign the intermediate index. give the maximum index. Then perform a new round of comparison, so that we can quickly find the index we need.

 example:  

public class Demo3 {

	public static void main(String[] args) {
		int[] arr = {11,22,33,44,55,66,77,88};
		System.out.println("Index array: ");
		SearchTool.print(arr);
		int num44 = SearchTool.search(arr, 44);
		System.out.println("index44=" +num44);
		
	}

}
class SearchTool{
	public static void print(int[] arr) {
		for(int x= 0 ;x <arr.length;x++) {
			System.out.print(arr[x]+ " ");
		}
		System.out.println();
	}
	public static int search(int[] arr,int value) {
		int min = 0 ;
		int max = arr.length-1;
		int middle = (min+max)/2;
		while(arr[middle] != value) {
			if(arr[middle]>value) {
				max = middle - 1 ;
			}else {
				min = middle + 1 ;
			}
			
			if(max<min) {
				return -1 ;
			}
		}
		return middle;
	}
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324850324&siteId=291194637