这三种排序有俩个过程:
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)的时间级别。
这三种排序的思想
冒泡排序:在首轮,第一项和第二项比较,将大的放在后面,然后比较第二项和第三项,将大的放在后面,以此类推在首轮结束,最大的数据已经在最后一项了。在一轮轮的比较中,后面的已经排好的数据项越来越多,需要排序的数据项越来越少,直到为零。
选择排序:在冒泡排序上做了优化,减少了交换次数,在首轮选择最小的数放在第一项,一轮之后第一项是有序的了,第二轮从第二项开始选择最小的数放在第二项,以此类推,直到整个数组完全有序。
插入排序:和前俩种排序不同,插入排序在排序过程中是局部有序,随着插入项的增多,有序部分的项的位置会发生改变,而冒泡排序和选择排序每轮确定的项数的位置是永远不变的。在首轮,选择第二项作为插入项,然后取出这一项放在一个变量中,和前一项比较而且小,则前一项后移到第二项的位置,然后第二项也就是插入项放在前一项的位置,第二轮选择第三项作为插入项然后取出和前一项也就是第二项比较如果小,第二项后移到插入项,然后插入相在和第一项比较如果小,则第一项后移到第二项,插入项放在第一项,以此类推。
冒泡排序:
// bubbleSort.java
// demonstrates bubble sort
// to run this program: C>java BubbleSortApp
class ArrayBub
{
private long[] a; // ref to array a
private int nElems; // number of data items
//--------------------------------------------------------------
public ArrayBub(int max) // constructor
{
a = new long[max]; // create the array
nElems = 0; // no items yet
}
//--------------------------------------------------------------
public void insert(long value) // put element into array
{
a[nElems] = value; // insert it
nElems++; // increment size
}
//--------------------------------------------------------------
public void display() // displays array contents
{
for(int j=0; j
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//--------------------------------------------------------------
public void bubbleSort()
{
int out, in;
for(out=nElems-1; out>1; out--) // outer loop (backward)
for(in=0; in
if( a[in] > a[in+1] ) // out of order?
swap(in, in+1); // swap them
} // end bubbleSort()
//--------------------------------------------------------------
private void swap(int one, int two)
{
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}
//--------------------------------------------------------------
} // end class ArrayBub
class BubbleSortApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
ArrayBub arr; // reference to array
arr = new ArrayBub(maxSize); // create the array
arr.insert(77); // insert 10 items
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(); // display items
arr.bubbleSort(); // bubble sort them
arr.display(); // display them again
} // end main()
} // end class BubbleSortApp
选择排序:
// selectSort.java
// demonstrates selection sort
// to run this program: C>java SelectSortApp
class ArraySel
{
private long[] a; // ref to array a
private int nElems; // number of data items
//--------------------------------------------------------------
public ArraySel(int max) // constructor
{
a = new long[max]; // create the array
nElems = 0; // no items yet
}
//--------------------------------------------------------------
public void insert(long value) // put element into array
{
a[nElems] = value; // insert it
nElems++; // increment size
}
//--------------------------------------------------------------
public void display() // displays array contents
{
for(int j=0; j
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//--------------------------------------------------------------
public void selectionSort()
{
int out, in, min;
for(out=0; out
{
min = out; // minimum
for(in=out+1; in
if(a[in] < a[min] ) // if min greater,
min = in; // we have a new min
swap(out, min); // swap them
} // end for(out)
} // end selectionSort()
//--------------------------------------------------------------
private void swap(int one, int two)
{
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}
//--------------------------------------------------------------
} // end class ArraySel
class SelectSortApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
ArraySel arr; // reference to array
arr = new ArraySel(maxSize); // create the array
arr.insert(77); // insert 10 items
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(); // display items
arr.selectionSort(); // selection-sort them
arr.display(); // display them again
} // end main()
} // end class SelectSortApp
插入排序:
// insertSort.java
// demonstrates insertion sort
// to run this program: C>java InsertSortApp
//--------------------------------------------------------------
class ArrayIns
{
private long[] a; // ref to array a
private int nElems; // number of data items
//--------------------------------------------------------------
public ArrayIns(int max) // constructor
{
a = new long[max]; // create the array
nElems = 0; // no items yet
}
//--------------------------------------------------------------
public void insert(long value) // put element into array
{
a[nElems] = value; // insert it
nElems++; // increment size
}
//--------------------------------------------------------------
public void display() // displays array contents
{
for(int j=0; j
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//--------------------------------------------------------------
public void insertionSort()
{
int in, out;
for(out=1; out
{
long temp = a[out]; // remove marked item
in = out; // start shifts at out
while(in>0 && a[in-1] >= temp) // until one is smaller,
{
a[in] = a[in-1]; // shift item to right
--in; // go left one position
}
a[in] = temp; // insert marked item
} // end for
} // end insertionSort()
//--------------------------------------------------------------
} // end class ArrayIns
class InsertSortApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
ArrayIns arr; // reference to array
arr = new ArrayIns(maxSize); // create the array
arr.insert(77); // insert 10 items
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(); // display items
arr.insertionSort(); // insertion-sort them
arr.display();