排序算法

 

1.bubbleSort

import java.util.Scanner;

public class BubleSort {

	/**
	 * @param args   2011.10.05
	 */
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int num;
		int[] nums = new int[100];
		int i=0;
		while (true) {
			num=scanner.nextInt();
			if (num==-1)
				break;
			nums[i++] = num;
			
		}
		bubblesort(nums,i);
		for(int j=0;j<i;j++){
			System.out.print(nums[j]+" ");
		}
		System.out.println();
		
	}
	static void bubblesort(int[] nums,int n){
		for( int i=0;i<n-1;i++){
			boolean changed=false;
			for(int j=n-1;j>=i+1;j--){
				if(nums[j-1]>nums[j]){
					nums[j-1]=nums[j-1]+nums[j];
					nums[j]=nums[j-1]-nums[j];
					nums[j-1]=nums[j-1]-nums[j];
//					int tmp=nums[j-1];
//					nums[j-1]=nums[j];
//					nums[j]=tmp;
					changed=true;
				}
			}
			if(!changed) break;
		}
		
	}

}



2.quicksort

#include <stdio.h>
#include <stdlib.h>

void swap(int *a,int *b)/*交换*/
{
	int temp=*a;
	*a=*b;
	*b=temp;
}

int partition(int data[],int l, int h)  /*i的作用是指向可被交换的位置,j指向待判断的位置,返回中值位置*/
{	
	int i=l-1;
	int j;
	int pivot=data[h];
	for(j=1;j<h;j++)
	{
		if(data[j]<pivot)
		{
			i++;
			swap(&data[i],&data[j]);
		}
	}
	swap(&data[i+1],&data[h]);
	return i+1;
}

void quicksort(int data[],int l, int h)
{
	if(l<h)
	{
		int k=partition(data,l,h);
		quicksort(data,l,k-1);
		quicksort(data,k+1,h);
	}
}
int main()
{
	int data[] ={5,6,3,2,1};
	quicksort(data,0,4);
	for(int t=0;t<5;t++)
	{
		cout<<data[t]<<" "<<endl;
	}
	return 0;
}


1,3,4,5,2


快排有一个地方值得注意:partition的时候,两个标志位既可以同时先后从左往右,也可以一左一右同时进行。

int partition2(int data[],int l,int h)
{
	int i=l;
	int j=h;
	int pivot=data[i];
	for(i<j)
	{
		while(data[j]>=pivot && i<j)
			j--;
		data[i]=data[j];
		while(data[i]<=pivot && i<j)
			i++;
		data[j]=data[i];
		
	}
	data[i]=pivot;
	return i;
}


代码来自july


quicksort java version

public static <T extends Comparable<? super T>> void quickSort(T[]a, int low, int high){
		if(low+CUTOFF>high)  //子列较小时采用插入排序
			insertionSort(a,low,high);
		else{
			int middle=(low+high)/2;
			if(a[middle].compareTo(a[low])<0)
				swapReference(a,low,middle);
			if(a[high].compareTo(a[low])<0)
				swapReference(a,low,middle);
			if(a[high].compareTo(a[middle])<0)
				swapReference(a,middle,high);
			
			swapReference(a,middle,high-1);        //将middle位置的元素放在high-1位置
			T pivot = a[high-1];
			
			int i,j;
			for(i=low,j=high;;){
				while(a[++i].compareTo(pivot)<0)
					;
				while(pivot.compareTo(a[--j])<0)
					;
				if(i>=j)
					break;
				swapReference(a,i,j);
			}
			swapReference(a,i,high-1);
			quickSort(a, low, i-1);
			quickSort(a, i+1, high);
		}
	}

自己写的quicksort

public class QuickSort {

	/**
	 * @param args
	 */

	public static void main(String[] args) {


		int[] nums = { 2, 55, 33, 11, 9 };
		quicksort(nums, 0, nums.length - 1);
		for (int i = 0; i < nums.length; i++) {
			System.out.print(nums[i] + " ");
		}
		System.out.println();

	}

	static void quicksort(int nums[], int low, int high) {               //这里体现的是divide and conquer的思想。
		if (low < high) {   
			int pivotpos = partition(nums, low, high);
			quicksort(nums, low, pivotpos - 1);
			quicksort(nums, pivotpos + 1, high);
		}
	}

