---
title: " 各种排序算法java实现"
author: "water84222"
date: "2008-10-30T17:32:04Z"
tags: []
slug: "java"
draft: false
lastmod: "2008-10-30T17:32:04Z"
id: 260183
url: "https://onlyLove.iteye.com/blog/260183"
---

原文地址 http://blog.csdn.net/lschou520/archive/2008/10/29/3176422.aspx

插入排序:

 

01. package  org.rut.util.algorithm.support;
02. import  org.rut.util.algorithm.SortUtil;
03. public   class  InsertSort  implements  SortUtil.Sort{
04. /* (non-Javadoc)
05. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int\[])
06. \*/
07. public   void  sort( int \[] data) {
08. int  temp;
09. for ( int  i= 1 ;i&lt;data.length;i++){
10. for ( int  j=i;(j&gt; 0 )&&(data\[j]&lt;data\[j- 1 ]);j--){
11. SortUtil.swap(data,j,j- 1 );
12. }
13. }
14. }
15. }

冒泡排序:

 

01. package  org.rut.util.algorithm.support;
02. import  org.rut.util.algorithm.SortUtil;
03. public   class  BubbleSort  implements  SortUtil.Sort{
04. /* (non-Javadoc)
05. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int\[])
06. \*/
07. public   void  sort( int \[] data) {
08. int  temp;
09. for ( int  i= 0 ;i&lt;data.length;i++){
10. for ( int  j=data.length- 1 ;j&gt;i;j--){
11. if (data\[j]&lt;data\[j- 1 ]){
12. SortUtil.swap(data,j,j- 1 );
13. }
14. }
15. }
16. }
17. }

选择排序:

01. package  org.rut.util.algorithm.support;
02. import  org.rut.util.algorithm.SortUtil;
03. public   class  SelectionSort  implements  SortUtil.Sort {
04. /*
05. * (non-Javadoc)
06. \*
07. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int\[])
08. \*/
09. public   void  sort( int \[] data) {
10. int  temp;
11. for  ( int  i =  0 ; i &lt; data.length; i++) {
12. int  lowIndex = i;
13. for  ( int  j = data.length -  1 ; j &gt; i; j--) {
14. if  (data\[j] &lt; data\[lowIndex]) {
15. lowIndex = j;
16. }
17. }
18. SortUtil.swap(data,i,lowIndex);
19. }
20. }
21. }

Shell排序:

01. package  org.rut.util.algorithm.support;
02. import  org.rut.util.algorithm.SortUtil;
03. public   class  ShellSort  implements  SortUtil.Sort{
04. /* (non-Javadoc)
05. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int\[])
06. \*/
07. public   void  sort( int \[] data) {
08. for ( int  i=data.length/ 2 ;i&gt; 2 ;i/= 2 ){
09. for ( int  j= 0 ;j&lt;i;j++){
10. insertSort(data,j,i);
11. }
12. }
13. insertSort(data, 0 , 1 );
14. }
15. /\**
16. * @param data
17. * @param j
18. * @param i
19. \*/
20. private   void  insertSort( int \[] data,  int  start,  int  inc) {
21. int  temp;
22. for ( int  i=start+inc;i&lt;data.length;i+=inc){
23. for ( int  j=i;(j&gt;=inc)&&(data\[j]&lt;data\[j-inc]);j-=inc){
24. SortUtil.swap(data,j,j-inc);
25. }
26. }
27. }
28. }

快速排序:

01. package  org.rut.util.algorithm.support;
02. import  org.rut.util.algorithm.SortUtil;
03. public   class  QuickSort  implements  SortUtil.Sort{
04. /* (non-Javadoc)
05. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int\[])
06. \*/
07. public   void  sort( int \[] data) {
08. quickSort(data, 0 ,data.length- 1 );
09. }
10. private   void  quickSort( int \[] data, int  i, int  j){
11. int  pivotIndex=(i+j)/ 2 ;
12. //swap
13. SortUtil.swap(data,pivotIndex,j);
14. int  k=partition(data,i- 1 ,j,data\[j]);
15. SortUtil.swap(data,k,j);
16. if ((k-i)&gt; 1 ) quickSort(data,i,k- 1 );
17. if ((j-k)&gt; 1 ) quickSort(data,k+ 1 ,j);
18. }
19. /\**
20. * @param data
21. * @param i
22. * @param j
23. * @return
24. \*/
25. private   int  partition( int \[] data,  int  l,  int  r, int  pivot) {
26. do {
27. while (data\[++l]&lt;pivot);
28. while ((r!= 0 )&&data\[--r]&gt;pivot);
29. SortUtil.swap(data,l,r);
30. }
31. while (l&lt;r);
32. SortUtil.swap(data,l,r);
33. return  l;
34. }
35. }

改进后的快速排序:

