冒泡:
public class BubbleSort {
public static void main(String[] orgs){
int[] a = {2,1,10,2,8,6,7,20};
for(int i = 0 ; i < a.length; i++){
for(int j = 1 ; j <= a.length - 1 - i; j++){
if(a[j] < a[j - 1]){
int temp = a[j];
a[j] = a[j-1];
a[j - 1] = temp;
}
}
}
for(int foo : a){
System.out.print(foo + " ");
}
}
}
快速排序:
public class QuickSort {
public int partition(int[] array,int start,int end){
int pivot = array[start];
while(start < end){
while(start < end && array[end] > pivot){
end --;
}
if(start < end){
array[start] = array[end];
}
while(start < end && array[start] <= pivot){
start ++;
}
if(start < end){
array[end] = array[start];
}
}
array[start] = pivot;
return start;
}
public void quicksort(int[] array,int start,int end){
if(array == null || array.length<=1){
return;
}
if(start < end){
int p = partition(array, start, end);
quicksort(array,start,p - 1);
quicksort(array,p + 1,end);
}
}
public static void main(String[] orgs){
QuickSort qs = new QuickSort();
int[] a = {2,1,10,2,8,6,7,20};
qs.quicksort(a, 0, a.length - 1);
for(int foo : a){
System.out.print(foo + " ");
}
}
}
归并排序:
public class MergeSort {
public void merge(int[] array,int[] temp,int start,int mid, int end){
int i = start;
int j = mid + 1;
for(int k= start; k <= end; k++){
temp[k] = array[k];
}
while(i <= mid && j <= end){
if(temp[i] <= temp[j]){
array[start++] = temp[i];
i++;
}else{
array[start++] = temp[j];
j++;
}
}
while(i <= mid){
array[start++] = temp[i++];
}
while(j <= end){
array[start++] = temp[j++];
}
}
public void mergesort(int[] array,int[] temp,int start,int end){
if(array == null || array.length <= 1){
return;
}
if(start < end){
int mid = start + (end - start)/2 ;
mergesort(array,temp,start,mid);
mergesort(array,temp,mid+1,end);
merge(array,temp,start,mid,end);
}
}
public static void main(String[] orgs){
MergeSort ms = new MergeSort();
int[] a = {2,1,10,2,8,6,7,20};
int[] temp = new int[a.length];
ms.mergesort(a, temp, 0, a.length - 1);
for(int foo : a){
System.out.print(foo + " ");
}
}
}
堆排序:
public class HeapSort {
public void heapify(int[] array,int start,int end){
int length = end;
int lchild = 2*start + 1;
int rchild = 2*start + 2;
int min = start;
if(lchild < length && array[min] > array[lchild]){
min = lchild;
}
if(rchild < length && array[min] > array[rchild]){
min = rchild;
}
if(start != min){
int temp = array[min];
array[min] = array[start];
array[start] = temp;
heapify(array,min,end);
}
}
public int extractMin(int[] array,int length){
int ret = Integer.MIN_VALUE;
if(array != null && array.length > 0){
ret = array[0];
array[0] = array[length];
}
return ret;
}
public void swim(int[] array,int length){
if(length < array.length){
for(int i = length; i > 0;i = i / 2){
if(array[i] < array[i/2]){
int temp = array[i];
array[i] = array[i/2];
array[i/2] = temp;
}
}
}
}
public static void main(String[] orgs){
int[] a = {2,1,10,2,8,6,7,20};
HeapSort hs = new HeapSort();
int length = a.length - 1;
hs.heapify(a,0,length);
while(length >= 0){
System.out.print(hs.extractMin(a,length) + " ");
hs.heapify(a,0,--length);
}
}
}
插入排序:
public class InsertSort {
public static void main(String[] orgs){
int[] a = {2,1,10,2,8,6,7,20};
for(int i = 1 ; i < a.length; i++){
for(int j = i; j > 0 && (a[j] < a[j - 1]); j--){
int temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
for(int foo : a){
System.out.print(foo + " ");
}
}
}
选择排序:
public class SelectSort {
public static void main(String[] orgs){
int[] a = {2,1,10,2,8,6,7,20};
int min = 0;
for(int i = 0; i < a.length; i++){
min = i;
for(int j = i + 1;j < a.length; j++){
if(a[j] < a[min]){
min = j;
}
}
if(i != min){
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
for(int foo : a){
System.out.print(foo + " ");
}
}
}