Use Java (you can also read it in C language) to implement bubble sort and binary search (detailed process diagram) + reverse array

 

Table of contents

1. Bubble sorting

1. Introduction to bubble sorting

2. Sorting ideas

3. Complete code

2. Search by half

1. Half search introduction

2. Search ideas

3. Complete code

3. Array in reverse order

1. Reverse order thinking

2..Complete code


1. Bubble sorting

Bubble sorting is one of many kinds of sorting. It is very common in both C language and Java. It will also be used in data structures later.

1. Introduction to bubble sorting

(1) Bubble sorting idea

  • Sort pairwise. After each sorting, the largest (or smallest) will rise to the end.
  • Each time a round of sorting is completed, one less number needs to be compared.

(2) Bubble sort scenario

  • Mostly used for sorting array contents

2. Sorting ideas

(1) What is needed to complete the sorting

  • There is an array
  • Array length required

(2) Analysis of the sorting process

  • We sort the following array into ascending order

int[] arr = {10,9,8,7,6,5,4,3,2,1};
  • The first bubble sort: 10 numbers need to be compared 9 times to successfully sort a number

  • The second bubble sort: there are 9 numbers to be sorted, so the number of comparisons required is 8

The subsequent number of sorting passes is similar. Next, we summarize the rules.

  • As can be seen from the above figure: the total number of times that 10 numbers need to be compared is 10-1 times, that is, (arr.length-1)
  • The number of comparisons required for the first pass of sorting is 9, and for the second pass it is 8; it is related to the number of passes, so the following code is obtained
 int i =0;
 for (i = 0; i < arr.length-1; i++) {

     for (int j = 0; j < arr.length-1-i; j++) {
        //可自己设置条件条件
         if(arr[j]>arr[j+1]) {
            //完成两个数字的交换
             int tmp = arr[j+1];
             arr[j+1] = arr[j];
             arr[j] = tmp;
          }
      }
  }
  •  The condition for ascending order is that the first number is greater than the second number and it needs to be exchanged; if it is in reverse order, it means that the first number is less than the second number and it needs to be exchanged.

3. Complete code

import java.util.Arrays;

public class Sort{
public static void main(String[] args) {
        //冒泡排序
        int[] arr = {10,9,8,7,6,5,4,3,2,1};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));

    }
    public static void bubbleSort(int[] arr) {
        //升序
        int i =0;
        for (i = 0; i < arr.length-1; i++) {

            for (int j = 0; j < arr.length-1-i; j++) {
                if(arr[j]>arr[j+1]) {
                  int tmp = arr[j+1];
                  arr[j+1] = arr[j];
                  arr[j] = tmp;

                }
            }
        }
    }
}

2. Search by half

1. Half search introduction

(1) Half search, also known as binary search.

(2) Binary search starts from the middle position every time, so it is called half search (binary search)

(3) Conditions under which binary search can be performed

  • The data in the array must be ordered
  • If it is out of order, you can put it in order first

2. Search ideas

(1) How to write the parameters of the method

  • Let us stipulate: if it is found, return its subscript position, otherwise return -1
  • You need to pass the array and the data you want to find to the following method
public static int binarySearch(int[] arr,int k) {
       
    }

(2) Internal details to find

  • Positioning subscript: Determine the positions of both ends to facilitate finding the middle position

 public static int binarySearch(int[] arr,int k) {
        int left = 0;
        int right = arr.length-1;
    }
  • The middle number means:
 int mid = (left+right)/2;

(3) Process analysis of binary search 

  •  Look at the code first
 public static int binarySearch(int[] arr,int k) {
        int left = 0;
        int right = arr.length-1;
        while(left <= right) {
            //从中间位置开始找
            int mid = (left+right)/2;
            if(k < arr[mid]) {//k在左边
                right=mid-1;
            } else if(k > arr[mid]) {
                //在右边
                left=mid+1;
            } else {
                return mid;
            }
        }
        return -1;
    }
  • Illustration of the search process: first search

  • Second search: After the search, right points to 10, left and mid both point to 8

  • The third and fourth searches: if found, return the subscript of the mid position.

3. Complete code

public static void main(String[] args) {
        //1.折半查找,要求数组内容为有序.找到了返回下标
        int[] arr1 = {2,5,7,8,10,11,15,17,20,22};
        int ret = binarySearch(arr1,10);
        System.out.println(ret);
        //2.当数组无序时,使用Array.sort排序后折半查找
        int[] arr2 = {9,8,7,6,5,4,3,2,1,0};
        Arrays.sort(arr2);
        System.out.println(Arrays.toString(arr2));
        int cur = binarySearch(arr2,11);
        System.out.println(cur);
    }
    public static int binarySearch(int[] arr,int k) {
        int left = 0;
        int right = arr.length-1;
        while(left <= right) {
            //从中间位置开始找
            int mid = (left+right)/2;
            if(k < arr[mid]) {//k在左边
                right=mid-1;
            } else if(k > arr[mid]) {
                //在右边
                left=mid+1;
            } else {
                return mid;
            }
        }
        return -1;
    }

3. Array in reverse order

1. Reverse order thinking

(1) It is required to reverse the order of the array contents, not to print them in reverse order.

int[] arr = {10,9,8,7,6,5,4,3,2,1,0};

(2) Set the head and tail positions

 public static void reverse(int[] arr) {
        int left = 0;//头位置
        int right =arr.length-1;//尾位置 
    }

This is to exchange data from both ends

(3) Circular exchange of data

 while(left < right) {//相等的时候就不需要逆序了
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            left++;
            right--;
        }

The internal code actually implements the exchange of two numbers; when left==right, there is only one data left that has not been exchanged. In fact, there is no need to exchange anymore (exchange is OK)

2..Complete code

 public static void main(String[] args) {
        //逆序数组
        int[] arr = {10,9,8,7,6,5,4,3,2,1,0};
        reverse(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void reverse(int[] arr) {
        int left = 0;
        int right =arr.length-1;
        while(left < right) {//相等的时候就不需要逆序了
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            left++;
            right--;
        }

    }

Guess you like

Origin blog.csdn.net/2301_77053417/article/details/134188575