八种排序算法(内部排序)
八种排序算法很长时间没有使用了,今天做一个总结,方便以后自己用的时候参考。
这八种排序算法都是内部算法,这八种排序算法分别是:
1. 插入排序
1)直接插入排序
2)希尔排序
2.选择排序
1)简单选择排序
2)堆排序
3.交换排序
1)冒泡排序
2)快速排序
4.归并排序
5.基数排序
public class DirectInsertionSort {
/**
* 直接插入排序
* @param args
*/
public static void main(String[] args) {
int[] arrayInt = {17,56,80,17,12,9,100,64,42,37,64,82,123,974,314,548};
int length = arrayInt.length;
for( int i=1; i<length; i++){
for( int j=i; j>0; j--){
if( arrayInt[j] <= arrayInt[j-1]){
int k = arrayInt[j];
arrayInt[j] = arrayInt[j-1];
arrayInt[j-1] = k;
}
}
}
for( int i=0; i<length; i++){
System.out.println( arrayInt[i]);
}
}
}
public class ShellSort {
/**
* 希尔排序
* @param args
*/
public static void main(String[] args) {
int[] arrayInt = {17,56,80,17,12,9,100,64,42,37,64,82,123,974,314,548};
int length = arrayInt.length;
System.out.println( "length="+length);
shellSort( arrayInt);
for( int i=0; i<length; i++){
System.out.println( "arrayInt[" + i + "]=" + arrayInt[i]);
}
}
public static void shellSort( int[] arrayInt){
int j = 0;
int temp = 0;
for ( int increment=arrayInt.length/2; increment>0; increment/=2){
for ( int i=increment; i<arrayInt.length; i++){
temp = arrayInt[i];
for ( j=i; j>=increment; j-=increment){
if( temp > arrayInt[j-increment]){
arrayInt[j] = arrayInt[j - increment];
}else{
break;
}
}
arrayInt[j] = temp;
}
}
}
}
public class SimpleSelectionSort {
/**
* 简单选择排序
* @param args
*/
public static void main(String[] args) {
int[] arrayInt = {17,56,80,17,12,9,100,64,42,37,64,82,123,974,314,548};
int length = arrayInt.length;
for( int i=0; i<length; i++){
for( int j=i+1; j<length; j++){
if( arrayInt[i] > arrayInt[j]){
int k = arrayInt[i];
arrayInt[i] = arrayInt[j];
arrayInt[j] = k;
}
}
}
for( int i=0; i<length; i++){
System.out.println( arrayInt[i]);
}
}
}
public class BubbleSort {
/**
* 冒泡排序
* @param args
*/
public static void main(String[] args) {
int[] arrayInt = {17,56,80,17,12,9,100,64,42,37,64,82,123,974,314,548};
int length = arrayInt.length;
for( int i=0; i<length-1; i++){
for( int j=0; j<length-i-1; j++){
if( arrayInt[j] > arrayInt[j+1]){
int k = arrayInt[j];
arrayInt[j] = arrayInt[j+1];
arrayInt[j+1] = k;
}
}
}
for( int i=0; i<length; i++){
System.out.println( arrayInt[i]);
}
}
}
public class QuickSort {
/**
* 快速排序
* @param args
*/
public static void main(String[] args) {
int[] arrayInt = {17,56,80,17,12,9,100,64,42,37,64,82,123,974,314,548};
int start = 0;
int end = arrayInt.length-1;
quickSort( arrayInt, start, end);
}
public static void quickSort( int[] arrayInt, int start, int end){
if( start >= end){
return;
}
int i = start;
int j = end;
int value = arrayInt[start];
boolean flag = true;
while( i != j){
if( flag){
if( value > arrayInt[j]){
int k = arrayInt[i];
arrayInt[i] = arrayInt[j];
arrayInt[j] = k;
i++;
flag = false;
}else{
j--;
}
}else{
if( value < arrayInt[i]){
int k = arrayInt[i];
arrayInt[i] = arrayInt[j];
arrayInt[j] = k;
j--;
flag = true;
}else{
i++;
}
}
}
for( int m=0; m<arrayInt.length; m++){
System.out.print(arrayInt[m] + " ");
}
System.out.println();
quickSort( arrayInt, start, j-1);
quickSort( arrayInt, i+1, end);
}
}
public class MergeSort {
/**
* 归并排序
* @param args
*/
public static void main(String[] args) {
int[] arrayInt = {17,56,80,17,12,9,100,64,42,37,64,82,123,974,314,548};
int start = 0;
int end = arrayInt.length-1;
mergeSort( arrayInt, start, end);
for( int i=0; i<arrayInt.length; i++){
System.out.print( arrayInt[i]+" ");
}
}
public static int[] mergeSort( int[] arrayInt, int start, int end){
int middle = ( start+end)/2;
if( start < end){
mergeSort( arrayInt, start, middle);
mergeSort( arrayInt, middle+1, end);
sort( arrayInt, start, middle, end);
}
return arrayInt;
}
public static void sort( int[] arrayInt, int start, int middle, int end){
int[] arrayTemp = new int[ end-start+1];
int i = start;
int j = middle +1;
int k = 0;
while( i <= middle && j <= end){
if( arrayInt[i] < arrayInt[j]){
arrayTemp[k] = arrayInt[i];
k++;
i++;
}else{
arrayTemp[k] = arrayInt[j];
k++;
j++;
}
}
while( i<=middle){
arrayTemp[k] = arrayInt[i];
k++;
i++;
}
while( j <= end){
arrayTemp[k] = arrayInt[j];
k++;
j++;
}
for( int m=0; m<arrayTemp.length; m++){
arrayInt[m+start] = arrayTemp[m];
}
}
}