当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

Sort

发表于: 2015-06-08   作者:残云cruel   来源:转载   浏览:
摘要: BubbleSort.Java package sort; public class BubbleSort { public static void main(String[] args) { int[] number={6,7,8,5,4,1,2,3}; // bubbleSort(number); int low=0; int high=numb
BubbleSort.Java

package sort;

public class BubbleSort {

    public static void main(String[] args) {
		int[] number={6,7,8,5,4,1,2,3};
//		bubbleSort(number);
		int low=0;
		int high=number.length-1;
	    quickSort(number,low,high);
		//print 
		for(int i=0;i<number.length;i++)
			System.out.print(number[i]+" ");				
	}
    //bubbleSort

	public static  void bubbleSort(int[] number) {	
		int tempt;
		boolean continueSort=true;
		while(continueSort){		
			continueSort=false;
			for(int i=0;i<number.length-1;i++){
				if(number[i]>number[i+1]){
					tempt=number[i+1];
					number[i+1]=number[i];
					number[i]=tempt;
					continueSort=true;				
				}			
			}		
		}
	}
	//recursion quickSort
	private static void quickSort(int[] number,int low,int high) {
	    if(low<high){//to identify whether this part has more than 1 numbers
	    	int pivotloc=Partition(number,low,high);
			quickSort(number,low,pivotloc-1);
			quickSort(number,pivotloc+1,high);	    	
	    }	
	}
	//Partition算法10.6(b)
	static int Partition(int[] number,int low,int high){
		int tempt=number[low];
		int pivotkey=number[low];
		while(low<high){
			while(low<high && number[high]>=pivotkey)
				high--;
			number[low]=number[high];
			while(low<high && number[low]<=pivotkey)
				low++;
			number[high]=number[low];		
		}
		number[low]=tempt;
		return low;	
	}	
}

 

Sort

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

推荐文章
1 sort
3 Sort
4 sort
5 Sort
6 sort
7 Sort
8 sort
9 sort
10 sort
11 sort
12 sort
13 Sort
14 Sort
15 Sort
16 sort
17 sort
18 sort()
19 sort
20 sort
编辑推荐
1 SORT
转自:http://yiyickf.iteye.com/blog/1047010 排序算法java版,速度排行:冒泡排序、简单选择排序
简介 counting sort和radix sort和原来的那些通过比较交换来排序的方法不一样。原来的常用排序算法
首先说明该算法也是来自于维基百科 http://en.wikipedia.org/wiki/Gnome_sort Gnome Sort,这是该算
1. Merge Sort and Quick Sort are two classic sorting algorithms and Critical components in th
dic = {'a':31, 'bc':5, 'c':3, 'asd':4, '33':56, 'd':0} 想把dic的value按照从大到小排序(value
为了完成二维数据快速分类,最先使用的是hash分类。 前几天我突然想,既然基数排序的时间复杂度也不
看到园子里有人发排序算法的随笔,想起之前发现的一个好网页,日本人写的,用脚本编写的一个算法柱
Sort List 问题: Sort a linked list in O(n log n) time using constant space complexity. 思路
1. Basic plan: a) Shuffle the array. b) Partition so that, for some j – entry a[j] is in pla
1. Quicksort is honored as one of top 10 algorithms of 20th century in science and engineerin
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号