public class Sort {
/**
* @param args
*/
public static void selectSort(int[] a){
int temp = 0;
int flag = 0;
for (int i = 0; i < a.length; i++) {
temp = a[i];
flag = i;
for (int j = i+1; j < a.length; j++) {
if (temp > a[j]) {
temp = a[j];
flag = j;
}
}
if (flag != i) {
a[flag] = a[i];
a[i] = temp;
}
}
}
public static void insertSort(int[] a){
int temp;
int j;
for (int i = 1; i < a.length; i++) {
temp = a[i];
j = i;
if (temp < a[j-1]) {
while (j >= 1 && temp < a[j-1]) {
a[j] = a[j-1];
j--;
}
}
a[j] = temp;
}
}
public static void bubbleSort(int[] a) {
int temp;
int len = a.length;
for (int i = 0; i < a.length -1; i++) {
for (int j = len - 1; j > i; j--) {
if (a[j] < a[j - 1]) {
temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
}
}
}
}
//快速排序
public static void sortOfQuicksort(int[] a,int low ,int high){
int i,j;
int index;
if (low >= high) {
return;
}
i = low;
j = high;
index =a[i];
while(i < j){
while(i<j&&a[j]>=index)//右边都是比index大的数
j--;
if(i<j)//否则交换
a[i++] = a[j];
while(i<j&&a[i]<index)
i++;
if(i<j)
a[j--] = a[i];
}
a[i] = index;//将关键的数index放中间,左边都是比index小的数,右边都是比index大的数
sortOfQuicksort(a,low,i-1);//递归调用直到low>=high,即low>= i-1
sortOfQuicksort(a,i+1,high);
}
public static void quickSort(int[] a){
sortOfQuicksort(a,0,a.length-1);
}
//希尔排序
public static void shellSort(int[] a){
int len = a.length;
int i,j;
int h;
int temp;
for (h = len/2; h > 0; h= h/2) {
for (i = h; i < a.length; i++) {
temp = a[i];
for ( j = i - h; j >= 0; j -= h) {
if (temp < a[j]) {
a[j+h] = a[j];
}
else
break;
}
a[j+h] = temp;
}
}
}
/* 堆排序
* pos:子堆位置
* len:整个堆索引最大值
*/
public static void adjustMinHeap(int[] a,int pos,int len){
int child = 0;
int temp = 0;
for (temp = a[pos]; 2*pos+1 <=len; pos = child) {
child = 2*pos+1;
if(child < len&&a[child] > a[child+1]){
child++;
}
if(a[child] < temp){//if 和else if作用不同
a[pos] = a[child];
}
else
break;
}
a[pos] = temp;
}
public static void HeapSort(int[] a){
int len = a.length;
int i = 0;
for (i = len/2 - 1; i >= 0; i--) {
adjustMinHeap(a,i,len - 1);
}
int temp = 0;
for (int j = len - 1; j > 0; j--) {
temp = a[j];
a[j] = a[0];
a[0] = temp;
adjustMinHeap(a,0,j - 1);
}
}
//归并排序
public static void merge(int[] a,int left,int mid ,int right){
int lengthLeft = mid - left +1;
int lengthRight = right - mid;
int[] L = new int[lengthLeft];
int[] R = new int[lengthRight];
System.arraycopy(a, left, L, 0, lengthLeft);
System.arraycopy(a, mid+1, R, 0, lengthRight);
int k = 0;
int i = 0, j = 0;
// for(i = 0,k = left;i < lengthLeft;i++,k++)
// L[i] = a[k];
// for(i = 0,k = mid+1;i < lengthRight;i++,k++)
// R[i] = a[k];
for (i = 0,j = 0,k = left; i < lengthLeft&&j < lengthRight; k++) {
if(L[i] < R[j]){
a[k] = L[i];
++i;
}
else{
a[k] = R[j];
++j;
}
}
if(i < lengthLeft){
System.arraycopy(L, i, a, k, lengthLeft - i );//lengthLeft表示长度,无需加一
// for(j = i;j < lengthLeft;j++,k++){
// a[k] = L[j];
// }
}
if(j < lengthRight)
System.arraycopy(R, j, a, k, lengthRight - j );
// for(i=j;i<lengthRight;i++,k++){
// a[k] = R[i];
// }
}
public static void mergeSort(int[] a,int left,int right ){
int mid = 0;
if(left < right){
mid = (left + right)/2;
mergeSort(a,left,mid);
mergeSort(a,mid+1,right);
merge(a,left,mid,right);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {9,4,2,6,0,1,2,6,8};
// int[] temp = new int[3] ;
// mergeSort(a,0,a.length - 1);
// Sort.shellSort(a);
insertSort(a);
// bubbleSort(a);
// System.arraycopy(a, 0, temp, 0, 3);
for (int i = 0; i < a.length; i++) {
System.out.print(" "+a[i]);
}
}
}
常用排序算法Java实现
最新推荐文章于 2022-10-17 10:23:19 发布