01. package  org.rut.util.algorithm.support;
02. import  org.rut.util.algorithm.SortUtil;
03. public   class  ImprovedQuickSort  implements  SortUtil.Sort {
04. private   static   int  MAX\_STACK\_SIZE= 4096 ;
05. private   static   int  THRESHOLD= 10 ;
06. /* (non-Javadoc)
07. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int\[])
08. \*/
09. public   void  sort( int \[] data) {
10. int \[] stack= new   int \[MAX\_STACK\_SIZE];
11. int  top=- 1 ;
12. int  pivot;
13. int  pivotIndex,l,r;
14. stack\[++top]= 0 ;
15. stack\[++top]=data.length- 1 ;
16. while (top&gt; 0 ){
17. int  j=stack\[top--];
18. int  i=stack\[top--];
19. pivotIndex=(i+j)/ 2 ;
20. pivot=data\[pivotIndex];
21. SortUtil.swap(data,pivotIndex,j);
22. //partition
23. l=i- 1 ;
24. r=j;
25. do {
26. while (data\[++l]&lt;pivot);
27. while ((r!= 0 )&&(data\[--r]&gt;pivot));
28. SortUtil.swap(data,l,r);
29. }
30. while (l&lt;r);
31. SortUtil.swap(data,l,r);
32. SortUtil.swap(data,l,j);
33. if ((l-i)&gt;THRESHOLD){
34. stack\[++top]=i;
35. stack\[++top]=l- 1 ;
36. }
37. if ((j-l)&gt;THRESHOLD){
38. stack\[++top]=l+ 1 ;
39. stack\[++top]=j;
40. }
41. }
42. //new InsertSort().sort(data);
43. insertSort(data);
44. }
45. /\**
46. * @param data
47. \*/
48. private   void  insertSort( int \[] data) {
49. int  temp;
50. for ( int  i= 1 ;i&lt;data.length;i++){
51. for ( int  j=i;(j&gt; 0 )&&(data\[j]&lt;data\[j- 1 ]);j--){
52. SortUtil.swap(data,j,j- 1 );
53. }
54. }
55. }
56. }

归并排序:

01. package  org.rut.util.algorithm.support;
02. import  org.rut.util.algorithm.SortUtil;
03. public   class  MergeSort  implements  SortUtil.Sort{
04. /* (non-Javadoc)
05. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int\[])
06. \*/
07. public   void  sort( int \[] data) {
08. int \[] temp= new   int \[data.length];
09. mergeSort(data,temp, 0 ,data.length- 1 );
10. }
11. private   void  mergeSort( int \[] data, int \[] temp, int  l, int  r){
12. int  mid=(l+r)/ 2 ;
13. if (l==r)  return  ;
14. mergeSort(data,temp,l,mid);
15. mergeSort(data,temp,mid+ 1 ,r);
16. for ( int  i=l;i&lt;=r;i++){
17. temp\[i]=data\[i];
18. }
19. int  i1=l;
20. int  i2=mid+ 1 ;
21. for ( int  cur=l;cur&lt;=r;cur++){
22. if (i1==mid+ 1 )
23. data\[cur]=temp\[i2++];
24. else   if (i2&gt;r)
25. data\[cur]=temp\[i1++];
26. else   if (temp\[i1]&lt;temp\[i2])
27. data\[cur]=temp\[i1++];
28. else
29. data\[cur]=temp\[i2++];
30. }
31. }
32. }

改进后的归并排序:

 

01. package  org.rut.util.algorithm.support;
02. import  org.rut.util.algorithm.SortUtil;
03. public   class  ImprovedMergeSort  implements  SortUtil.Sort {
04. private   static   final   int  THRESHOLD =  10 ;
05. /*
06. * (non-Javadoc)
07. \*
08. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int\[])
09. \*/
10. public   void  sort( int \[] data) {
11. int \[] temp= new   int \[data.length];
12. mergeSort(data,temp, 0 ,data.length- 1 );
13. }
14. private   void  mergeSort( int \[] data,  int \[] temp,  int  l,  int  r) {
15. int  i, j, k;
16. int  mid = (l + r) /  2 ;
17. if  (l == r)
18. return ;
19. if  ((mid - l) &gt;= THRESHOLD)
20. mergeSort(data, temp, l, mid);
21. else
22. insertSort(data, l, mid - l +  1 );
23. if  ((r - mid) &gt; THRESHOLD)
24. mergeSort(data, temp, mid +  1 , r);
25. else
26. insertSort(data, mid +  1 , r - mid);
27. for  (i = l; i &lt;= mid; i++) {
28. temp\[i] = data\[i];
29. }
30. for  (j =  1 ; j &lt;= r - mid; j++) {
31. temp\[r - j +  1 ] = data\[j + mid];
32. }
33. int  a = temp\[l];
34. int  b = temp\[r];
35. for  (i = l, j = r, k = l; k &lt;= r; k++) {
36. if  (a &lt; b) {
37. data\[k] = temp\[i++];
38. a = temp\[i];
39. }  else  {
40. data\[k] = temp\[j--];
41. b = temp\[j];
42. }
43. }
44. }
45. /\**
46. * @param data
47. * @param l
48. * @param i
49. \*/
50. private   void  insertSort( int \[] data,  int  start,  int  len) {
51. for ( int  i=start+ 1 ;i&lt;start+len;i++){
52. for ( int  j=i;(j&gt;start) && data\[j]&lt;data\[j- 1 ];j--){
53. SortUtil.swap(data,j,j- 1 );
54. }
55. }
56. }
57. }

