1.冒泡排序
/**
* Created by jinzhao.w on 2015/4/7.
*/
public class BubbleSort {
public static int [] sort(int [] array){
int temp=0;
for(int i=array.length-1;i>0;--i){
for(int j=0;j<i;j++ ){
if(array[j]>array[j+1]){
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
return array;
}
public static void main(String [] args){
int[] test={1,3,2,5,4};
BubbleSort.sort(test);
for(int i=0;i<test.length;i++){
System.out.print(test[i]);
}
}
}
-------------------------------------------------------------------------
2.选择排序
/**
* Created by 737597978 on 2015/4/8.
*/
public class SelectSort {
public static int[] sort(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
int min = i;
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[min]) {
min = j;
}
}
swap(i, min, array);
}
return array;
}
private static void swap(int i, int min, int[] array) {
int temp = array[i];
array[i] = array[min];
array[min] = temp;
}
public static void main(String[] args) {
int[] test = {7, 1, 4, 5, 9, 8};
SelectSort.sort(test);
for (int i = 0; i < test.length; i++) {
System.out.print(test[i]);
}
}
}
-----------------------------------------------------------------------------------------
3.插入排序
/**
* Created by 737597978 on 2015/4/8.
*/
public class InsertSort {
public static int [] sort(int array[]){
for(int i=1;i<array.length;i++){
int temp=array[i];
int inner=i;
while (inner>0&&array[inner-1]>=temp){
array[inner]=array[inner-1];
--inner;
}
array[inner]=temp;
}
return array;
}
public static void main(String [] args){
int[] test={3,2,4,5,7,1};
InsertSort.sort(test);
for(int i=0;i<test.length;i++){
System.out.print(test[i]);
}
}
}
----------------------------------------------------------------------------------------------------
4.希尔排序
/**
* Created by 737597978 on 2015/4/8.
*/
public class ShellSort {
public static void sort(int[] array) {
int inner, outer;
int temp;
int h = 1;
while (h <= array.length / 3) {
h = 3 * h + 1;
}
while (h > 0) {
for (outer = h; outer < array.length; outer++) {
temp = array[outer];
inner = outer;
while (inner > h - 1 && array[inner - h] >= temp) {
array[inner] = array[inner - h];
inner -= h;
}
array[inner] = temp;
}
h = (h - 1) / 3;
}
}
public static void main(String[] args) {
int[] test = {2, 3, 4, 1, 5};
ShellSort.sort(test);
for (int i = 0; i < test.length; i++) {
System.out.print(test[i]);
}
}
}
----------------------------------------------------------------------------------------------
5.快速排序
/**
* Created by 737597978 on 2015/4/9.
*/
public class QuickSort {
private static int[] chaosAarray = {2, 3, 7, 1, 5};
public static void sort(int left, int right) {
if (right <= left) {
return;
} else {
int pivot = chaosAarray[right];
int partition = partitionIt(left, right, pivot);
sort(left, partition - 1);
sort(partition + 1, right);
}
}
public static int partitionIt(int left, int right, int pivot) {
int leftPtr = left - 1;
int rightPtr = right;
while (true) {
while (chaosAarray[++leftPtr] < pivot) ;
while (rightPtr > 0 && chaosAarray[--rightPtr] > pivot) ;
if (leftPtr >= rightPtr) {
break;
} else {
swap(leftPtr, rightPtr);
}
}
//调整枢纽值在数组中的位置 将枢钮值跟数组最右边的值对换
swap(leftPtr, right);
return leftPtr;
}
public static void swap(int leftPtr, int rightPtr) {
int temp = chaosAarray[rightPtr];
chaosAarray[rightPtr] = chaosAarray[leftPtr];
chaosAarray[leftPtr] = temp;
}
public static void main(String[] args) {
QuickSort.sort(0, chaosAarray.length - 1);
for (int i = 0; i < chaosAarray.length; i++) {
System.out.print(chaosAarray[i]);
}
}
}