Sort (bubble, bucket sort, quick) (java)

Each project will be used in a basic sort, such as performance ranking, and so on, we can see the importance of sorting, sorting each method has its own characteristics, the choice of which to sort by time, and the efficiency based on how much data resource consumption and other issues to consider, there are a variety of sorting, such as bubble sort, binary sort, quick sort, bucket sort, heap sort, and so on, where mainly to tell you to accept what bubble sort and bucket sort and quick sort.

Bubble Sort

Bubble Sort is more typical of a sort, when we first contact with the sort usually bubble sort, bubble sort is the idea of ​​large numbers or decimals sink to complete the order. Give everyone a look at the source code

Package Sort; 

public  class maopao { 

    public  static  void main (String [] args) {
         int [] = A new new  int [10];                                       // for array assignment, we may be a digital input from the console to be sorted by Scanner 
        int m ;
         for ( int I = 0; I <10; I ++ ) { 
            A [I] = I; 
        } 
        for ( int K = 0; K <a.length; K ++) {                                  // how many number to sort 
            for ( int p = 0; p <a.length-                          1; p ++) {// every how many comparisons 
                IF (a [K]> a = [P]) {                                               // Comparative 
                    m = a [K]; 
                    a [K] = a [P];                                                   // two numbers exchange 
                    a [P] = m; 
                } 
            } 
        } 
        for ( int I: A) { 
            System.out.println (I);                                         // Finally iterate complete sort 
        } 
    } 

}

 

 

Bubble sort is thought to sink large numbers, decimal or sinking, bubble sort time complexity is O (N²), the time complexity is high, bubble sort when using appropriate small quantity, when when a large number, do not recommend the use of bubble sort, a waste of time. For example these numbers, the first number and the second number acquire comparison, 3 <7, without exchanging, 4 and 7 and Comparative 7> exchange position 4 and 7 and 9 are compared, 7 <9, no We need to be exchanged; then left a few numbers do not compare, try it in accordance with the method of the top. Then fake it here first sort. Next introduce one kind.
Here Insert Picture Description

Here Insert Picture Description

Bucket sort

For bucket sort you may not have heard, but the bucket sort has its own advantages, easier to understand, let's take a look bucket sort it. Barrel, can be understood as a container, bucket sort the name suggests is to use some sort the container, it uses this principle to sort, and then look at the source code.

package sort;

import java.util.Scanner;

public class TSort {
    public static void main(String[] args) {
        System.out.println("请输入要排序的数量");
        Scanner sn=new Scanner(System.in);
        int number=sn.nextInt();
        System.out.println("请输入要排序中的最大数");
        int max=sn.nextInt();
        int []array=new int[max+1];
        for(int j=0;j<array.length;j++) {
            array[j]=0;                                                          //定义容器,也就是桶
        }
        System.out.println("请输入要排序的数");
        for(int i=0;i<number;i++) {
            int numbers=sn.nextInt();                                
            array[numbers]++;                                              //表示有numbers这个数放到了array[numbers]里,array[numbers]+1
        }
        sn.close();
        for(int j=0;j<array.length;j++) {
            for(int i=1;i<=array[j];i++) {                           //将有添加进来的数,按顺序输出出去
                System.out.println(j);                            
            }
        }

    }
    }

 

 

桶排序是准备一个连续的数组作为容器,若有数添加进来,则在定义的容器数组相应的地方自加一次,然后添加进来的数相应的数组上的数应该是>=1的,然后依次输出即可。这个方法的弊端已经明显了吧,就是当你要给一个特别大的数进行排序时,需要建立一个特别大的数组,并且还要浪费很多空间,所以当要排序的数很大时,不建议使用这一种方法

快速排序

快速排序大家可能听说过,这种方法用的也比较多,使用这种方法会用到递归这种思想,递归,就是写一个方法然后自己调用自己,设置一个退出条件,完成操作。老规矩,先看源码

package sort;

import java.util.Scanner;

public class QuikSort {
    public static void quickSort(int[] arrars,int left,int right){
            int i,j,temp,t;
            if(left>right){
                return;
            }
            i=left;
            j=right;
            //temp就是基准位
            temp = arrars[left];
     
            while (i<j) {
                //先看右边,依次往左递减
                while (temp<=arrars[j]&&i<j) {
                    j--;
                }
                //再看左边,依次往右递增
                while (temp>=arrars[i]&&i<j) {
                    i++;
                }
                //如果满足条件则交换
                if (i<j) {
                    t = arrars[j];
                    arrars[j] = arrars[i];
                    arrars[i] = t;
                }
     
            }
            //最后将基准为与i和j相等位置的数字交换
            arrars[left] = arrars[i];
            arrars[i] = temp;
            //递归调用左半数组
            quickSort(arrars, left, j-1);
            //递归调用右半数组
            quickSort(arrars, j+1, right);
        }
        public static void main(String[] args){
            int[] arrays = {10,7,2,4,7,62,3,4,2,1,8,9,19};
            quickSort(arrays, 0, arrays.length-1);
            for (int i = 0; i < arrays.length; i++) {
                System.out.println(arrays[i]);
            }
        }
    
//}
}

 

 

Quick sort of thinking that put these numbers into an array, the first to come up with a number, usually take the first number, the number to find the corresponding position, but this position is rather special, requiring a smaller place than this count it's on the left, than its large numbers into the right side of this number, and thus need to set the number of a left and a right side, left side exit conditions is greater than the label label on the right, and then lose out because there is no sense wasting time and space .

Guess you like

Origin www.cnblogs.com/jinlous/p/11725928.html