java中的排序算法——插入排序详解
生活随笔
收集整理的這篇文章主要介紹了
java中的排序算法——插入排序详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package com.algorithm;
/**
?* 插入排序及其變體
?*?
?* List可轉化為數組進行排序
?* Object數組中的元素必須實現Comparable接口,即元素必須是可比的
?*/
public class InsertSort {
/**
* 直接插入排序
*/
public static void insertSort(Object[] a){
Object cur = null; //保存當前遍歷到的元素
int len = a.length;
for(int i = 1;i < len;i++){
if(((Comparable)a[i]).compareTo(a[i-1]) < 0){ //如果后一個元素小于前一個
cur = a[i];
a[i] = a[i-1];
int j;
//在前面已經有序的表中查找插入位置,并移動元素
for(j = i-2;j>=0 && ((Comparable)cur).compareTo(a[j]) < 0;j--){
a[j+1] = a[j];
}
a[j+1] = cur;
}
}
}
/**
* 二分插入排序
* 使用二分查找查找插入位置時因為前面元素都已經有序
* 減小查找插入位置的比較次數,但元素移動次數不變
*/
public static void binaryInsertSort(Object[] a){
Object cur = null;
int len = a.length;
for(int i=1;i<len;i++){
if(((Comparable)a[i]).compareTo(a[i-1]) < 0){ //后一個元素小于前一個
cur = a[i];
int low = 0; //下界
int high = i - 1;
while(low <= high){ //找插入位置的索引
int mid = (low + high) / 2;
if(((Comparable)cur).compareTo(a[mid]) < 0){
high = mid - 1;
}else{
low = mid +1;
}
}
for(int j = i -1;j > high;j--){ //把high之后的元素右移
a[j+1] = a[j];
}
a[ high + 1] = cur;
}
}
}
/**
* 2-路插入排序
* 需要一個輔助數組s,及指向s的第一個及最后一個位置的索引,first和last,
* 即從first遍歷到final得到的列是有序的
* 2-路插入排序是在二分插入排序的基礎上進行改進,
* 減少了排序過程中移動的元素的次數,但增加了輔助空間.
*?
*/
public static void twoRoadInsertSort(Object[] a){
int len = a.length;
Object[] s = new Object[len]; //輔助數組
s[0] = a[0];
int first = len;
int last = 0;
for(int i = 1;i < len;i++){
if(((Comparable)a[i]).compareTo(s[0])<0){ ?//小于s[0],插入到s[0]之前,即從數組最后開始插入
int j;
for(j = first; j < len - 1 && ((Comparable)a[i]).compareTo(s[j])>0;j++){
s[j-1] = s[j];
}
s[j-1] = a[i];
first--;
}else{ //大于s[0],插入到s[0]之后
int j;
//查找插入位置并移動元素
for(j = last; j > 0 && ((Comparable)a[i]).compareTo(s[j])<0;j--){
s[j+1] = s[j];
}
s[j+1] = a[i];
last++;
}
}
System.out.println("first:"+first +" last:"+last);
//重新對a賦值
int count = 0;
for(int i = first;i < len;i++){
a[count++] = s[i];
}
for(int i=0;i<=last;i++){
a[count++] = s[i];
}
}
//test
public static void main(String[] args) {
Integer[] data = {49,38,65,97,76,13,27,49,14};
insertSort(data);
//binaryInsertSort(data);
//twoRoadInsertSort(data);
for(Integer d:data){
System.out.println(d);
}
}
}
/**
?* 插入排序及其變體
?*?
?* List可轉化為數組進行排序
?* Object數組中的元素必須實現Comparable接口,即元素必須是可比的
?*/
public class InsertSort {
/**
* 直接插入排序
*/
public static void insertSort(Object[] a){
Object cur = null; //保存當前遍歷到的元素
int len = a.length;
for(int i = 1;i < len;i++){
if(((Comparable)a[i]).compareTo(a[i-1]) < 0){ //如果后一個元素小于前一個
cur = a[i];
a[i] = a[i-1];
int j;
//在前面已經有序的表中查找插入位置,并移動元素
for(j = i-2;j>=0 && ((Comparable)cur).compareTo(a[j]) < 0;j--){
a[j+1] = a[j];
}
a[j+1] = cur;
}
}
}
/**
* 二分插入排序
* 使用二分查找查找插入位置時因為前面元素都已經有序
* 減小查找插入位置的比較次數,但元素移動次數不變
*/
public static void binaryInsertSort(Object[] a){
Object cur = null;
int len = a.length;
for(int i=1;i<len;i++){
if(((Comparable)a[i]).compareTo(a[i-1]) < 0){ //后一個元素小于前一個
cur = a[i];
int low = 0; //下界
int high = i - 1;
while(low <= high){ //找插入位置的索引
int mid = (low + high) / 2;
if(((Comparable)cur).compareTo(a[mid]) < 0){
high = mid - 1;
}else{
low = mid +1;
}
}
for(int j = i -1;j > high;j--){ //把high之后的元素右移
a[j+1] = a[j];
}
a[ high + 1] = cur;
}
}
}
/**
* 2-路插入排序
* 需要一個輔助數組s,及指向s的第一個及最后一個位置的索引,first和last,
* 即從first遍歷到final得到的列是有序的
* 2-路插入排序是在二分插入排序的基礎上進行改進,
* 減少了排序過程中移動的元素的次數,但增加了輔助空間.
*?
*/
public static void twoRoadInsertSort(Object[] a){
int len = a.length;
Object[] s = new Object[len]; //輔助數組
s[0] = a[0];
int first = len;
int last = 0;
for(int i = 1;i < len;i++){
if(((Comparable)a[i]).compareTo(s[0])<0){ ?//小于s[0],插入到s[0]之前,即從數組最后開始插入
int j;
for(j = first; j < len - 1 && ((Comparable)a[i]).compareTo(s[j])>0;j++){
s[j-1] = s[j];
}
s[j-1] = a[i];
first--;
}else{ //大于s[0],插入到s[0]之后
int j;
//查找插入位置并移動元素
for(j = last; j > 0 && ((Comparable)a[i]).compareTo(s[j])<0;j--){
s[j+1] = s[j];
}
s[j+1] = a[i];
last++;
}
}
System.out.println("first:"+first +" last:"+last);
//重新對a賦值
int count = 0;
for(int i = first;i < len;i++){
a[count++] = s[i];
}
for(int i=0;i<=last;i++){
a[count++] = s[i];
}
}
//test
public static void main(String[] args) {
Integer[] data = {49,38,65,97,76,13,27,49,14};
insertSort(data);
//binaryInsertSort(data);
//twoRoadInsertSort(data);
for(Integer d:data){
System.out.println(d);
}
}
}
總結
以上是生活随笔為你收集整理的java中的排序算法——插入排序详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中的排序算法——简单选择排序,树
- 下一篇: java中的排序算法——归并排序