排序算法Java实现

import java.util.Random;

public class Sort {
	//Bubble sort  时间复杂度为O(n^2)
	public void bubbleSort(int a[])
	{
		int temp = 0;
		int count = 0;
		for(int i=0;i<a.length;++i)
		{
			count = 0;
			for(int j=0;j<a.length-1;++j)
			{
				if(a[j]>a[j+1])
				{
					temp = a[j];
					a[j] = a[j+1];
					a[j+1] = temp;
					++count;
				}
			}
			System.out.println("i="+i+",count="+count);
			if(count==0)
				break;
		}
	}
	
	//Select Sort  时间复杂度为O(n^2)
	public void selectSort(int a[])
	{
		int min_index= 0;
		int temp = 0;
		
		for(int i=0;i<a.length;++i)
		{
			min_index = i; 
			for(int j=i+1;j<a.length;++j)
			{
				if(a[j]<a[min_index])
					min_index = j;
			}
			if(min_index!=i)
			{
				temp = a[min_index];
				a[min_index] = a[i];
				a[i] = temp;
			}
		}
	}
	
	//insert sort   时间复杂度为O(n^2)
	public void insertSort(int a[])
	{
		int temp = 0;
		int j = 0;
		for(int i=1;i<a.length;++i)
		{
			if(a[i]<a[i-1])
			{
				temp = a[i];
				j = i-1;
				do{
					a[j+1] = a[j];
					--j;
				}while(j>=0 && temp<a[j]);
				a[j+1] = temp;
			}
		}
	}
	
	//Shell Sort  
	public void shellSort(int a[])
	{
		int gap = a.length;
		int j = 0;
		int temp = 0;
		do
		{
			gap = gap/3+1;
			for(int i=gap;i<a.length;++i)
			{
				if(a[i]<a[i-gap])
				{
					temp = a[i];
					j = i-gap;
					do
					{
						a[j+gap] = a[j];
						j -= gap;
					}while(j>=0 && temp<a[j]);
					a[j+gap] = temp;
				}
			}
		}while(gap>1);
	}
	
	//Quick Sort  时间复杂度为O(n*log2 n)
	void quickSort(int arr[],final int left,final int right)
	{
		if(left<right)
		{
			int pivotpos = partition(arr,left,right);
			quickSort(arr,left,pivotpos-1);
			quickSort(arr,pivotpos+1,right);
		}
	}
	
	int partition(int arr[],final int low,final int high)
	{
		int pivotpos = low;
		int temp = 0;
		for(int i=low+1;i<=high;++i)
		{
			if(arr[low]>arr[i])
			{
				++pivotpos;
				if(pivotpos!=i)
				{
					temp = arr[i];
					arr[i] = arr[pivotpos];
					arr[pivotpos] = temp;
				}
			}
		}
		
		temp = arr[low];
		arr[low] = arr[pivotpos];
		arr[pivotpos] = temp;
		return pivotpos;
	}
	
	public static void main(String[]args){
		final int SIZE = 30;
		int arr[]=new int[SIZE];
		Sort sort = new Sort();
		Random rand = new Random();
		
		for(int i=0;i<SIZE;++i)
		{
			//produce random number,maximum is 100
			arr[i] = rand.nextInt(100);
			System.out.print(arr[i]+"  ");
		}
		System.out.println();
		
		/****************************************************************
		System.out.println("Bubble Sort:");
		sort.bubbleSort(arr);
		
		for(int i=0;i<arr.length;i++)
		{
			System.out.print(arr[i]+"  ");
		}
		System.out.println();
		****************************************************************/
		/**************************************************************
		System.out.println("Select Sort:");
		sort.selectSort(arr);
		
		for(int i=0;i<arr.length;++i)
			System.out.print(arr[i]+"  ");
		System.out.println();
		***************************************************************/
		/**************************************************************
		System.out.println("Insert Sort:");
		sort.insertSort(arr);
		for(int i=0;i<arr.length;++i)
			System.out.print(arr[i]+"  ");
		System.out.println();
		****************************************************************/
		/***************************************************************
		System.out.println("Shell Sort:");
		sort.shellSort(arr);
		
		for(int i=0;i<arr.length;++i)
			System.out.print(arr[i]+"  ");
		System.out.println();
		*****************************************************************/
		/****************************************************************/
		System.out.println("Quick Sort:");
		sort.quickSort(arr, 0, arr.length-1);
		//int a[] = {21,25,16,8,60,49};
		//sort.quickSort(a, 0, a.length-1);
		for(int i=0;i<arr.length;++i)
			System.out.print(arr[i]+"  ");
		System.out.println();
	}
}

 
 




更多排序算法有个博客链接排序算法总结!

你可能感兴趣的:(排序算法Java实现)