java冒泡排序

java面向对象的冒泡排序,选择排序和插入排序的比较

这三种排序有俩个过程:

  1.比较俩个数据。                                     

  2.交换俩个数据或复制其中一项。

这三种排序的时间级别

  冒泡排序:比较 (N-1)+(N-2)+...+2+1 = N*(N-1)/2=N2/2

       交换  0——N2/2 = N2/4

       总时间 3/4*N2

  选择排序:比较 (N-1)+(N-2)+...+2+1 = N*(N-1)/2=N2/2

       交换  0——3*(N-1)=3*(N-1)/2=3/2*N

       总时间 N2/2+3/2*N

  插入排序:第一轮最多比较一次,第二轮最多比较俩次,最后一轮比较N-1次,所以最多比较N*(N-1)/2。

       复制的次数和比较的次数大致相等,但是复制消耗的时间比交换要小.

       比较 0——N*(N-1)/2=N*(N-1)/4=N2/4

       复制 0——N*(N-1)/2=N*(N-1)/4=N2/4

       总时间 N2//2

  总结:插入排序算法比冒泡快一倍,比选择排序略快一点,但这些算法都是O(N2)的时间级别。

  

这三种排序的思想  

  冒泡排序:在首轮,第一项和第二项比较,将大的放在后面,然后比较第二项和第三项,将大的放在后面,以此类推在首轮结束,最大的数据已经在最后一项了。在一轮轮的比较中,后面的已经排好的数据项越来越多,需要排序的数据项越来越少,直到为零。

  选择排序:在冒泡排序上做了优化,减少了交换次数,在首轮选择最小的数放在第一项,一轮之后第一项是有序的了,第二轮从第二项开始选择最小的数放在第二项,以此类推,直到整个数组完全有序。

  插入排序:和前俩种排序不同,插入排序在排序过程中是局部有序,随着插入项的增多,有序部分的项的位置会发生改变,而冒泡排序和选择排序每轮确定的项数的位置是永远不变的。在首轮,选择第二项作为插入项,然后取出这一项放在一个变量中,和前一项比较而且小,则前一项后移到第二项的位置,然后第二项也就是插入项放在前一项的位置,第二轮选择第三项作为插入项然后取出和前一项也就是第二项比较如果小,第二项后移到插入项,然后插入相在和第一项比较如果小,则第一项后移到第二项,插入项放在第一项,以此类推。

这三种排序的java程序

  1.冒泡排序

复制代码
package aa;
class BubbleSort{
    private long[] a;
    private int nElems;
    
    public BubbleSort(int max){
        a = new long[max];
        nElems = 0;
    }
    
    public void insert(long value){
        a[nElems] = value;
        nElems ++;
    }
    
    public void display(){
        for(int i = 0; i < nElems; i++)
        {
            System.out.println(a[i] + " ");
        }
        System.out.println("");
    }
    
    public void bubbleSort(){
        for(int i = 0; i < nElems - 1; i++)
        {
            for(int j = 0; j < nElems - 1 - i; j++)
            {
                if(a[j] > a[j + 1])
                {
                    long temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
    }
}
public class BubbleSortApp {
    public static void main(String[] args){
        BubbleSort arr = new BubbleSort(100);
        arr.insert(77);
        arr.insert(99);
        arr.insert(44);
        arr.insert(55);
        arr.insert(22);
        arr.insert(88);
        arr.insert(11);
        arr.insert(00);
        arr.insert(66);
        arr.insert(33);
        arr.display();
        arr.bubbleSort();
        arr.display();
    }    
}
复制代码

   2.选择排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package aa;
 
class SelectSort{
    private long[] a;
    private int nElems;
     
    public SelectSort(int max){
        a = new long[max];
        nElems = 0;
    }
     
    public void insert(long value){
        a[nElems] = value;
        nElems ++;
    }
     
    public void display(){
        for(int i = 0; i < nElems; i++)
        {
            System.out.println(a[i] + " ");
        }
        System.out.println("");
    }
     
    public void SelectionSort(){
        for(int i = 0; i < nElems - 1; i++)
        {
            int min = i;
            for(int j = i + 1; j < nElems; j++)
            {
                if(a[j] < a[min])
                {
                    long temp = a[j];
                    a[j] = a[min];
                    a[min] = temp;
                }
            }
        }
    }
}
 
public class SelectSortApp {
    public static void main(String[] args){
        SelectSort arr = new SelectSort(100);
        arr.insert(77);
        arr.insert(99);
        arr.insert(44);
        arr.insert(55);
        arr.insert(22);
        arr.insert(88);
        arr.insert(11);
        arr.insert(00);
        arr.insert(66);
        arr.insert(33);
        arr.display();
        arr.SelectionSort();
        arr.display();
    }  
}

  3.插入排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package aa;
class InsertSort{
    private long[] a;
    private int nElems;
     
    public InsertSort(int max){
        a = new long[max];
        nElems = 0;
    }
     
    public void insert(long value){
        a[nElems] = value;
        nElems ++;
    }
     
    public void display(){
        for(int j = 0; j < nElems; j++)
        {
            System.out.println(a[j] + " ");
        }
        System.out.println("");
    }
     
    public void insertionSort(){
        int out,in;
        for(out = 1; out < nElems; out++)
        {
            long temp = a[out];
            in = out;
            while(in > 0 && a[in-1] >= temp)
            {
                a[in] = a[in - 1];
                in --;
            }
            a[in] = temp;
        }
    }
}
public class InsertSortApp {
    public static void main(String[] args){
        int maxSize = 100;
        InsertSort arr = new InsertSort(maxSize);      
        arr.insert(77);
        arr.insert(99);
        arr.insert(44);
        arr.insert(55);
        arr.insert(22);    
        arr.insert(88);
        arr.insert(11);
        arr.insert(00);
        arr.insert(77);
        arr.insert(77); <br>
        arr.display();
        arr.insertionSort();
        arr.display();
    }
     
}
原文地址:https://www.cnblogs.com/pressur/p/11200607.html