关闭

对五种排序的实现【java】

标签: 冒泡排序堆排序快速排序java
23人阅读 评论(0) 收藏 举报
分类:

冒泡排序:

package sort;

//冒泡
public class BubbleSort {
	private int[] a;
	public BubbleSort(int[] a) {
		// TODO Auto-generated constructor stub
		this.a = a;
	}
	
	//把小的冒泡到前面,即从小到大
	public void sort() {
		for (int i = 0;i < a.length;i++) {
			for (int j = i+1;j < a.length;j++) {
				if (a[i]>a[j]) {
					int t = a[i];
					a[i] = a[j];
					a[j] = t;
				}
			}
		}
	}
	
	public static void main(String[] args) {
		int a[] = {5,6,4,3,8,6,4};
		BubbleSort sort = new BubbleSort(a);
		sort.sort();
		for (int i:sort.a)
		System.out.println(i);
	}
	
}
插入排序:

package sort;

public class InsertSort {
	
	private int[] a;
	
	public InsertSort(int[] a) {
		// TODO Auto-generated constructor stub
		this.a = a;
	}
	
	//从小到大
	public void sort() {
		for (int i = 0;i < a.length;i++) {
			for (int j = 0;j < i;j++) {
				if (a[i]<a[j]) {
					int t = a[i];
					for (int k = i-1;k >= j;k--) {
						a[k+1] = a[k];
					}
					a[j] = t;
				}
			}
		}
	}
	
	public static void main(String[] args) {
		int a[] = {5,6,4,3,8,6,4};
		InsertSort sort = new InsertSort(a);
		sort.sort();
		for (int i:sort.a)
		System.out.println(i);
	}
}

归并排序:

package sort;


//归并
public class MergeSort {
	private int a[];
	private int length;
	
	public MergeSort(int a[]) {
		// TODO Auto-generated constructor stub
		this.a = a;
		this.length = a.length;
	}
	
	public void sort() {
		MergeSort(0,length-1);
	}
	
	public void MergeSort(int m,int n) {
		int mid = (m + n) / 2;
		if (m < n) {
			MergeSort(m, mid);
			MergeSort(mid + 1, n);
			Merge(m,mid,n);
		}
	}
	
	public void Merge(int m, int mid, int n) {
		System.out.println("Merge("+m+","+mid+","+n+")");
		int p1 = m;
		int p2 = mid + 1;
		int temp[] = new int[n-m+1];
		int p = 0;
		if (m<n) {
			while(p <= n - m) {
				if (p1<=mid&&p2<=n) {
					if (a[p1]<a[p2]) {
						temp[p++] = a[p1++];
					}else {
						temp[p++] = a[p2++];
					}
				}
				else {
					if (p1>mid&&p2<=n) {
						temp[p++] = a[p2++];
					}
					if (p2>n&&p1<=mid) {
						temp[p++] = a[p1++];
					}
				}
			}
			for (int i=0;i<temp.length;i++) {
				a[m + i] = temp[i];
			}
		}
	}
	public static void main(String[] args) {
		int a[] = {5,6,4,3,8,6,4};
		MergeSort sort = new MergeSort(a);
		sort.sort();
		for (int i:sort.a)
		System.out.print(i+",");
	}
}


堆排序:

package sort;


public class HeapSort {
	private int a[];
	private int length;
	public HeapSort(int a[]) {
		// TODO Auto-generated constructor stub
		this.a = a;
		this.length = a.length;
	}
	
	public void MaxHeap(int i) {
		//int parent = (i+1)/2-1;
		int left = 2*(i+1)-1;
		int right = 2*(i+1);
		if (left < length&&a[left] > a[right]&&a[left] > a[i]) {
			int t = a[i];
			a[i] = a[left];
			a[left] = t;
			MaxHeap(left);
		}
		else if (right < length&&a[left] < a[right]&&a[right] > a[i]) {
			int t = a[i];
			a[i] = a[right];
			a[right] = t;
			MaxHeap(right);
		}
	}
	
