简单的排序算法

排序算法

1.冒泡排序

(1)比较前后相邻的两个数,如果前面的数大于后面的数,就将这两个数进行交换。就像是小的数据向上冒泡,大的数据往下沉。

(2)遍历次数为N-1次

import java.util.Scanner;
public class BubbleSort {
    public static void upBubbleSort(int []array,int n){ // n 排序的位数
        int i,j;
        int temp;
        for(i=0;i<n;i++){
            for(j =1;j<n-i;j++){
                  if(array[j-1]>array[j]){ // 如果前面的数大于后面就进行交换
                  temp = array[j-1];
                  array[j-1] = array[j];
                  array[j]= temp;
                  }
            }
        }
    }
    public  static void lowBubbleSort(int []array,int n){
        int i,j;
        int temp;
        for(i =0;i<n;i++){
            for(j=1;j<n-i;j++){
                if(array[j-1]<array[j]){ // 如果前面的值小于后面就进行交换
                    temp =  array[j-1];
                    array[j-1] = array[j];
                    array[j] = temp;
                }
            }
        }
    }
}
class Test01{
    public static void main(String [] args){
       Scanner input = new Scanner(System.in);
       int []array = new int [10];
       System.out.println("请你随机输入十个整数");
       for(int i = 0 ;i<array.length;i++){
           array[i] = input.nextInt();
       }
       BubbleSort.upBubbleSort(array,10);
       System.out.println("排序后的整数");
       for(int  i =0;i<array.length;i++){
           System.out.print(array[i]+" ");
       }
    }
}
/* 测试案例
请你随机输入十个整数
9 8 7 6 5 4 3 2 1 0
排序后的整数
0 1 2 3 4 5 6 7 8 9 
Process finished with exit code 0

*/

2.插入排序

通过构建有序序列,对没有排序的数据,在已排序的序列中从后向前扫描,找到相应的位置插入。这类排序有快有慢,如果输入的数据是已经排序好的,则出现最佳排序,它的运行时间是一个输入函数的线性函数。如果输入的是逆序数据则出现最坏的情况时间复杂度为(n2)

import java.util.Scanner;
public class InsertionSort {
       public static void insertionSort(int []array){
           for(int i =1;i<array.length;i++){
               int insertVal =  array[i]; // 插入的数
               int index = i-1; // 插入的数的前一位,
               while(index>=0&&array[index]>insertVal){ // 于前一位数进行比较
                     array[index+1] = array[index]; //将数往后移。
                   index--;
               }
               array[index+1] = insertVal; // 把插入的数放在合适的位置。
           }
       }
}
class Test02{
    public static void main(String [] args){
        Scanner input = new Scanner (System.in);
        int []array = new int [10];
        System.out.println("请你输入十个整数");
        for(int i =0;i<array.length;i++){
            array[i] = input.nextInt();
        }
        InsertionSort.insertionSort(array);
        System.out.println("经插入排序法排序后的十个整数");
        for(int i =0;i<array.length;i++){
            System.out.print(array[i]+" ");
        }
    }
}

3.快速排序法

**原理:**选择一个关键值作为基准值,比基准值小的在左边,大的在右边(一般为无序),基准值一般为数组的第一个元素。

进行一次循环:从后往前比较,用基准值和最后一个值进行比较,如果比基准值小交的交换位置,如果没有继续比较下一个,直到找到第一个比基准值小进行交换。找到这个值后又从前往后比较,如果有比基准值大的交换位置,同理,如果没有继续比较下一个,直到找到第一个比基准值大的交换位置。

import java.util.Scanner;
public class QuickSort {
    public static void quickSort(int []array,int low,int high){
        int start = low;
        int end = high;
        int key = array[low];
        while(end > start){
            // 从后往前比较
            while(end>start&&array[end]>=key){ // 如果没有比关键值小的,比较下一个,
                end--;                 // 直到有比关键值小的然后交换位置
                if(array[end]<=key){
                    int temp = array[end];
                    array[end] = array[start];
                    array[start] = temp;
                }
            }
            // 从前往后比较
            while(end>start&&array[start]<=key){
                start ++;
                if(start>=key){
                   int temp= array[start];
                   array[start] = array[end];
                   array[end] = array[start];
                }
            } // 这时候经过第一次循环,关键值的位置已经确定,左边的值都比关键值小,右边的值都比关键值大,
            // 但是两边的循序可能不一样,进行递归调用。
            if(start>low) quickSort(array,low,start-1); // 递归
            if(end<high) quickSort(array,end+1,high);
        }
    }
}
class Test03{
    public static void main(String [] args){
        int array [] = new int [100];
         Scanner input = new Scanner(System.in);
         int n ;
         System.out.println("请输入你要查排序的个数:");
         n = input.nextInt();
        for(int i =0;i<n;i++){
            array[i] = input.nextInt();
        }
        QuickSort.quickSort(array,0,9);
        System.out.println("排序后的数据");
        for(int i = 0;i<n;i++)
        {
            System.out.print(array[i]+" ");
        }
    }
}
/*
请输入你要查排序的个数:
10
9 8 7 6 5 4 3 2 1 0
排序后的数据
9 8 7 6 5 4 3 2 1 0 
Process finished with exit code 0

*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

“逢雨”

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值