	static int partition(int[] nums, int low, int high) {
		int pivotpos=low;                                           //quick sort的速度取决于输入,此外很到程度上跟pivot的选择有关!
		int pivot = nums[low];
		for(int i=low+1;i<=high;i++){                               //这个循环里面的内容是比较难以理解的。脑子里多过即便就好了
			if(nums[i]<pivot){
				pivotpos++;
				if(i!=pivotpos) {
					int tmp=nums[i];
					nums[i]=nums[pivotpos];
					nums[pivotpos]=tmp;
				}
			}
		}
		nums[low]=nums[pivotpos];
		nums[pivotpos]=pivot;
		return pivotpos;
	}

}




3.ShellSort

import java.util.Scanner;

public class ShellSort {

	/**
	 * @param args  2011.10.05  值得注意的是:直接插入排序其实就是gap=1的时候的
	 * 				希尔排序情形。熟悉了直接插入排序那么希尔排序也就很容易写出来了。
	 */
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = 0; // 待排序数组长度
		int num;
		int[] nums = new int[100];
		while (true) {
			num = scanner.nextInt();
			if (num == -1)
				break;
			nums[n++] = num;
		}
		shellsort(nums, n);
		for (int i = 0; i < n; i++) {
			System.out.print(nums[i] + " ");
		}
		System.out.println();
	}

	static void shellsort(int[] nums, int n) {
		int gap = n;
		do {
			gap = gap / 3 + 1;
			for (int i = gap; i < n; i++) {
				if (nums[i - gap] > nums[i]) {
					int tmp = nums[i];
					int j = i - gap;
					do {
						nums[j + gap] = nums[j];
						j = j - gap;
					} while (j >= 0 && nums[j] > tmp);
					nums[j + gap] = tmp;
				}
			}
		} while (gap > 1);
	}

}



4.insertion sort

import java.util.Scanner;


public class InsertSort {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int num;
		int[] nums = new int[100];
		int n=0;   //数组长度
		while(true){
			num=scanner.nextInt();
			if(num==-1) break;
			nums[n++]=num;
		}
		insertsort(nums, n);
		for(int i=0;i<n;i++){
			System.out.print(nums[i]+" ");
		}
		System.out.println();
		
	}
	static void insertsort(int[] nums, int n){
		for( int i=1;i<n;i++){
			if(nums[i-1]>nums[i]){
				int tmp=nums[i];
				int j=i-1;
				do {
					nums[j+1]=nums[j];
					j--;
				} while (j>=0 && nums[j]>tmp);
				nums[j+1]=tmp;
			}
		}
	}

}

5.binary insertion sort

import java.util.Scanner;


public class BinaryInsertSort {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n=0;             //待排序数组长度
		int num;
		int[] nums = new int[100];
		while(true){
			num=scanner.nextInt();
			if(num==-1) break;
			nums[n++]=num;
		}
		binaryinsertsort(nums, n);
		for(int i=0;i<n;i++){
			System.out.print(nums[i]+" ");
		}
		System.out.println();
	}
	
	static void binaryinsertsort(int nums[], int n){
		for(int i=1;i<n;i++){
			int temp=nums[i];
			int low=0;
			int high=i-1;
			while(low<=high){            //这里的等号很关键
				int mid=(low+high)/2;
				if(nums[mid]<nums[i]){
					low=mid+1;
				}else{
					high=mid-1;
				}
			}
			for(int j=i-1;j>=low;j--){
				nums[j+1]=nums[j];
			}
			nums[low]=temp;
			
		}
	}

}

6.计数排序

import java.util.Scanner;

import javax.security.auth.kerberos.KerberosKey;


public class CountingSort {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("0~n?n=");
		int n=scanner.nextInt();
		
		System.out.print("length of the unsorted array?:");
		int lenthA=scanner.nextInt();
		
		System.out.println("please enter the unsorted array(end with -1):");
		int[] A = new int[lenthA];
		int [] B = new int[lenthA];
		int i=0;
		while(true){
			int k=scanner.nextInt();
			if(k==-1) break;
			A[i++]=k;
		}
		
		int [] C = new int[n+1];
		countingsort(A, B, C);
		for(int j=0;j<A.length;j++){
			System.out.print(" "+B[j]);
		}
	}
	
	static void countingsort(int[] A,int[] B,int[] C){
		for(int i=0;i<C.length;i++){
			C[i]=0;
		}
		for(int j=0;j<A.length;j++){
			C[A[j]]=C[A[j]]+1;
		}
		for(int i=1;i<C.length;i++){
			C[i]=C[i]+C[i-1];
		}
		for(int j=A.length-1;j>=0;j--){
			B[C[A[j]]-1]=A[j];
			C[A[j]]=C[A[j]]-1;
		}
	}

}



 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值