---
title: "几种稳定排序算法"
author: "晴空之羽"
date: "2014-04-25T17:49:34Z"
tags: ["Java", "算法"]
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2014-04-25T17:49:34Z"
id: 2054866
url: "https://820199753.iteye.com/blog/2054866"
---

对Java中的一些稳定算法进行了整理，使其符合自己的编程习惯。 \[code="java"] package com.study.sort; import java.util.LinkedList; public class StableSort { /\** * 生成一个随机数组 * @param length 数组长度 * @return 生成的数组 \*/ public static int\[] init(int length){ int \[] before=new int\[length]; Random rand=new Random(47); for(int i=0;i=0&&key0;i--){ for(int j=0;jbefore\[j+1]){ int temp=before\[j+1]; before\[j+1]=before\[j]; before\[j]=temp; } } for(int j=(before.length-1-(i+1));j&gt;i;j--){ if(before\[j] temp = new LinkedList(); int indexA=lowerBound; int indexB=mid; while(indexA&lt;=(mid-1)&&indexB&lt;=upperBound){ if(before\[indexA]0){ before\[lowerBound+index]=temp.poll(); index++; } } /\** * 打印数组内容到控制台 * @param before 待显示数组 \*/ public static void prt(int\[] before){ for(int x:before){ System.out.print(x+" "); } System.out.println(""); } public static void main(String\[] args){ int\[] before=init(10); prt(before); // insertSort(before); // bubbleSort(before); // cocktailSort(before); mergeSort(before); prt(before); } } \[/code]