	public void MakeMaxHeap() {
		for (int i = length/2; i >= 0; i--) {
			MaxHeap(i);
		}
	}
	
	public void toLastPos(int i) {
		int t = a[0];
		a[0] = a[i];
		a[i] = t;
		length--;
	}
	
	public void sort() {
		
		if (a == null || length <= 1) {  
            return;  
        }  
		
		MakeMaxHeap();
		for (int i:a)
		System.out.println("in:"+i);
		//最大的被轮流挤进数组后面,最后变成由大到小排序好的数组
		for(int i = length-1; i > 1; i--) {
			toLastPos(i);
			MaxHeap(0);
		}
		//当剩下两个节点时,这时如果把最大的数挤入数组中会导致还有一个节点没有被排序
		//跳出循环后,剩下的两个节点执行一次MaxHeap(0)即完成了两个节点的排序
		MaxHeap(0);
	}
	
	public static void main(String[] args) {
		int a[] = {-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
		HeapSort sort = new HeapSort(a);
		sort.sort();
		for (int i:sort.a)
		System.out.println(i);
	}
}


快速排序:

package sort;

public class QuickSort {
	private int[] a;
	private int length;
	public final static int Left = 0;
	public final static int Right = 1;
	public QuickSort(int a[]) {
		// TODO Auto-generated constructor stub
		this.a = a;
		this.length = a.length;
	}
	
	public void QuickSort(int a[], int m, int n) {
		if (n<=m) {
			return;
		}
		int standard = choose(a, m, n);
		int left = m;//左指针
		int right = n;//右指针
		int LeftOrRight = Left;//默认指针左移
		while(left < right) {
			//指针左移
			if(LeftOrRight==Left) {
				if (a[right] > standard) {
					right--;
				}
				else if (a[right] <= standard&&a[left] >= standard) {
					int t = a[right];
					a[right] = a[left];
					a[left] = t;
					LeftOrRight = Right;
					//这里是为了处理两边都等于基准值的情况,先移动一个指针后让下面的逻辑去处理,以免进入死循环
					if (a[left] == standard&&a[right] == standard) {
						right--;
					}
				}
				else if (a[right] <= standard&&a[left] <= standard) {
					LeftOrRight = Right;//左右都小于基准值时,改变指针移动方向
				}
			}
			//指针右移
			else {
				if (a[left] < standard) {
					left++;
				}
				else if (a[left] >= standard&&a[right] >= standard) {
					LeftOrRight = Left;//左右都大于基准值时,改变指针移动方向
					//这里是为了处理两边都等于基准值的情况,先移动一个指针后让下面的逻辑去处理,以免进入死循环
					if (a[left] == standard&&a[right] == standard) {
						left++;
					}
				}
				else if (a[left] >= standard&&a[right] <= standard) {
					int t = a[right];
					a[right] = a[left];
					a[left] = t;
					LeftOrRight = Left;
				}
			}
		}
		//这里我增加了一段代码,由于在基准值的选择中会出现重复值,处理时找到基准值的最左边界和最右边界
		//理解拙劣,只能采用这种方法。。
		int times=0;
		for (int i = 0; i <a.length;i++) {
			if (a[i]==standard&×==0) {
				left = i;
				times++;
			}
			else if (a[i]==standard&×==0) {
				right = i;
			}
		}
		//System.out.println("m,n="+m+","+n+":standard="+standard);
		QuickSort(a,m,left-1);
		QuickSort(a,right+1,n);
	}
	
	public void sort() {
		QuickSort(a,0,length-1);
	}
	
	public int choose(int[] a, int m, int n){
		int standard = a[m];//选取第一个元素作为基准值
		return standard;
	}
	
	public static void main(String[] args) {
		int a[] = {5,6,4,3,8,6,4,5,234,2,35,23,5};
		QuickSort sort = new QuickSort(a);
		sort.sort();
		for (int i:sort.a)
		System.out.print(i+",");
	}
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:598次
    • 积分:96
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:3篇
    • 译文:0篇
    • 评论:0条
    文章分类