Algorithm.Sort (各类排序)

(排序的递归分为 分治法和归纳法)

    冒泡排序及其递归实现

    堆排序

    插入排序及其递归实现

    合并排序及其递归实现

    快速排序

    选择排序及其递归实现

    希尔排序

package Algorithm.Sort;

import java.util.Vector;
/*Author: CPlusPlus小码农
 *If any question, 
 *Please contact:           
 * http://daixiecplusplus.blog.163.com/
 * QQ:1926742804
 */
public class BubbleSwapSort<E extends Comparable<E> > {
	public void Sort(Vector<E> v)
	{
		for(int i = 0 ; i < v.size() ; ++i)
		{
			for(int j = i+1 ; j < v.size() ; ++j)
			{
				E first = v.get(i);
				E second = v.get(j);
				if(first.compareTo(second) > 0)
				{
					v.set(i, second);
					v.set(j, first);
				}
			}
		}
	}
	
	public void SortByExcursion(Vector<E> v,int low)
	{
		if(low == v.size()) return;
		for(int j = low+1;j < v.size() ;++j)
		{
			E first = v.get(low);
			E second = v.get(j);
			if(first.compareTo(second) > 0)
			{
				v.set(low, second);
				v.set(j, first);
			}
		}
		SortByExcursion(v,low+1);
	}
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Vector<Integer> v = new Vector<Integer>();
		v.add(10);
		v.add(20);
		v.add(30);
		v.add(15);
		v.add(50);
		v.add(25);
		v.add(40);
		v.add(80);
		v.add(100);
		v.add(1);
		v.add(2);
		
		BubbleSwapSort<Integer> s = new BubbleSwapSort<Integer>();
		//s.Sort(v);
		s.SortByExcursion(v, 0);
		System.out.println(v);
	}

}

package Algorithm.Sort;
import java.util.Vector;
import Structure.Heap.MaxHeap;
/*Author: CPlusPlus小码农
 *If any question, 
 *Please contact:           
 * http://daixiecplusplus.blog.163.com/
 * QQ:1926742804
 */
public class HeapSort {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Vector<Integer> v = new Vector<Integer>();
		v.add(30);
		v.add(60);
		v.add(8);
		v.add(40);
		v.add(70);
		v.add(12);
		v.add(10);
		
		MaxHeap<Integer> mh = new MaxHeap<Integer>(10);
		mh.BuildHeap(v);
		mh.Print();
		System.out.println();
		
		mh.Sort();
		mh.Print();

	}

}

package Algorithm.Sort;
import java.util.Vector;
/*Author: CPlusPlus小码农
 *If any question, 
 *Please contact:           
 * http://daixiecplusplus.blog.163.com/
 * QQ:1926742804
 */
public class InsertionSort<E extends Comparable<E> > {
	private void Swap(Vector<E> v,int i,int j)
	{
		E temp = v.get(i);
		v.set(i, v.get(j));
		v.set(j, temp);
	}
	
	public void Sort(Vector<E> v)
	{
		for(int i = 1; i < v.size() ; ++i)
		{
			E cur = v.get(i);
			int j = i-1;
			while(j>=0 && v.get(j).compareTo(cur) > 0)
			{
				Swap(v,j,j+1);
				j--;
			}
			v.set(j+1, cur);
		}
	}

	public void SortByExcursion(Vector<E> v,int low)
	{
		if(low > 1)
		{
			E cur = v.get(low);
			SortByExcursion(v,low-1);
			int j = low - 1;
			while(j >= 0 && v.get(j).compareTo(cur) > 0)
			{
				Swap(v,j,j+1);
				j--;
			}
			v.set(j+1, cur);
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Vector<Integer> v = new Vector<Integer>();
		v.add(10);
		v.add(20);
		v.add(30);
		v.add(15);
		v.add(50);
		v.add(25);
		v.add(40);
		v.add(80);
		v.add(100);
		v.add(1);
		v.add(2);
		
		InsertionSort<Integer> i = new InsertionSort<Integer>();
		//i.Sort(v);
		
		i.SortByExcursion(v, v.size()-1);
		System.out.println(v);

	}

}

package Algorithm.Sort;
import java.util.Vector;
/*Author: CPlusPlus小码农
 *If any question, 
 *Please contact:           
 * http://daixiecplusplus.blog.163.com/
 * QQ:1926742804
 */
public class MergeSort<E extends Comparable<E> > {
	
