几种基础的排序方法

写排序demo要在test里新建class 不是在main里
在这里插入图片描述
想要在控制台打印 需要加test注解
在这里插入图片描述

冒泡排序
package com.jia.mysort;

import org.junit.Test;
/**
 * 冒泡排序
 */
public class MyBubble {
    @Test
    public void bubbleSort(){
        //定义一个数组
        int []source = {2,8,6,1,9,4,5};
        //外层,用于记录和控制第几轮循环
        for (int i = 0; i < source.length-1; i++) {
            //内层,用于控制每轮循环的次数
            for (int j = 0; j < source.length-1-i; j++) {
                //如果前面的数大于后面的数,则交换位置
                if(source[j]>source[j+1]){
                    int temp = source[j];
                    source[j]=source[j+1];
                    source[j+1]=temp;
                }
            }
        }
        //循环输出
        for (int i = 0; i < source.length; i++) {
            System.out.print(source[i]+"     ");
        }
    }

}

简单选择排序
package com.jia.mysort;

import org.junit.Test;

public class MySelect {
    @Test
    /**
     * 简单选择排序
     */
    public void selectSort(){
        int []source = {5,2,1,3,6,4,9};
        //外层,用于控制第几轮循环
        for (int i = 0; i < source.length - 1; i++) {
            // 内层,用于控制每轮循环次数
            for (int j = i+1; j < source.length; j++) {
                //如果第i个数大于后面的数,则交换位置
                if(source[i]>source[j]){
                    int temp = source[i];
                    source[i] = source[j];
                    source[j] = temp;
                }
            }
        }
        for (int i = 0; i < source.length; i++) {
            System.out.print(source[i]+"    ");
        }
    }

}

二分法查找
一定要是在排好序的情况下进行查找
在test里写demo时要用system.out.print进行输出 而不是用log打印

package com.jia.mysort;

import android.util.Log;

import org.junit.Test;
/**
 * 二分法查找
 * @return
 */
public class MyDichotomy {
    @Test
    public void twoPointQuery(){
        int []source = {1,2,3,4,5,6};
        int num = 5;//查找5的索引
        int start = 0;//查找的起始角标
        int end = source.length-1;//查找的结尾角标
        int mid = (start+end)/2;//查找的中间角标
        while (num!=source[mid]){
            if(source[mid]>num){
                end = mid-1;
            }else if (source[mid]<num){
                start = mid+1;
            }
            //防止死循环
            if(start>end){
                mid = -1;// -1代表没有这个数字
                break;
            }
            mid = (start + end)/2;
        }
        // 循环结束的时候,mid的值即为要查找的角标值
        System.out.print("要查找的num的角标为:"+mid);
    }

}

快速排序

package com.jia.mysort;

import org.junit.Test;

public class MyQuick {
    @Test
    public void quick(){
        int[] list = {6, 1, 2, 7, 9, 3, 4, 5, 10, 8};
        System.out.println("************快速排序************");
        System.out.println("排序前:");
        display(list);

    System.out.println("排序后:");
    quickSort(list, 0, list.length - 1);
    display(list);
}

/**
 * 快速排序算法
 */
public static void quickSort(int[] list, int left, int right) {
    if (left < right) {
        // 分割数组,找到分割点
        int point = partition(list, left, right);

        // 递归调用,对左子数组进行快速排序
        quickSort(list, left, point - 1);
        // 递归调用,对右子数组进行快速排序
        quickSort(list, point + 1, right);
    }
}

/**
 * 分割数组,找到分割点
 */
public static int partition(int[] list, int left, int right) {
    // 用数组的第一个元素作为基准数
    int first = list[left];
    while (left < right) {
        while (left < right && list[right] >= first) {
            right--;
        }
        // 交换
        swap(list, left, right);

        while (left < right && list[left] <= first) {
            left++;
        }
        // 交换
        swap(list, left, right);
    }
    // 返回分割点所在的位置
    return left;
}

/**
 * 交换数组中两个位置的元素
 */
public static void swap(int[] list, int left, int right) {
    int temp;
    if (list != null && list.length > 0) {
        temp = list[left];
        list[left] = list[right];
        list[right] = temp;
    }
}

/**
 * 遍历打印
 */
public static void display(int[] list) {
    System.out.println("********展示开始********");
    if (list != null && list.length > 0) {
        for (int num : list) {
            System.out.print(num + " ");
        }
        System.out.println("");
    }
    System.out.println("********展示结束********");
}

}

猜你喜欢

转载自blog.csdn.net/aijaijgnaw/article/details/84539750
今日推荐