冒泡排序:
[java] view plain copy print ? import java.util.Arrays; public class Bubbling { public int[] result(int[] array){ //int型可以换成是Object,但数组元素必须实现compareto()方法。 if(array.length==0){ return array; }else{ int length,temp; length = array.length; for(int i=1;i for(int a=0;a if(array[a]>array[a+1]){ temp =array[a]; array[a]=array[a+1]; array[a+1]=temp; } } } } return array; } public static void main(String[] args) { int[] a =new int[5]; a[0]=1; a[1]=5; a[2]=3; a[3]=0; a[4]=6; Bubbling bubbling = new Bubbling(); a=bubbling.result(a); System.out.println(Arrays.toString(a)); }
快速排序:
[java] view plain copy print ? public class QuickSort { public static void main(String[] args) { quicksort qs = new quicksort(); int data[] = {44,22,2,32,54,22,88,77,99,11}; qs.data = data; qs.sort(0, qs.data.length-1); qs.display(); } } class quicksort { public int data[]; private int partition(int sortArray[],int low,int hight)//划分 { int key = sortArray[low]; while(low { while(low=key) hight--; sortArray[low] = sortArray[hight]; while(low<=key) low++; sortArray[hight] = sortArray[low]; } sortArray[low] = key; return low; } public void sort(int low,int hight) { if(low { int result = partition(data,low,hight);//把整体分成独立的两部分,返回轴值位置 sort(low,result-1);//左侧重复 sort(result+1,hight);//右侧重复 } } public void display() { for(int i=0;i { System.out.print(data[i]); System.out.print(" "); } } }插入排序:
[java] view plain copy print ? import java.util.Arrays; public class InsertionSort { public int[] getResult(int[] objects){ int temp,in,out; for(out=1;out temp =objects[out]; in =out; while(in>0&&objects[in-1]>temp){ objects[in]=objects[in-1]; --in; } objects[in]=temp; } return objects; } public static void main(String[] args) { int[] a={1,3,2,4,0}; InsertionSort insertionSort =new InsertionSort(); a=insertionSort.getResult(a); System.out.println(Arrays.toString(a)); } }希尔排序:
[java] view plain copy print ? import java.util.Arrays; public class Shell_Sort { public int[] shellSort(int[] theArray) { int inner=0,outer=0; int nElems=theArray.length; long temp=0; int h=1; //find initial value of h while(h<=nElems/3) //1,4,13,40,121,... h=h*3+1; while(h>0) { //当间隔h>1时,进行小部分插入排序;当间隔h=1时,进行整体插入排序 for(outer=h;outer { temp=theArray[outer]; inner=outer; while(inner>h-1&&theArray[inner-h]>=temp) { theArray[inner]=theArray[inner-h]; inner-=h; } theArray[inner]=(int) temp; } h=(h-1)/3; // 间隔从大减小,一直减小到1,此时因为间隔为1,就相当于是做整体的插入排序 } return theArray; } public static void main(String[] args) { int[] a={1,4,2,7,3,12,44,21,55,32,11}; Shell_Sort g=new Shell_Sort(); a=g.shellSort(a); System.out.println(Arrays.toString(a)); } }
选择排序:
[java] view plain copy print ? import java.util.Arrays; public class SelectSort { public int[] getResult(int[] objects){ int in,out,min,temp; //如果数组为null,直接返回 if(objects==null||objects.length==0){ return objects; } for(out=0;out1;out++){ min =out; //寻找最小值 for(in=out+1;in if(objects[min]>objects[in]){ min = in; } } //与out交换位置 temp =objects[out]; objects[out]=objects[min]; objects[min]=temp; } return objects; } public static void main(String[] args) { SelectSort ss = new SelectSort(); int[] a ={2,5,4,1}; a=ss.getResult(a); System.out.println(Arrays.toString(a)); } }