冒泡排序,快速排序,归并排序,选择排序,插入排序,堆排序
来源:互联网 发布:java二进制流转字符串 编辑:程序博客网 时间:2024/06/09 09:04
冒泡:
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 + " ");}}}
- 插入排序、冒泡排序、选择排序、Shell排序、快速排序、归并排序、堆排序
- C# 插入排序 冒泡排序 选择排序 快速排序 堆排序 归并排序 基数排序 希尔排序
- 冒泡排序 快速排序 选择排序 堆排序 直接插入排序 希尔排序 归并排序
- 冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序java实现
- 插入排序、希尔排序、冒泡排序、快速排序、选择排序、堆排序、归并排序
- 冒泡排序,插入排序,快速排序,归并排序,堆排序,选择排序,希尔排序
- 插入排序、二分插入排序、希尔排序、选择排序、冒泡排序、鸡尾酒排序、快速排序、堆排序、归并排序
- 插入排序、冒泡排序、选择排序、快速排序、堆排序、归并排序算法比较
- 冒泡排序,选择排序,插入排序,堆排序,归并排序,快速排序
- 冒泡排序,快速排序,归并排序,选择排序,插入排序,堆排序
- 快速排序 选择排序 插入排序 冒泡排序 堆排序 归并排序 基数排序
- 面试题:冒泡排序,选择排序,插入排序,快速排序,归并排序,堆排序
- 冒泡排序 快速排序 插入排序 堆排序 选择排序
- 插入排序、选择排序、冒泡排序、快速排序、堆排序
- 内排序(插入排序、冒泡排序、选择排序、shell排序、快速排序、归并排序、堆排序)
- 排序算法: 冒泡排序, 快速排序,希尔排序,直接插入排序 ,直接选择排序,归并排序,堆排序
- 各种排序算法总结----基数排序、归并排序、插入排序、冒泡排序、选择排序、快速排序、堆排序、希尔排序
- 七种排序算法,包括:插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序
- C++ char*,const char*,string的相互转换
- C# 将字符串转换成GB2312--很蛋疼的一个Class
- InterlockedIncrement函数详解
- wmi 查找BIOS信息
- 黑马程序员_Java高新技术——内省,注解
- 冒泡排序,快速排序,归并排序,选择排序,插入排序,堆排序
- 解决Oracle更改数据库表名报错
- 互联网 DBA 需要做那些事
- How to handle jQuery.ajax(..) JSON request ?
- Eclipse快捷键大全
- ubuntu更新源
- Linux script 命令记录(数据库)操作步骤
- 【数组与指针】如何在C/C++中动态分配二维数组
- Android中使用Post请求