	public void Copy(Vector<E> src,Vector<E> dst)
	{
		for(int i = 0 ; i < src.size() ; ++i)
		{
			dst.insertElementAt(src.get(i), i);
		}
	}
	public void Sort(Vector<E> v)
	{
		int size = 1;
		int times = v.size()%2 + v.size()/2;
		Vector<E> tempVector = new Vector<E>(v.size());
		for(int i = 0 ; i < times ; ++i)
		{
			tempVector.clear();
			Copy(v, tempVector);
			int lLow;
			int lHigh;
			int rLow;
			int rHigh;
			int currentRound = v.size()/(2*size)+v.size()%(2);
			for(int j = 0;j< currentRound;++j)
			{
				lLow = 2*size*j;
				lHigh = lLow+size-1;
				rLow = lHigh+1;
				if(j != currentRound -1)
				{
					rHigh = rLow+size-1;
				}else rHigh = v.size()-1;
				Sort(v,tempVector,lLow,lHigh,rLow,rHigh);
			}
			v.clear();
			Copy(tempVector,v );
			size *= 2;
		}
	}
	
	public void Sort(Vector<E> v,int low,int high)
	{
		if(low < high)
		{
			int mid = (low+high)/2;
			Sort(v,low,mid);
			Sort(v,mid+1,high);
			
			Vector<E> dst = new Vector<E>(v);
			Sort(v,dst,low,mid,mid+1,high);
			for(int i = low;i <= high; ++i)
			{
				v.set(i, dst.get(i));
			}
		}
	}
	
	private void Sort(Vector<E> v,Vector<E> dst,int leftLow,int leftHigh,int rightLow,int rightHigh)
	{
		int i = leftLow;
		int j = rightLow;
		int p = leftLow;
		while(i <= leftHigh && j <= rightHigh)
		{
			E left = v.get(i);
			E right = v.get(j);
			if(left.compareTo(right) > 0)
			{
				dst.set(p, right);
				j++;
			}
			else{
				dst.set(p, left);
				i++;
			}
			++p;
		}
		if(j == 1+ rightHigh) 
		{
			while(i <= leftHigh)
			{
				dst.set(p++, v.get(i));
				++i;
			}
		}else if(i == 1+ leftHigh)
		{
			while(j <= rightHigh)
			{
				dst.set(p++, v.get(j));
				++j;
			}
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Vector<Integer> v = new Vector<Integer>();
		v.add(10);
		v.add(20);
		v.add(30);
		v.add(15);
		v.add(50);
		v.add(25);
		v.add(40);
		v.add(80);
		v.add(100);
		v.add(1);
		v.add(2);
		
		MergeSort<Integer> ms = new MergeSort<Integer>();
		//ms.Sort(v);
		
		ms.Sort(v, 0, v.size()-1);
		
		System.out.println(v);
	}

}

package Algorithm.Sort;
import java.util.Vector;
/*Author: CPlusPlus小码农
 *If any question, 
 *Please contact:           
 * http://daixiecplusplus.blog.163.com/
 * QQ:1926742804
 */
public class QuickSort<E extends Comparable<E> > {
	private void Swap(Vector<E> v,int i,int j)
	{
		E temp = v.get(i);
		v.set(i, v.get(j));
		v.set(j, temp);
	}
	
	public void Pick(Vector<E> v,int low,int high,int m)
	{
		if(low >= high) return;
		int mid = (low+high)/2;
		
		if(v.get(low).compareTo(v.get(mid))  >0) Swap(v,low,mid);
		if(v.get(low).compareTo(v.get(high)) >0) Swap(v,low,high);
		if(v.get(mid).compareTo(v.get(high)) >0) Swap(v,mid,high);
		Swap(v,mid,high-1);
		
		if(low == high -1) return;
		
		int k =low;
		int r = high-1;
		
		E compare = v.get(high-1);
		while(true)
		{
			while(v.get(++k).compareTo(compare)<0);
			while(v.get(--r).compareTo(compare)>0);
			if(k < r) Swap(v,k,r);
			else break;
		}
		Swap(v,k,high-1);
		
		if(m<k) Pick(v,low,k-1,m);
		else Pick(v,k+1,high,m-k);
	}
	