堆排序:

01. package  org.rut.util.algorithm.support;
02. import  org.rut.util.algorithm.SortUtil;
03. public   class  HeapSort  implements  SortUtil.Sort{
04. /* (non-Javadoc)
05. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int\[])
06. \*/
07. public   void  sort( int \[] data) {
08. MaxHeap h= new  MaxHeap();
09. h.init(data);
10. for ( int  i= 0 ;i&lt;data.length;i++)
11. h.remove();
12. System.arraycopy(h.queue, 1 ,data, 0 ,data.length);
13. }
14. private   static   class  MaxHeap{
15. void  init( int \[] data){
16. this .queue= new   int \[data.length+ 1 ];
17. for ( int  i= 0 ;i&lt;data.length;i++){
18. queue\[++size]=data\[i];
19. fixUp(size);
20. }
21. }
22. private   int  size= 0 ;
23. private   int \[] queue;
24. public   int  get() {
25. return  queue\[ 1 ];
26. }
27. public   void  remove() {
28. SortUtil.swap(queue, 1 ,size--);
29. fixDown( 1 );
30. }
31. //fixdown
32. private   void  fixDown( int  k) {
33. int  j;
34. while  ((j = k &lt;&lt;  1 ) &lt;= size) {
35. if  (j &lt; size && queue\[j]&lt;queue\[j+ 1 ])
36. j++;
37. if  (queue\[k]&gt;queue\[j])  //不用交换
38. break ;
39. SortUtil.swap(queue,j,k);
40. k = j;
41. }
42. }
43. private   void  fixUp( int  k) {
44. while  (k &gt;  1 ) {
45. int  j = k &gt;&gt;  1 ;
46. if  (queue\[j]&gt;queue\[k])
47. break ;
48. SortUtil.swap(queue,j,k);
49. k = j;
50. }
51. }
52. }
53. }

SortUtil:

01. package  org.rut.util.algorithm;
02. import  org.rut.util.algorithm.support.BubbleSort;
03. import  org.rut.util.algorithm.support.HeapSort;
04. import  org.rut.util.algorithm.support.ImprovedMergeSort;
05. import  org.rut.util.algorithm.support.ImprovedQuickSort;
06. import  org.rut.util.algorithm.support.InsertSort;
07. import  org.rut.util.algorithm.support.MergeSort;
08. import  org.rut.util.algorithm.support.QuickSort;
09. import  org.rut.util.algorithm.support.SelectionSort;
10. import  org.rut.util.algorithm.support.ShellSort;
11. public   class  SortUtil {
12. public   final   static   int  INSERT =  1 ;
13. public   final   static   int  BUBBLE =  2 ;
14. public   final   static   int  SELECTION =  3 ;
15. public   final   static   int  SHELL =  4 ;
16. public   final   static   int  QUICK =  5 ;
17. public   final   static   int  IMPROVED\_QUICK =  6 ;
18. public   final   static   int  MERGE =  7 ;
19. public   final   static   int  IMPROVED\_MERGE =  8 ;
20. public   final   static   int  HEAP =  9 ;
21. public   static   void  sort( int \[] data) {
22. sort(data, IMPROVED\_QUICK);
23. }
24. private   static  String\[] name={
25. "insert" , "bubble" , "selection" , "shell" , "quick" , "improved\_quick" , "merge" , "improved\_merge" , "heap"
26. };
27. private   static  Sort\[] impl= new  Sort\[]{
28. new  InsertSort(),
29. new  BubbleSort(),
30. new  SelectionSort(),
31. new  ShellSort(),
32. new  QuickSort(),
33. new  ImprovedQuickSort(),
34. new  MergeSort(),
35. new  ImprovedMergeSort(),
36. new  HeapSort()
37. };
38. public   static  String toString( int  algorithm){
39. return  name\[algorithm- 1 ];
40. }
41. public   static   void  sort( int \[] data,  int  algorithm) {
42. impl\[algorithm- 1 ].sort(data);
43. }
44. public   static   interface  Sort {
45. public   void  sort( int \[] data);
46. }
47. public   static   void  swap( int \[] data,  int  i,  int  j) {
48. int  temp = data\[i];
49. data\[i] = data\[j];
50. data\[j] = temp;
51. }
52. }