【笔记】冒泡,选择,插入三者的排序算法时间复杂度均是O(N2)级,排序主要是执行比较和交换(复制)操作,相对来说:选择排序较冒泡排序减少了交换次数;
而插入排序左边是一直保持有序的。
【参考书籍】Java数据结构与算法
以下是源代码,针对数组的操作,存取元素为基本类型:
1 import java.util.Random;
2
3 class HighArray{
4 private long[] array;
5 private int nElems,i_mark;
6 public HighArray(int max){
7 array=new long[max];
8 nElems=0;
9 }
10 public boolean find(long searchkey){
11 for(i_mark=0;i_mark<nElems;i_mark++)
12 if(array[i_mark]==searchkey)
13 break;
14 if(i_mark==nElems)
15 return false;
16 else
17 return true;
18 }
19 public boolean binarySearch(long searchkey){
20 int lowerBound=0;
21 int highBound=nElems-1;
22 int curIn=0;
23 while(true){
24 curIn=(highBound+lowerBound)/2;
25 if(array[curIn]==searchkey)
26 return true;
27 else if(lowerBound>highBound)
28 return false;
29 else{
30 for(int i=lowerBound;i<highBound;i++){
31 if(array[i]>searchkey)
32 highBound=curIn-1;
33 if(array[i]<searchkey)
34 lowerBound=curIn+1;
35 }
36 }
37 }
38 }//find :1~log^n+1
39 public void insert(long value){
40 array[nElems]=value;
41 nElems++;
42 }
43 public void bubbleSort(){
44 for(int i=0;i<nElems;i++)
45 for(int j=0;j<nElems-1-i;j++)
46 if(array[j]>array[j+1]){
47 swap(j,j+1);
48 }
49 }//sort:O(N*N)
50 public void selectSort(){
51 for(int i=0;i<nElems-1;i++)
52 for(int j=i+1;j<nElems;j++)
53 if(array[i]>array[j])
54 swap(i,j);
55 }//sort:O(N*N),Relatively speaking,it's better than the bubbleSort,because the count of swap
56 public void swap(int one,int two){
57 long temp;
58 temp=array[one];
59 array[one]=array[two];
60 array[two]=temp;
61 }//sort:O(N*N)but better than before two
62
63 public void insertSort(){
64 int in;
65 int out;
66 long temp;
67 for(out=nElems;out>0;out--){
68 in=out;
69 temp=array[in];
70 while(in>0&&array[in-1]>temp){
71 array[in]=array[in-1];
72 in--;
73 }
74 }
75 }
76 public boolean delete(long value){
77 if(find(value)){
78 for(int k=i_mark;k<nElems-1;k++)
79 array[k]=array[k+1];
80 nElems--;
81 return true;
82 }
83 else
84 return false;
85 }
86 public void display(){
87 for(int j=0;j<nElems;j++)
88 System.out.println("array["+j+"] = "+array[j]);
89 }
90 }
91
92 public class ArrayQuery{
93
94 public static void main(String[] args){
95 boolean flag=false;
96 int max=1000;
97 HighArray harr=new HighArray(max);
98 for(int j=0;j<max-1;j++)
99 harr.insert(new Random().nextInt(max));
100 harr.display();
101 if(harr.find(13))
102 System.out.println("we find it: ");
103 else
104 System.out.println("there is no:");
105 if(harr.delete(15)){
106 System.out.println("delete success:");
107 harr.display();
108 }
109 else{
110 System.out.println("delete fail:");
111 harr.display();
112 }
113 long a,b;
114 a = System.nanoTime();;
115 harr.bubbleSort();
116 b = System.nanoTime();;
117 System.out.println("bubbleSort result:time = "+(b-a)+"ns.");
118 //harr.display();
119
120
121 a = System.nanoTime();
122 harr.selectSort();
123 b = System.nanoTime();
124 System.out.println("selectSort result:time = "+(b-a)+"ns.");
125 //harr.display();
126
127 a = System.nanoTime();
128 harr.insertSort();
129 b = System.nanoTime();
130 System.out.println("insertSort result:time = "+(b-a)+"ns.");
131 //harr.display();
132 }
133 }