新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Java中的排序方法有哪些
今天就跟大家聊聊有關(guān)Java中的排序方法有哪些,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),平頂山網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:平頂山等地區(qū)。平頂山做網(wǎng)站價(jià)格咨詢:13518219792
直接插入排序
import java.util.HashMap;
public class InsertSort {
private static int contrastCount = 0;//對(duì)比次數(shù)
private static int swapCount = 0;//交換次數(shù)
public static void main(String[] args) {
System.out.println("直接插入排序:\n 假設(shè)前面的序列都已經(jīng)排序好了,把后面未排序的數(shù)往已排好序的序列內(nèi)插入,時(shí)間復(fù)雜度O(n^2),空間復(fù)雜度O(1),穩(wěn)定排序");
HashMap hashMap = new HashMap();
init(hashMap);//初始化
System.out.println("初始序列為:");
print(hashMap, 0);//打印
insert(hashMap);//排序
}
/**
* 初始化函數(shù)
* @param hashMap
*/
private static void init(HashMap hashMap) {
hashMap.put(0, null);//第一位置空
hashMap.put(1, 0);
hashMap.put(2, 5);
hashMap.put(3, 11);
hashMap.put(4, 12);
hashMap.put(5, 13);
hashMap.put(6, 4);
hashMap.put(7, 1);
hashMap.put(8, 5);
hashMap.put(9, 8);
hashMap.put(10, 6);
hashMap.put(11, 4);
hashMap.put(12, 8);
}
/**
* 進(jìn)行插入排序
* @param hashMap 待排序的表
*/
private static void insert(HashMap hashMap){
System.out.println("開始插入排序:");
int i,j;
//排序開始時(shí)間
long start = System.currentTimeMillis();
for(i=2; i
冒泡排序
import java.util.HashMap;
/**
* 冒泡排序
* @author HHF
* 2014年3月19日
*/
public class BubbleSort {
private static int contrastCount = 0;//對(duì)比次數(shù)
private static int swapCount = 0;//交換次數(shù)
public static void main(String[] args) {
System.out.println("冒泡排序:\n 第一輪使最大值沉淀到最底下,采用從頭開始的兩兩比較的辦法,如果i大于i++則交換,下一次有從第一個(gè)開始循環(huán),比較次數(shù)減一,然后依次重復(fù)即可,"
+ "\n 如果一次比較為發(fā)生任何交換,則可提前終止,時(shí)間復(fù)雜度O(n^2),空間復(fù)雜度O(1),穩(wěn)定排序");
HashMap hashMap = new HashMap();
init(hashMap);//初始化
System.out.println("初始序列為:");
print(hashMap, 0);//打印
bubble(hashMap);//排序
}
/**
* 初始化函數(shù)
* @param hashMap
*/
private static void init(HashMap hashMap) {
hashMap.put(0, null);//第一位置空
hashMap.put(1, 10);
hashMap.put(2, 5);
hashMap.put(3, 11);
hashMap.put(4, 12);
hashMap.put(5, 13);
hashMap.put(6, 4);
hashMap.put(7, 1);
hashMap.put(8, 5);
hashMap.put(9, 8);
hashMap.put(10, 6);
hashMap.put(11, 4);
hashMap.put(12, 8);
}
/**
* 進(jìn)行冒泡排序
* @param hashMap 待排序的表
*/
private static void bubble(HashMap hashMap){
System.out.println("開始冒泡排序:");
//排序開始時(shí)間
long start = System.currentTimeMillis();
boolean swap = false;//是否發(fā)生交換
int count = 1;//只為了計(jì)數(shù)
for(int i=1; ihashMap.get(j+1)){//需要發(fā)生交換j 和 j+1
hashMap.put(0, hashMap.get(j));
hashMap.put(j, hashMap.get(j+1));
hashMap.put(j+1, hashMap.get(0));
swap = true;
contrastCount++;//發(fā)生一次對(duì)比
swapCount++;//發(fā)生一次交換
swapCount++;//發(fā)生一次交換
swapCount++;//發(fā)生一次交換
}
contrastCount++;//跳出if還有一次對(duì)比
}
print(hashMap, count++);
if(!swap)
break;
}
//排序結(jié)束時(shí)間
long end = System.currentTimeMillis();
System.out.println("結(jié)果為:");
print(hashMap, 0);//輸出排序結(jié)束的序列
hashMap.clear();//清空
System.out.print("一共發(fā)生了 "+contrastCount+" 次對(duì)比\t");
System.out.print("一共發(fā)生了 "+swapCount+" 次交換\t");
System.out.println("執(zhí)行時(shí)間為"+(end-start)+"毫秒");
}
/**
* 打印已排序好的元素
* @param hashMap 已排序的表
* @param j 第j趟排序
*/
private static void print(HashMap hashMap, int j){
if(j != 0)
System.out.print("第 "+j+" 趟:\t");
for(int i=1; i
快速排序
import java.util.HashMap;
public class QuickSort {
private static int contrastCount = 0;//對(duì)比次數(shù)
private static int swapCount = 0;//交換次數(shù)
public static void main(String[] args) {
System.out.println("快速排序:\n 任取一個(gè)數(shù)作為分界線,比它小的放到左邊,比它大的放在其右邊,然后分別對(duì)左右進(jìn)行遞歸,時(shí)間復(fù)雜度O(nLgn),空間復(fù)雜度O(n),不穩(wěn)定排序");
HashMap hashMap = new HashMap();
init(hashMap);//初始化
System.out.println("初始序列為:");
print(hashMap, 0, 0);//打印
System.out.println("開始快速排序:");
//排序開始時(shí)間
long start = System.currentTimeMillis();
quick(hashMap, 1, hashMap.size()-1);//排序
//排序結(jié)束時(shí)間
long end = System.currentTimeMillis();
System.out.println("結(jié)果為:");
print(hashMap, 0, 0);//輸出排序結(jié)束的序列
hashMap.clear();//清空
System.out.print("一共發(fā)生了 "+contrastCount+" 次對(duì)比\t");
System.out.print("一共發(fā)生了 "+swapCount+" 次交換\t");
System.out.println("執(zhí)行時(shí)間為"+(end-start)+"毫秒");
System.out.println("(注:此輸出序列為代碼執(zhí)行過(guò)程的序列, 即先左邊遞歸 再 右邊遞歸, 而教科書上的遞歸序列往往是左右同時(shí)進(jìn)行的結(jié)果,特此說(shuō)明)");
}
/**
* 初始化函數(shù)
* @param hashMap
*/
private static void init(HashMap hashMap) {
hashMap.put(0, null);//第一位置空
hashMap.put(1, 10);
hashMap.put(2, 5);
hashMap.put(3, 11);
hashMap.put(4, 12);
hashMap.put(5, 13);
hashMap.put(6, 4);
hashMap.put(7, 1);
hashMap.put(8, 5);
hashMap.put(9, 8);
hashMap.put(10, 6);
hashMap.put(11, 4);
hashMap.put(12, 8);
}
/**
* 進(jìn)行快速排序
* @param hashMap 待排序的表
* @param low
* @param high
*/
static int count = 1;
private static void quick(HashMap hashMap, int low, int high){
if(low hashMap, int low, int high){
hashMap.put(0, hashMap.get(low));//選擇一個(gè)分界值 此時(shí)第low位元素內(nèi)的值已經(jīng)無(wú)所謂被覆蓋了
swapCount++;//發(fā)生一次交換
while(lowhashMap.get(low)){//開始從左往右走 直到有不對(duì)的數(shù)據(jù) 或者 到頭了
low++;
contrastCount++;//發(fā)生一次對(duì)比
}
if(low hashMap, int j, int k){
if(j != 0)
System.out.print("第 "+j+" 趟:(分界線為"+k+")\t");
for(int i=1; i
直接選擇排序
import java.util.HashMap;
public class SelectionSort {
private static int contrastCount = 0;//對(duì)比次數(shù)
private static int swapCount = 0;//交換次數(shù)
public static void main(String[] args) {
System.out.println("選擇排序:\n 每次選擇最小的,然后與對(duì)應(yīng)的位置處元素交換,時(shí)間復(fù)雜度O(n^2),空間復(fù)雜度O(1),不穩(wěn)定排序");
HashMap hashMap = new HashMap();
init(hashMap);//初始化
System.out.println("初始序列為:");
print(hashMap, 0, 0);//打印
select(hashMap);//排序
}
/**
* 初始化函數(shù)
* @param hashMap
*/
private static void init(HashMap hashMap) {
hashMap.put(0, null);//第一位置空
hashMap.put(1, 10);
hashMap.put(2, 5);
hashMap.put(3, 11);
hashMap.put(4, 12);
hashMap.put(5, 13);
hashMap.put(6, 4);
hashMap.put(7, 1);
hashMap.put(8, 5);
hashMap.put(9, 8);
hashMap.put(10, 6);
hashMap.put(11, 4);
hashMap.put(12, 8);
}
/**
* 進(jìn)行選擇排序
* @param hashMap 待排序的表
*/
private static void select(HashMap hashMap){
System.out.println("開始選擇排序:");
//排序開始時(shí)間
long start = System.currentTimeMillis();
int count = 1;//只為統(tǒng)計(jì)執(zhí)行次數(shù)
for(int i=hashMap.size()-1; i>1; i--){//需要循環(huán)查詢的次數(shù) 最后一個(gè)元素不用考慮
int k = i;//記錄本次查找序列最大值的下標(biāo) 初始為該數(shù)應(yīng)該要放的位置
for(int j=1; j
堆排序
import java.util.HashMap;
public class HeapSort {
private static int contrastCount = 0;//對(duì)比次數(shù)
private static int swapCount = 0;//交換次數(shù)
private static int printCount = 1;//執(zhí)行打印次數(shù)
public static void main(String[] args) {
System.out.println("堆排序:\n 首先建立一個(gè)堆(方法是首先把序列排成二叉樹,然后從下往上,從右往左使得每一個(gè)小子樹中的父節(jié)點(diǎn)大于子節(jié)點(diǎn),然后從上往下,從左往右記錄堆入序列),"
+ "\n 然后把堆的根節(jié)點(diǎn)和最底下 的孩子節(jié)點(diǎn)交換,整理堆,再重復(fù)交換,整理,時(shí)間復(fù)雜度O(nlgn),空間復(fù)雜度O(1),不穩(wěn)定排序");
HashMap hashMap = new HashMap();
init(hashMap);//初始化
System.out.println("初始序列為:");
print(hashMap, 0);//打印
heap(hashMap);//排序
}
/**
* 初始化函數(shù)
* @param hashMap
*/
private static void init(HashMap hashMap) {
hashMap.put(0, null);//第一位置空
hashMap.put(1, 10);
hashMap.put(2, 5);
hashMap.put(3, 11);
hashMap.put(4, 12);
hashMap.put(5, 13);
hashMap.put(6, 4);
hashMap.put(7, 1);
hashMap.put(8, 5);
hashMap.put(9, 8);
hashMap.put(10, 6);
hashMap.put(11, 4);
hashMap.put(12, 8);
}
/**
* 進(jìn)行堆排序
* @param hashMap 待排序的表
*/
private static void heap(HashMap hashMap){
System.out.println("開始建堆:");
//排序開始時(shí)間87
long start = System.currentTimeMillis();
for(int i=(hashMap.size()-1)/2; i>=1; i--){//開始建堆
sift(hashMap, i, hashMap.size()-1);//把所有的節(jié)點(diǎn)調(diào)好位置即可以
}
System.out.println("建堆成功");
for(int j=hashMap.size()-1; j>=1; j--){//每次都把第一個(gè)元素與最后一個(gè)未排序的交換 然后再調(diào)整第一個(gè)節(jié)點(diǎn)即可
hashMap.put(0, hashMap.get(1));
hashMap.put(1, hashMap.get(j));
hashMap.put(j, hashMap.get(0));
sift(hashMap, 1, j-1);//剩下要排序的數(shù)位為j-1
swapCount++;//發(fā)生一次交換
swapCount++;//發(fā)生一次交換
swapCount++;//發(fā)生一次交換
}
//排序結(jié)束時(shí)間
long end = System.currentTimeMillis();
System.out.println("結(jié)果為:");
print(hashMap, 0);//輸出排序結(jié)束的序列
hashMap.clear();//清空
System.out.print("一共發(fā)生了 "+contrastCount+" 次對(duì)比\t");
System.out.print("一共發(fā)生了 "+swapCount+" 次交換\t");
System.out.println("執(zhí)行時(shí)間為"+(end-start)+"毫秒");
}
/**
* 把第i位的元素移動(dòng)到合適位置 與其子孩子的最大值比較 如果有發(fā)生交換 則繼續(xù)往下比較
* @param hashMap
* @param i 待移動(dòng)的數(shù)下標(biāo)
* @param n 表示要查找的范圍 從1到n個(gè)
*/
private static void sift(HashMap hashMap, int i, int n){
int j = 2*i;//j為i的左孩子
hashMap.put(0, hashMap.get(i));//當(dāng)前被待排序的數(shù)
while(j<=n){//如果有左孩子的
if(hashMap.containsKey(j+1) && hashMap.get(j) hashMap, int j){
if(j != 0)
System.out.print("第 "+j+" 趟:\t");
for(int i=1; i
歸并排序
import java.util.HashMap;
public class MergeSort {
private static int contrastCount = 0;//對(duì)比次數(shù)
private static int swapCount = 0;//交換次數(shù)
private static int printCount = 1;//只為統(tǒng)計(jì)執(zhí)行次數(shù)
public static void main(String[] args) {
System.out.println("歸并爾排序:\n 先將數(shù)據(jù)分為n組,然后沒兩組開始合并,相鄰兩個(gè)合并為一個(gè)新的有序隊(duì)列,重復(fù)合并直到整個(gè)隊(duì)列有序,時(shí)間復(fù)雜度O(nlgn),空間復(fù)雜度O(n),穩(wěn)定排序");
HashMap hashMap = new HashMap();//未排序
HashMap hashMapNew = new HashMap();//已排序
hashMapNew.put(0, null);//第一位置空
init(hashMap);//初始化
System.out.println("初始序列為:");
print(hashMap, 0);//打印
System.out.println("開始?xì)w并爾排序:");
//排序開始時(shí)間
long start = System.currentTimeMillis();
merge(hashMap, hashMapNew, 1, hashMap.size()-1);//排序
//排序結(jié)束時(shí)間
long end = System.currentTimeMillis();
System.out.println("結(jié)果為:");
print(hashMapNew, 0);//輸出排序結(jié)束的序列
hashMap.clear();//清空
System.out.print("一共發(fā)生了 "+contrastCount+" 次對(duì)比\t");
System.out.print("一共發(fā)生了 "+swapCount+" 次交換\t");
System.out.println("執(zhí)行時(shí)間為"+(end-start)+"毫秒");
System.out.println("(注:此輸出序列為代碼執(zhí)行過(guò)程的序列, 即先左邊遞歸 再 右邊遞歸, 而教科書上的遞歸序列往往是左右同時(shí)進(jìn)行的結(jié)果,特此說(shuō)明)");
}
/**
* 初始化函數(shù)
* @param hashMap
*/
private static void init(HashMap hashMap) {
hashMap.put(0, null);//第一位置空
hashMap.put(1, 10);
hashMap.put(2, 5);
hashMap.put(3, 11);
hashMap.put(4, 12);
hashMap.put(5, 13);
hashMap.put(6, 4);
hashMap.put(7, 1);
hashMap.put(8, 5);
hashMap.put(9, 8);
hashMap.put(10, 6);
hashMap.put(11, 4);
hashMap.put(12, 8);
}
/**
* 進(jìn)行歸并爾排序
* @param hashMap 待排序的表
* @param hashMapNew 已排序的表
*/
private static void merge(HashMap hashMap, HashMap hashMapNew, int low, int high){
if(low == high){
hashMapNew.put(low, hashMap.get(low));
swapCount++;//發(fā)生一次交換
}else{
int meddle = (int)((low+high)/2);//將這一序列數(shù)均分的中間值
merge(hashMap, hashMapNew, low, meddle);//繼續(xù)對(duì)左邊的序列遞歸
merge(hashMap, hashMapNew, meddle+1, high);//對(duì)右邊的序列遞歸
mergeSort(hashMap, hashMapNew, low, meddle, high);//把相鄰的序列組合起來(lái)
for(int i=low; i<=high; i++){//將已經(jīng)排好序的hashMapNew【low,high】覆蓋hashMap【low,high】以便進(jìn)入下一次的遞歸歸并
hashMap.put(i, hashMapNew.get(i));
swapCount++;//發(fā)生一次交換
}
}
}
/**
* 進(jìn)行歸并爾排序 把【low,meddle】和【meddle+1,high】和并為一個(gè)有序的hashMapNew【low,high】
* @param hashMap 待排序的表
* @param hashMapNew 已排序的表
* @param low 低位
* @param meddle 中位
* @param high 高位
*/
private static void mergeSort(HashMap hashMap, HashMap hashMapNew, int low, int meddle, int high){
int k = low;
int j = meddle+1;
while(low<=meddle && j<=high){//兩個(gè)序列組合成一個(gè)序列 從小到達(dá)的順序
if(hashMap.get(low) < hashMap.get(j)){
hashMapNew.put(k++, hashMap.get(low++));//放入合適的位置
}else{
hashMapNew.put(k++, hashMap.get(j++));//放入合適的位置
}
contrastCount++;//發(fā)生一次對(duì)比
swapCount++;//發(fā)生一次交換
}
//如果有一方多出來(lái)了 則直接賦值
while(low<=meddle){
hashMapNew.put(k++, hashMap.get(low++));//放入合適的位置
swapCount++;//發(fā)生一次交換
}
while(j<=high){
hashMapNew.put(k++, hashMap.get(j++));//放入合適的位置
swapCount++;//發(fā)生一次交換
}
print(hashMapNew, printCount++);
}
/**
* 打印已排序好的元素
* @param hashMap 已排序的表
* @param j 第j趟排序
*/
private static void print(HashMap hashMap, int j){
if(j != 0)
System.out.print("第 "+j+" 趟:\t");
for(int i=1; i
最低位優(yōu)先基數(shù)排序
/**
* 最低位優(yōu)先基數(shù)排序
* @author HHF
*
*/
public class LSDSort {
private static int contrastCount = 0;//對(duì)比次數(shù)
private static int swapCount = 0;//交換次數(shù)
private static int printCount = 1;//只為統(tǒng)計(jì)執(zhí)行次數(shù)
public static void main(String[] args) {
System.out.println("最低位優(yōu)先基數(shù)排序:\n 按個(gè)位、十位、百位排序,不需要比較,只需要對(duì)數(shù)求余然后保存到相應(yīng)下標(biāo)的二維數(shù)組內(nèi),然后依次讀取,每一進(jìn)制重復(fù)依次 ,時(shí)間復(fù)雜度O(d(n+rd)),空間復(fù)雜度O(n+rd),穩(wěn)定排序");
int[] data = { 173, 22, 93, 43, 55, 14, 28, 65, 39, 81, 33, 100 };
System.out.println("初始序列為:");
print(data, 0);//打印
LSD(data, 3);
}
public static void LSD(int[] number, int d) {// d表示最大的數(shù)有多少位
int k = 0;//number的小標(biāo)
int n = 1;//當(dāng)比較十位的時(shí)候 n=10 比較百位的時(shí)候 n=100 用來(lái)吧高位降低方便求余數(shù)
int m = 1;//正在比較number中數(shù)據(jù)的倒數(shù)第幾位
int[][] temp = new int[10][number.length];// 數(shù)組的第一維表示可能的余數(shù)0-9 二維依次記錄該余數(shù)相同的數(shù)
int[] order = new int[10];// 數(shù)組orderp[i]用來(lái)表示該位的余數(shù)是i的數(shù)的個(gè)數(shù)
//排序開始時(shí)間
long start = System.currentTimeMillis();
while (m <= d) {//d=5則比較五次
for (int i = 0; i < number.length; i++) {//把number中的數(shù)按余數(shù)插入到temp中去
int lsd = ((number[i] / n) % 10);//求得該數(shù)的余數(shù)
temp[lsd][order[lsd]] = number[i];//保存到相應(yīng)的地方
order[lsd]++;//該余數(shù)有幾個(gè)
swapCount++;//發(fā)生一次交換
}
for (int i = 0; i < 10; i++) {//將temp中的數(shù)據(jù)按順序提取出來(lái)
if (order[i] != 0)//如果該余數(shù)沒有數(shù)據(jù)則不需要考慮
for (int j = 0; j < order[i]; j++) {//有給余數(shù)的數(shù)一共有多少個(gè)
number[k] = temp[i][j];//一一賦值
k++;
swapCount++;//發(fā)生一次交換
}
order[i] = 0;//置零,以便下一次使用
}
n *= 10;//進(jìn)制+1 往前走
k = 0;//從頭開始
m++;//進(jìn)制+1
print(number, printCount++);
}
//排序結(jié)束時(shí)間
long end = System.currentTimeMillis();
System.out.println("結(jié)果為:");
print(number, 0);//輸出排序結(jié)束的序列
System.out.print("一共發(fā)生了 "+contrastCount+" 次對(duì)比\t");
System.out.print("一共發(fā)生了 "+swapCount+" 次交換\t");
System.out.println("執(zhí)行時(shí)間為"+(end-start)+"毫秒");
}
/**
* 打印已排序好的元素
* @param data 已排序的表
* @param j 第j趟排序
*/
private static void print(int[] data, int j){
if(j != 0)
System.out.print("第 "+j+" 趟:\t");
for (int i = 0; i < data.length; i++) {
System.out.print(data[i] + " ");
}
System.out.println();
}
}
看完上述內(nèi)容,你們對(duì)Java中的排序方法有哪些有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
當(dāng)前題目:Java中的排序方法有哪些
文章分享:http://fisionsoft.com.cn/article/jheigp.html