	public void Sort(Vector<E> v,int low,int high)
	{
		if(low >= high) return;
		int mid = (low+high)/2;
		
		if(v.get(low).compareTo(v.get(mid))  >0) Swap(v,low,mid);
		if(v.get(low).compareTo(v.get(high)) >0) Swap(v,low,high);
		if(v.get(mid).compareTo(v.get(high)) >0) Swap(v,mid,high);
		Swap(v,mid,high-1);
		
		if(low == high -1) return;
		
		int k =low;
		int r = high-1;
		
		E compare = v.get(high-1);
		while(true)
		{
			while(v.get(++k).compareTo(compare)<0);
			while(v.get(--r).compareTo(compare)>0);
			if(k < r) Swap(v,k,r);
			else break;
		}
		Swap(v,k,high-1);
		
		Sort(v,low,k-1);
		Sort(v,k+1,high);
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Vector<Integer> v = new Vector<Integer>();
		v.add(10);
		v.add(20);
		v.add(30);
		v.add(15);
		v.add(50);
		v.add(25);
		v.add(40);
		v.add(80);
		v.add(100);
		v.add(1);
		v.add(2);
		
		QuickSort<Integer> q = new QuickSort<Integer>();
		//q.Sort(v,0,v.size()-1);
		//System.out.println(v);
		
		q.Pick(v, 0, v.size()-1, 7);
		System.out.println(v.get(7));
	}

}

package Algorithm.Sort;
import java.util.Vector;
/*Author: CPlusPlus小码农
 *If any question, 
 *Please contact:           
 * http://daixiecplusplus.blog.163.com/
 * QQ:1926742804
 */
public class SelectionSort<E extends Comparable<E> > {
	public void Sort(Vector<E> v)
	{
		for(int i = 0 ; i < v.size() ; ++i)
		{
			int current = i;
			for(int j = i+1 ; j < v.size() ; ++j)
			{
				E cur = v.get(current);
				E now = v.get(j);
				if(now.compareTo(cur) < 0) current = j;
			}
			if(current != i)
			{
				E temp = v.get(current);
				v.set(current, v.get(i));
				v.set(i, temp);
			}
		}
	}
	
	public void SortByExcursion(Vector<E> v,int low)
	{
		if(low == v.size()) return;
		int current = low;
		for(int j = low + 1; j < v.size() ; ++j)
		{
			E cur = v.get(current);
			E now = v.get(j);
			if(now.compareTo(cur) < 0) current = j;
		}
		if(current != low)
		{
			E temp = v.get(current);
			v.set(current, v.get(low));
			v.set(low, temp);
		}
		SortByExcursion(v,low+1);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Vector<Integer> v = new Vector<Integer>();
		v.add(10);
		v.add(20);
		v.add(30);
		v.add(15);
		v.add(50);
		v.add(25);
		v.add(40);
		v.add(80);
		v.add(100);
		v.add(1);
		v.add(2);
		
		SelectionSort<Integer> s = new SelectionSort<Integer>();
		s.SortByExcursion(v, 0);
		//s.Sort(v);
		
		System.out.println(v);
	}

}

package Algorithm.Sort;
import java.util.Vector;
/*Author: CPlusPlus小码农
 *If any question, 
 *Please contact:           
 * http://daixiecplusplus.blog.163.com/
 * QQ:1926742804
 */
public class ShellSort<E extends Comparable<E> > {
	private void Swap(Vector<E> v,int i,int j)
	{
		E temp = v.get(i);
		v.set(i, v.get(j));
		v.set(j, temp);
	}
	
	private void ShellStep(Vector<E> v,int step,int start)
	{
		for(int i = start+step; i < v.size(); i = i + step)
		{
			E temp = v.get(i);
			int j = i - step;
			while(j >= start && v.get(j).compareTo(temp) > 0)
			{
				Swap(v,j,j+step);
				j = j - step;
			}
			v.set(j+step, temp);
		}
	}
	
	public void Sort(Vector<E> v)
	{
		int size = 1;
		for(; size < v.size() ; )
		{
			size = 3*size+1;
		}
		size = (size - 1)/3;
		while(size >= 1)
		{
			for(int i = 0 ; i < size ;++i )
			{
				ShellStep(v,size,i);
				size = (size - 1)/3;
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Vector<Integer> v = new Vector<Integer>();
		v.add(10);
		v.add(20);
		v.add(30);
		v.add(15);
		v.add(50);
		v.add(25);
		v.add(40);
		v.add(80);
		v.add(100);
		v.add(1);
		v.add(2);
		
		ShellSort<Integer> s = new ShellSort<Integer>();
		s.Sort(v);
		System.out.println(v);

	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值