-----------------------------------------------------------------------
线性查找O(N):与N成正比 T=K*(N/2)
二分查找O(logN):与log(N)成正比 T=k*log(N)
无序数组插入O(1):常数 T=K
有序数组插入O(N)
无序数组删除O(N)
有序数组插入O(N)
O(1) 优秀
O(logN) 良好
O(N) 还可以
O(N^2) 差一些了
----------------------------------------------------------------------------------------
冒泡 选择 插入
希尔 快速
****冒泡排序
N个人队列,角标0~N-1,从低到高排队。
从队列最左边开始,比较0 1, 0高 则两数交换。1高则什么也不做。然后移一个位置比较1 2。
左边的高 就交换位置。以此类推。第一趟(N-1次比较)排序之后,最高的人处于队伍的最右边。
规则:
1 比较两个队员(0、1)如果左边的队员高,则交换位置
2 向右移动一个位置,比较下面的两个队员(1、2)
一直比较到队列的最右端,虽然没有把所有的队员都排好序,但是最高的队员已经处于队列的最右边了。
第一趟(N-1次比较)排序之后,最高的人处于队伍的最右边。(最大的项 ‘冒泡’ 到数组的顶端),
数组最顶端的数据确定。无需再排。
第二趟排序 找出 第二高的,放在n-2的位置。
关键代码<理解>:
public void bubbleSort() {
int out, in;
for(out=nElems-1; out>1; out--) // outer loop (backward <---)
for(in=0; in<out; in++) // inner loop (forward --->)
if( a[in] > a[in+1] ) // out of order?
swap(in, in+1); // swap them
} // end bubbleSort()
效率O(n^2):
N个数据项,第一趟排序n-1此比较,第二趟n-2 以此类推....
(n-1)+(n-2)+...+1 = ( n*(n-1) )/2 约做了 n^2/2次比较
****选择排序
选择排序改进了冒泡排序,将必要的交换次数从O(N^2)减少到O(N)但是比较次数仍未O(N^2)
规则:
先把所有队员扫描一趟,从中挑出(选择)最矮的队员,将其和最左边的队员互换位置,
这样最矮的人再第一个位置 确定下来,再从其余的队员中(选择出)最矮的放在第二个位置。以此类推。
更细节描述:
排序从最左边开始,记录下最左边球员身高,紫毛巾放在其前面。
用下一个球员的身高与第一个相比,如果这个更矮则第一个的身高,记下此人身高,
将紫毛巾放在其前面。否则不变。继续演这个队列走下。
走完第一趟,最矮的一个人就确定了,然后和处在0位置的问进行交换。
在下一趟排序中所做的事是一模一样的,只是忽略掉最左边的一个队员。
关键代码:
public void selectionSort(){
int out, in, min;
for(out=0; out<nElems-1; out++) // outer loop -->
{
min = out; // minimum min角标
for(in=out+1; in<nElems; in++) // inner loop -->
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()
效率O(n^2):
但是选择排序 比冒泡排序快 。
将必要的交换次数从O(N^2)减少到O(N)但是比较次数仍未O(N^2)
****插入排序
虽然插入排序算法仍然需要O(N^2)时间,但一般情况下,比冒泡排序快一倍。比选择排序还快一点。
1.标记队员左边的局部有序,标记队员和右边的都没有排过序。
2.在局部有序中找到合适和位置插入被标记的队员。(需要部分已排序队员 右移 腾出位置存放被标记的队员)
为提供移动所需空间,就先让被标记的队员出列(程序中,这个数据项被存在一个临时变量中)
3.排完之后,被标记队员 和左边的 有序队列 都拍好了。
4.将上次标记队员的 下一个队员 作为 标记队员。开始新的循环。以此类推。
关键代码:
public void insertionSort()
{
int in, out;
for(out=1; out<nElems; out++) // out is dividing line
{
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()
效率:
O(N^2)
****对象排序
前面的排序算法使用基本数据类型,但排序却更多用于对象排序。而不是对基本数据类型排序。
---------------------------------------------------------------------------------------------------------------------------------------------
线性查找O(N):与N成正比 T=K*(N/2)
二分查找O(logN):与log(N)成正比 T=k*log(N)
无序数组插入O(1):常数 T=K
有序数组插入O(N)
无序数组删除O(N)
有序数组插入O(N)
O(1) 优秀
O(logN) 良好
O(N) 还可以
O(N^2) 差一些了
----------------------------------------------------------------------------------------
冒泡 选择 插入
希尔 快速
****冒泡排序
N个人队列,角标0~N-1,从低到高排队。
从队列最左边开始,比较0 1, 0高 则两数交换。1高则什么也不做。然后移一个位置比较1 2。
左边的高 就交换位置。以此类推。第一趟(N-1次比较)排序之后,最高的人处于队伍的最右边。
规则:
1 比较两个队员(0、1)如果左边的队员高,则交换位置
2 向右移动一个位置,比较下面的两个队员(1、2)
一直比较到队列的最右端,虽然没有把所有的队员都排好序,但是最高的队员已经处于队列的最右边了。
第一趟(N-1次比较)排序之后,最高的人处于队伍的最右边。(最大的项 ‘冒泡’ 到数组的顶端),
数组最顶端的数据确定。无需再排。
第二趟排序 找出 第二高的,放在n-2的位置。
关键代码<理解>:
public void bubbleSort() {
int out, in;
for(out=nElems-1; out>1; out--) // outer loop (backward <---)
for(in=0; in<out; in++) // inner loop (forward --->)
if( a[in] > a[in+1] ) // out of order?
swap(in, in+1); // swap them
} // end bubbleSort()
效率O(n^2):
N个数据项,第一趟排序n-1此比较,第二趟n-2 以此类推....
(n-1)+(n-2)+...+1 = ( n*(n-1) )/2 约做了 n^2/2次比较
****选择排序
选择排序改进了冒泡排序,将必要的交换次数从O(N^2)减少到O(N)但是比较次数仍未O(N^2)
规则:
先把所有队员扫描一趟,从中挑出(选择)最矮的队员,将其和最左边的队员互换位置,
这样最矮的人再第一个位置 确定下来,再从其余的队员中(选择出)最矮的放在第二个位置。以此类推。
更细节描述:
排序从最左边开始,记录下最左边球员身高,紫毛巾放在其前面。
用下一个球员的身高与第一个相比,如果这个更矮则第一个的身高,记下此人身高,
将紫毛巾放在其前面。否则不变。继续演这个队列走下。
走完第一趟,最矮的一个人就确定了,然后和处在0位置的问进行交换。
在下一趟排序中所做的事是一模一样的,只是忽略掉最左边的一个队员。
关键代码:
public void selectionSort(){
int out, in, min;
for(out=0; out<nElems-1; out++) // outer loop -->
{
min = out; // minimum min角标
for(in=out+1; in<nElems; in++) // inner loop -->
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()
效率O(n^2):
但是选择排序 比冒泡排序快 。
将必要的交换次数从O(N^2)减少到O(N)但是比较次数仍未O(N^2)
****插入排序
虽然插入排序算法仍然需要O(N^2)时间,但一般情况下,比冒泡排序快一倍。比选择排序还快一点。
1.标记队员左边的局部有序,标记队员和右边的都没有排过序。
2.在局部有序中找到合适和位置插入被标记的队员。(需要部分已排序队员 右移 腾出位置存放被标记的队员)
为提供移动所需空间,就先让被标记的队员出列(程序中,这个数据项被存在一个临时变量中)
3.排完之后,被标记队员 和左边的 有序队列 都拍好了。
4.将上次标记队员的 下一个队员 作为 标记队员。开始新的循环。以此类推。
关键代码:
public void insertionSort()
{
int in, out;
for(out=1; out<nElems; out++) // out is dividing line
{
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()
效率:
O(N^2)
****对象排序
前面的排序算法使用基本数据类型,但排序却更多用于对象排序。而不是对基本数据类型排序。
---------------------------------------------------------------------------------------------------------------------------------------------
《1》折半查找--code
// orderedArray.java
// demonstrates ordered array class
// to run this program: C>java OrderedApp
class OrdArray
{
private long[] a; // ref to array a
private int nElems; // number of data items
//-----------------------------------------------------------
public OrdArray(int max) // constructor
{
a = new long[max]; // create array
nElems = 0;
}
//-----------------------------------------------------------
public int size()
{ return nElems; }
//-----------------------------------------------------------
public int find(long searchKey)
{
int lowerBound = 0;
int upperBound = nElems-1;
int curIn;
while(true)
{
curIn = (lowerBound + upperBound ) / 2;
if(a[curIn]==searchKey)
return curIn; // found it
else if(lowerBound > upperBound)
return nElems; // can't find it
else // divide range
{
if(a[curIn] < searchKey)
lowerBound = curIn + 1; // it's in upper half
else
upperBound = curIn - 1; // it's in lower half
} // end else divide range
} // end while
} // end find()
//-----------------------------------------------------------
public void insert(long value) // put element into array
{
int j;
for(j=0; j<nElems; j++) // find where it goes
if(a[j] > value) // (linear search)
break;
for(int k=nElems; k>j; k--) // move bigger ones up
a[k] = a[k-1];
a[j] = value; // insert it
nElems++; // increment size
} // end insert()
//-----------------------------------------------------------
public boolean delete(long value)
{
int j = find(value);
if(j==nElems) // can't find it
return false;
else // found it
{
for(int k=j; k<nElems; k++) // move bigger ones down
a[k] = a[k+1];
nElems--; // decrement size
return true;
}
} // end delete()
//-----------------------------------------------------------
public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//-----------------------------------------------------------
} // end class OrdArray
class OrderedApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
OrdArray arr; // reference to array
arr = new OrdArray(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);
int searchKey = 55; // search for item
if( arr.find(searchKey) != arr.size() )
System.out.println("Found " + searchKey);
else
System.out.println("Can't find " + searchKey);
arr.display(); // display items
arr.delete(00); // delete 3 items
arr.delete(55);
arr.delete(99);
arr.display(); // display items again
} // end main()
} // end class OrderedApp
###################################################################################################
《2》冒泡排序--code
// 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<nElems; j++) // for each element,
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<out; in++) // inner loop (forward)
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
###################################################################################################
《3》选择排序--code
// 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<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//--------------------------------------------------------------
public void selectionSort()
{
int out, in, min;
for(out=0; out<nElems-1; out++) // outer loop
{
min = out; // minimum
for(in=out+1; in<nElems; in++) // inner loop
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
###################################################################################################
《4》插入排序--code
// 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<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//--------------------------------------------------------------
public void insertionSort()
{
int in, out;
for(out=1; out<nElems; out++) // out is dividing line
{
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(); // display them again
} // end main()
} // end class InsertSortApp
###################################################################################################
《5》对象排序--code
// objectSort.java
// demonstrates sorting objects (uses insertion sort)
// to run this program: C>java ObjectSortApp
class Person
{
private String lastName;
private String firstName;
private int age;
//-----------------------------------------------------------
public Person(String last, String first, int a)
{ // constructor
lastName = last;
firstName = first;
age = a;
}
//-----------------------------------------------------------
public void displayPerson()
{
System.out.print(" Last name: " + lastName);
System.out.print(", First name: " + firstName);
System.out.println(", Age: " + age);
}
//-----------------------------------------------------------
public String getLast() // get last name
{ return lastName; }
} // end class Person
class ArrayInOb
{
private Person[] a; // ref to array a
private int nElems; // number of data items
//--------------------------------------------------------------
public ArrayInOb(int max) // constructor
{
a = new Person[max]; // create the array
nElems = 0; // no items yet
}
//--------------------------------------------------------------
// put person into array
public void insert(String last, String first, int age)
{
a[nElems] = new Person(last, first, age);
nElems++; // increment size
}
//--------------------------------------------------------------
public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
a[j].displayPerson(); // display it
}
//--------------------------------------------------------------
public void insertionSort()
{
int in, out;
for(out=1; out<nElems; out++)
{
Person temp = a[out]; // out is dividing line
in = out; // start shifting at out
while(in>0 && a[in-1].getLast().compareTo(temp.getLast())>0)
{
a[in] = a[in-1]; // shift item to the right
--in; // go left one position
}
a[in] = temp; // insert marked item
} // end for
} // end insertionSort()
//--------------------------------------------------------------
} // end class ArrayInOb
class ObjectSortApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
ArrayInOb arr; // reference to array
arr = new ArrayInOb(maxSize); // create the array
arr.insert("Evans", "Patty", 24);
arr.insert("Smith", "Doc", 59);
arr.insert("Smith", "Lorraine", 37);
arr.insert("Smith", "Paul", 37);
arr.insert("Yee", "Tom", 43);
arr.insert("Hashimoto", "Sato", 21);
arr.insert("Stimson", "Henry", 29);
arr.insert("Velasquez", "Jose", 72);
arr.insert("Vang", "Minh", 22);
arr.insert("Creswell", "Lucinda", 18);
System.out.println("Before sorting:");
arr.display(); // display items
arr.insertionSort(); // insertion-sort them
System.out.println("After sorting:");
arr.display(); // display them again
} // end main()
} // end class ObjectSortApp
###################################################################################################
《java数据结构与算法》一书第三章读书笔记。