本文目錄一覽:
Java編程:簡化的插入排序?
代碼:(如果輸入必須是有序數組,不然方法內要先排序)
結果:
請給出java幾種排序方法
java常見的排序分為:
1 插入類排序
主要就是對於一個已經有序的序列中,插入一個新的記錄。它包括:直接插入排序,折半插入排序和希爾排序
2 交換類排序
這類排序的核心就是每次比較都要「交換」,在每一趟排序都會兩兩發生一系列的「交換」排序,但是每一趟排序都會讓一個記錄排序到它的最終位置上。它包括:起泡排序,快速排序
3 選擇類排序
每一趟排序都從一系列數據中選擇一個最大或最小的記錄,將它放置到第一個或最後一個為位置交換,只有在選擇後才交換,比起交換類排序,減少了交換記錄的時間。屬於它的排序:簡單選擇排序,堆排序
4 歸併類排序
將兩個或兩個以上的有序序列合併成一個新的序列
5 基數排序
主要基於多個關鍵字排序的。
下面針對上面所述的演算法,講解一些常用的java代碼寫的演算法
二 插入類排序之直接插入排序
直接插入排序,一般對於已經有序的隊列排序效果好。
基本思想:每趟將一個待排序的關鍵字按照大小插入到已經排序好的位置上。
演算法思路,從後往前先找到要插入的位置,如果小於則就交換,將元素向後移動,將要插入數據插入該位置即可。時間複雜度為O(n2),空間複雜度為O(1)
package sort.algorithm;
public class DirectInsertSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int data[] = { 2, 6, 10, 3, 9, 80, 1, 16, 27, 20 };
int temp, j;
for (int i = 1; i data.length; i++) {
temp = data[i];
j = i – 1;
// 每次比較都是對於已經有序的
while (j = 0 data[j] temp) {
data[j + 1] = data[j];
j–;
}
data[j + 1] = temp;
}
// 輸出排序好的數據
for (int k = 0; k data.length; k++) {
System.out.print(data[k] + ” “);
}
}
}
三 插入類排序之折半插入排序(二分法排序)
條件:在一個已經有序的隊列中,插入一個新的元素
折半插入排序記錄的比較次數與初始序列無關
思想:折半插入就是首先將隊列中取最小位置low和最大位置high,然後算出中間位置mid
將中間位置mid與待插入的數據data進行比較,
如果mid大於data,則就表示插入的數據在mid的左邊,high=mid-1;
如果mid小於data,則就表示插入的數據在mid的右邊,low=mid+1
最後整體進行右移操作。
時間複雜度O(n2),空間複雜度O(1)
package sort.algorithm;
//折半插入排序
public class HalfInsertSort {
public static void main(String[] args) {
int data[] = { 2, 6, 10, 3, 9, 80, 1, 16, 27, 20 };
// 存放臨時要插入的元素數據
int temp;
int low, mid, high;
for (int i = 1; i data.length; i++) {
temp = data[i];
// 在待插入排序的序號之前進行折半插入
low = 0;
high = i – 1;
while (low = high) {
mid = (low + high) / 2;
if (temp data[mid])
high = mid – 1;
else
// low=high的時候也就是找到了要插入的位置,
// 此時進入循環中,將low加1,則就是要插入的位置了
low = mid + 1;
}
// 找到了要插入的位置,從該位置一直到插入數據的位置之間數據向後移動
for (int j = i; j = low + 1; j–)
data[j] = data[j – 1];
// low已經代表了要插入的位置了
data[low] = temp;
}
for (int k = 0; k data.length; k++) {
System.out.print(data[k] + ” “);
}
}
}
四 插入類排序之希爾排序
希爾排序,也叫縮小增量排序,目的就是儘可能的減少交換次數,每一個組內最後都是有序的。
將待續按照某一種規則分為幾個子序列,不斷縮小規則,最後用一個直接插入排序合成
空間複雜度為O(1),時間複雜度為O(nlog2n)
演算法先將要排序的一組數按某個增量d(n/2,n為要排序數的個數)分成若干組,每組中記錄的下標相差d.對每組中全部元素進行直接插入排序,然後再用一個較小的增量(d/2)對它進行分組,在每組中再進行直接插入排序。當增量減到1時,進行直接插入排序後,排序完成。
package sort.algorithm;
public class ShellSort {
public static void main(String[] args) {
int a[] = { 1, 54, 6, 3, 78, 34, 12, 45, 56, 100 };
double d1 = a.length;
int temp = 0;
while (true)
{
//利用這個在將組內倍數減小
//這裡依次為5,3,2,1
d1 = Math.ceil(d1 / 2);
//d為增量每個分組之間索引的增量
int d = (int) d1;
//每個分組內部排序
for (int x = 0; x d; x++)
{
//組內利用直接插入排序
for (int i = x + d; i a.length; i += d) {
int j = i – d;
temp = a[i];
for (; j = 0 temp a[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]+” “);
}
}
五 交換類排序之冒泡排序
交換類排序核心就是每次比較都要進行交換
冒泡排序:是一種交換排序
每一趟比較相鄰的元素,較若大小不同則就會發生交換,每一趟排序都能將一個元素放到它最終的位置!每一趟就進行比較。
時間複雜度O(n2),空間複雜度O(1)
package sort.algorithm;
//冒泡排序:是一種交換排序
public class BubbleSort {
// 按照遞增順序排序
public static void main(String[] args) {
// TODO Auto-generated method stub
int data[] = { 2, 6, 10, 3, 9, 80, 1, 16, 27, 20, 13, 100, 37, 16 };
int temp = 0;
// 排序的比較趟數,每一趟都會將剩餘最大數放在最後面
for (int i = 0; i data.length – 1; i++) {
// 每一趟從開始進行比較,將該元素與其餘的元素進行比較
for (int j = 0; j data.length – 1; j++) {
if (data[j] data[j + 1]) {
temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
for (int i = 0; i data.length; i++)
System.out.print(data[i] + ” “);
}
}
java 數組插入排序改錯 急
修改Score類聲明,實現java.lang.Comparable介面;
public class Score implements java.lang.Comparable{
在方法getStuInfomation() 後追加兩個方法(compareTo為Comparable介面要求實現此方法,toString為重寫基類的列印輸出方法):
public int compareTo(Object otherScore) {
return this.stuScore – ((Score) otherScore).stuScore;
}
public String toString() {
return “[學號:” + stuNumber + “分數:” + stuScore + “姓名:” + stuName + “]”;
}
重新編譯Score.java,再運行Test,輸出結果為:
———-
[學號:3分數:323姓名:Lily]
[學號:5分數:332姓名:Mike]
[學號:2分數:445姓名:Fangfang]
[學號:1分數:472姓名:Liming]
[學號:4分數:540姓名:Green]
–2008/04/05
哎……
package method2;
import java.util.ArrayList;
import java.util.Collections;
public class Test {
public static void main(String args[]) {
ArrayList list = new ArrayList();
list.add(new Score(1, “Liming”, 472));
list.add(new Score(2, “Fangfang”, 445));
list.add(new Score(3, “Lily”, 323));
list.add(new Score(4, “Green”, 540));
list.add(new Score(5, “Mike”, 332));
Collections.sort(list, new StuScoreComparator());
for (int i = 0; i list.size(); i++) {
System.out.println(list.get(i).toString());
}
Collections.binarySearch(list, new Score(1, “Liming”, 472));
}
}
class StuScoreComparator extends Score implements java.util.Comparator {
public int compare(Object o1, Object o2) {
Score score1 = (Score) o1;
Score score2 = (Score) o2;
return score1.getStuScore() – score2.getStuScore();
}
}
package method2;
/**
*
* @author Liuzhichao
*/
public class Score implements java.lang.Comparable {
private int stuNumber;
private String stuName;
private int stuScore;
Score() {
}
public Score(int stuNumber, String stuName, int stuScore) {
if (stuNumber 0) {
this.stuNumber = stuNumber;
} else {
System.out.println(“輸入的學生學號不正確”);
System.exit(1);
}
if (stuScore 0) {
this.stuScore = stuScore;
} else {
System.out.println(“輸入的學生學號不正確”);
System.exit(1);
}
this.stuName = stuName;
}//設置學生信息
public void setStuNumber(int stuNumber) {
this.stuNumber = stuNumber;
}//設置學生學號
public void setStuScore(int stuScore) {
this.stuScore = stuScore;
}//設置學生分數
public void setStuName(String stuName) {
this.stuName = stuName;
}//設置學生名字
public int getStuNumber() {
return this.stuNumber;
}//設置學生學號
public int getStuScore() {
return this.stuScore;
}//設置學生分數
public String getStuName() {
return this.stuName;
}//獲取學生名字
public String getStuInfomation() {
return “名字:” + this.stuName + ” 學號:” + this.stuNumber + ” 成績:” + this.stuScore;
}//獲取學生信息
public int compareTo(Object otherScore) {
return this.stuScore – ((Score) otherScore).stuScore;
}
public String toString() {
return “[學號:” + stuNumber + “分數:” + stuScore + “姓名:” + stuName + “]”;
}
}
Java 直接插入排序法
比如數組[3,2,1,5]\x0d\x0a這段處理就返回[1,2,3,5]\x0d\x0a它的處理是從第二位開始依次跟前邊的比,比前邊的小就往前移動。\x0d\x0a也就是[3,2,1,5]\x0d\x0a [2,3,1,5]\x0d\x0a [1,2,3,5]\x0d\x0a(int j = i – 1; j = 0 temp
回答於 2022-11-16
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/194543.html