排序算法java实现
内排序和外排序。在排序过程中,全部记录存放在内存,则称为内排序,如果排序过程中需要使用外存,则称为外排序。
内排序有可以分为以下几类:
(1)、插入排序:直接插入排序、二分法插入排序、希尔排序。
(2)、选择排序:简单选择排序、堆排序。
(3)、交换排序:冒泡排序、快速排序。
(4)、归并排序
(5)、基数排序
1.直接插入排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class StraitInsertionSort { public static void main(String[] args) { int[] a = {32,4,557,12,76,2,34,56,21,67,88,43}; for (int i =1;i<a.length;i++) { int temp = a[i]; int j; for(j=i-1;j>=0;j--) { //将大于temp的往后移一位 if(a[j]>temp) { a[j+1]=a[j]; } else { break; } a[j]=temp; } } for (int i = 0; i<a.length;i++) { System.out.print(a[i]+" "); } } }
2.二分法插入排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class BinarySort { public static void main(String[] args) { int[] a = { 12, 54, 23, 7, 96, 32, 4, 62, 21, 97, 23, 123, 564, 99 }; // 当right<left时,停止查找插入的位置,插入位置为a[left]或a[right+1] for (int i = 0; i < a.length; i++) { int temp = a[i]; int left = 0; int right = i - 1; int mid = 0; while (left <= right) { mid = (left + right) / 2; if (temp < a[mid]) { right = mid - 1; } else { left = mid + 1; } } // 插入位置a[left]到a[i-1]之间的数字向后移一位 for (int j = i - 1; j >= left; j--) { a[j + 1] = a[j]; } a[right+1] = temp; } for (int m = 0; m < a.length; m++) { System.out.print(a[m] + " "); } } }
3.希尔排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class ShellSort { public static void main(String[] args) { // TODO Auto-generated method stub int[] a ={123,5,21,87,54,87,23,11,3,57,90,75,9}; //不稳定,两个数字相同会被移动 int d=a.length; while(true) { d=d/2; for(int x=0;x<d;x++) { for(int i=x+d;i<a.length;i=i+d) { int temp=a[i]; int j; for(j=i-d;j>=0&&a[j]>temp;j=j-d) { a[j+d]=a[j]; } a[j+d]=temp; } } if(d==1) { break; } } for(int i =0;i<a.length;i++) { System.out.print(a[i]+" "); } } }
4.简单选择排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class StraitSelectionSort { public static void main(String[] args) { int[] a={23,5,3,21,88,45,23,455,644,322,456,9}; for(int i=0;i<a.length;i++) { int min=a[i]; int n=i;//最小数的索引 for(int j=i+1;j<a.length;j++) { if(a[j]<min) { min=a[j]; n=j; } } a[n]=a[i]; a[i]=min; } for(int i=0;i<a.length;i++) { System.out.print(a[i]+" "); } } }
5.堆排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.util.Arrays; public class HeapSort { public static void main(String[] args) { int[] a={40,24,64,77,89,3,2,13,75,33,13,54,23,98}; int arrayLength=a.length; for(int i=0;i<arrayLength;i++) { //建堆 buildMaxHeap(a,arrayLength-1-i); //交换堆顶和最后一个元素 swap(a,0,arrayLength-1-i); System.out.println(Arrays.toString(a)); } } //对data数组从0到LastIndex建大顶堆 public static void buildMaxHeap(int[] data,int lastIndex) { //从lastIndex的父节点开始 for(int i=(lastIndex-1)/2;i>=0;i--) { //k保存正在判断的节点 int k=i; //如果当前k节点的子节点存在 while(k*2+1<=lastIndex) { //k节点左子节点的索引 int biggerIndex=2*k+1; //如果biggerIndex小于lastIndex,则biggerIndex+1代表的k的右子节点存在 if(biggerIndex<lastIndex) { //如果右子节点较大 if(data[biggerIndex]<data[biggerIndex+1]) { //biggerIndex总是记录较大子节点的索引 biggerIndex++; } } //如果k节点的值小于较大子节点的值 if(data[k]<data[biggerIndex]) { swap(data,k,biggerIndex); //将biggerIndex赋予k,开始下一次循环 k=biggerIndex; } else { break; } } } } private static void swap(int[] data, int i, int j) { // TODO Auto-generated method stub int tmp=data[i]; data[i]=data[j]; data[j]=tmp; } }
6.冒泡排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class BubbleSort { public static void main(String[] args) { // TODO Auto-generated method stub int[] a ={32,54,12,78,95,23,12,65,88,99}; for(int i=0;i<a.length;i++) { //遍历一次把最大的沉下去,或把最小的浮上来 for(int j=a.length-i-1;j>0;j--) { if(a[j-1]>a[j]) { int tmp=a[j]; a[j]=a[j-1]; a[j-1]=tmp; } } } for (int i=0;i<a.length;i++) { System.out.print(a[i]+" "); } } }
7.快速排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class QuikSort { public static void main(String[] args) { int[] a = { 32, 5, 6, 7, 341, 12, 567, 84, 234, 99 }; quik(a); for(int i=0;i<a.length;i++) { System.out.print(a[i]+" "); } } private static void quik(int[] a) { if (a.length > 0) { quickSort(a, 0, a.length - 1); } } private static void quickSort(int[] a, int low, int high) { // TODO Auto-generated method stub if (low < high) { int mid = getMiddle(a, low, high); quickSort(a, low, mid - 1); quickSort(a, mid + 1, high); } } private static int getMiddle(int[] a, int low, int high) { // TODO Auto-generated method stub int tmp = a[low];// 基准元素 while (low < high) { // 找到比基准元素小的元素位置 while (low < high && a[high] >= tmp) { high--; } a[low] = a[high]; while (low < high && a[low] < tmp) { low++; } a[high] = a[low]; } a[low] = tmp; return low; } }
8.归并排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class MergeSort { public static void main(String[] args) { // TODO Auto-generated method stub int[] a ={12,34,66,8,4,13,76,90,56,11,36}; mergeSort(a,0,a.length-1); for(int i=0;i<a.length;i++) { System.out.print(a[i]+" "); } } private static void mergeSort(int[] a, int left, int right) { // TODO Auto-generated method stub if(left<right) { int middle=(left+right)/2; mergeSort(a,left,middle); mergeSort(a,middle+1,right); merge(a,left,middle,right); } } private static void merge(int[] a, int left, int middle, int right) { // TODO Auto-generated method stub int[] tmpArr=new int[a.length]; int mid = middle+1;//右边的起始位置 int tmp=left; int third=left; while(left<=middle&&mid<=right) { //从两个数组中取较小的数放入tmpArr if(a[left]<=a[mid]) { tmpArr[third++]=a[left++]; } else { tmpArr[third++]=a[mid++]; } } //将剩余部分放入tmpArr while(left<=middle) { tmpArr[third++]=a[left++]; } while(mid<=right) { tmpArr[third++]=a[mid++]; } while(tmp<=right) { a[tmp]=tmpArr[tmp++]; } } }
9.基数排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.util.ArrayList; import java.util.List; public class BucketSort { public static void main(String[] args) { // TODO Auto-generated method stub int[] a = { 23, 65, 8, 23, 67, 8, 42, 45, 78, 934, 66, 34 }; sort(a); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } private static void sort(int[] array) { // TODO Auto-generated method stub // 找到最大数,确定要排序几遍 int max = 0; for (int i = 0; i < array.length; i++) { if (max < array[i]) { max = array[i]; } } // 判断位数 int times = 0; while (max > 0) { max = max / 10; times++; } // 建立十个队列 List<ArrayList> queue = new ArrayList<ArrayList>(); for (int i = 0; i < 10; i++) { ArrayList queue1 = new ArrayList(); queue.add(queue1); } // 进行times次分配和收集 for (int i = 0; i < times; i++) { // 分配 for (int j = 0; j < array.length; j++) { int x = array[j] % (int) Math.pow(10, i + 1) / (int) Math.pow(10, i); ArrayList<Integer> queue2 = queue.get(x); queue2.add(array[j]); queue.set(x, queue2); } // 收集 int count = 0; for (int j = 0; j < 10; j++) { while (queue.get(j).size() > 0) { ArrayList<Integer> queue3 = queue.get(j); array[count] = queue3.get(0); queue3.remove(0); count++; } } } } }
参考链接:http://www.cnblogs.com/liuling/p/2013-7-24-01.html
To follow the path:
look to the master,
follow the master,
walk with the master,
see through the master,
become the master.