作者:opheliamaizi | 来源:互联网 | 2024-10-16 10:01
冒泡:publicclassBubbleSort{publicstaticvoidmain(String[]orgs){int[]a{2,1,10,2,8,6,7,20};for
冒泡:
public class BubbleSort {
public static void main(String[] orgs){
int[] a = {2,1,10,2,8,6,7,20};
for(int i = 0 ; i for(int j = 1 ; j <= a.length - 1 - i; j++){
if(a[j] 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 while(start pivot){
end --;
}
if(start array[start] = array[end];
}
while(start start ++;
}
if(start 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 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 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 array[lchild]){
min = lchild;
}
if(rchild 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 for(int i = length; i > 0;i = i / 2){
if(array[i] 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 for(int j = i; j > 0 && (a[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 min = i;
for(int j = i + 1;j if(a[j] min = j;
}
}
if(i != min){
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
for(int foo : a){
System.out.print(foo + " ");
}
}
}