《数据结构与算法(java语言版)第2版 Adam Drozdek著》排序的代码

package sort_function;


public class TestSort {


public static void main(String[] args) {
// TODO Auto-generated method stub


}//main

//插入排序
public void insertionSort(Object []data){
for(int i = 1,j; i < data.length; i++){
Comparable tmp = (Comparable)data[i];
for (j = i; j > 0 && tmp.compareTo(data[j-1])  < 0; j--){
data[j] = data[j-1];
}//for
data[j] = tmp;
}//for
}//insertionSort

//选择排序
public void selectionSort(Object [] data){
int i,j,least;//least是最小元素的所在位置
for(i = 0 ; i < data.length - 1; i++){
for(j = i + 1,least = i; j < data.length;j++){
if(((Comparable)data[j]).compareTo(data[least])<0){ //Comparable类
least = j;
}//if
if(i!=least){
swap(data,least,i);
}//if
}//for
}//for
}//selectionSort

//交换数据
public void swap(Object []a,int e1,int e2){
Object tmp = a[e1];
a[e1] = a[e2];
a[e2] = tmp;
}//swap

//冒泡排序
public void bubbleSort(Object []data){
for(int i = 0; i < data.length - 1 ; i ++){
for(int j = data.length - 1; j > i; --j){
if(((Comparable)data[j]).compareTo(data[j-1]) < 0 ){
swap(data, j, j - 1);
}//if
}//for
}//for
}//bubbleSort

//Shell排序
public void shellSort(Object []data){
int i,j ,k,h,hCnt,increments[] = new int[20];
//create an appropriate number of increments h 
for(h = 1,i = 0 ;h < data.length ;i++ ){
increments[i] = h;
h = 3 * h + 1;
}//for
//loop on the number of different increments h
for(i--;i>=0;i--){
h = increments[i];
//loopon the number of subarrays h-sorted in ith pass
for(hCnt = h ;hCnt < 2*h; hCnt++){
//insertion sort for subarray containing every hth element of array data
for(j = hCnt; j < data.length ;){
Comparable tmp = (Comparable)data[j];
k = j ;
while(k-h >= 0&& tmp.compareTo(data[k-h] )< 0){
data[k] = data[k-h];
}//while
data[k] = tmp;
j+= h;
}//for 3
}//for 2
}//for 1

}//shellSort

//
void heapSort(Object []data){
for(int i = data.length / 2 -1;i >= 0; --i){
moveDown(data,i,data.length -1);
}//for
for(int i = data.length - 1;i>=1;--i){
swap(data,0,i);
moveDown(data,0,i -1);
}//for
}//heapSort

//根元素沿着树向下移动的算法
public void moveDown(Object [] data,int first,int last){
int largest = 2* first + 1;
while(largest <= last){
if(largest <last &&  //first has two children (at 2 * first+1 and 2*first +2)
((Comparable)data[largest]).compareTo(data[largest+1]) < 0 ){
largest++;
}//if
if(((Comparable)data[first]).compareTo( data[largest]) < 0 ){
swap(data,first,largest); //if necessary,swap values
first = largest; //and move down
largest = first * 2 + 1;
}//if
else largest = last +1;//to exit the loop:the heap property isn't violated by data[first]
}//while
}//moveDown

//快速排序
public void quickSort(Object []data,int first,int last){
int lower = first + 1,upper = last;
swap(data,first,(first + last) / 2);
Comparable bound = (Comparable)data[first];
while(lower <= upper){
while(bound.compareTo( data[upper]) <= 0){
lower++;
}//while
while(bound.compareTo( data[upper]) > 0){
upper--;
}//while
if(lower < upper){
swap(data,lower++,upper--);
}//if
else lower++;
}//while
swap(data,upper,first);
if(first<upper-1){
quickSort(data,first,upper-1);
}//if
if(upper+1<last){
quickSort(data,upper+1,last);
}//if
}//quickSort(Object []data,int first,int last)

public void quickSort(Object []data){
if(data.length < 2){
return;
}//if
int max = 0; 
//find the largest element and put it at the end of data;
for(int i = 1;i<data.length ;i++){
if(((Comparable)data[max]).compareTo(data[i]) < 0 )
max = i;
swap(data,data.length -1,max);//largest el is now in its
quickSort(data,0,data.length  - 2); //final position
}//for
}//quickSort(Object []data)

//对整型数进行基数排序,将整数看作是数字的连接
public void radixSort(int [] data){
int i = 0,d,j,k,factor;
int radix = 10;
int digits = 10;
Queue [] queues = new Queue[radix];
for(d = 0;d < radix ;d++){
queues[d] = new Queue();
}//for
for (d = 1, factor = 1; d <= digits; factor *= radix,i++ ){
for(j = 0; j < data.length; j++){
queues[(data[j] / factor) % radix].enqueue(new Integer(data[j]));
}//for
for(j = k = 0; j < radix; j++){
while(!queues[j].isEmpty()){
data[k++] = ((Integer) queues[j].dequeue()).intValue();
}//while
}//for
}//for1

}//radixSort

//对整型数进行基数排序,将整数看作是位的连接
public void bitRadixSort(int []data){
int d,j,k,factor,mask = 1;
Queue []queues = new Queue[2];
queues[0] = new Queue();
queues[1] = new Queue();
int bits =  32;      //bitCount(data);
for(d = 1; d <= bits; d++){
for(j = 0; j < data.length ; j++){
queues[(data[j] & mask) == 0 ? 0 : 1].enqueue(new Integer(data[j]));
}//for
mask <<= 1;
k = 0;
while(!queues[0].isEmpty())
data[k++] = ((Integer)queues[0].dequeue()).intValue();
while(!queues[1].isEmpty())
data[k++] = ((Integer)queues[1].dequeue()).intValue();
k = 0;
}
}


}//TestSort



//队列类

public class Queue {
private java.util.LinkedList list = new java.util.LinkedList();
public Queue(){

}

public void clear(){
list.clear();
}

public boolean isEmpty(){
return list.isEmpty();
}

public Object firstEl(){
return list.getFirst();
}

public Object dequeue(){
return list.removeFirst();
}

public void enqueue(Object el){
list.addLast(el);;
}

public String toString(){
return list.toString();
}
}




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值