ITeye博客

各种排序算法java实现

water84222 · 2008-10-30 17:32 · javaついて · Markdown

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

插入排序:

 

  1. package  org.rut.util.algorithm.support;
  2. import  org.rut.util.algorithm.SortUtil;
  3. public   class  InsertSort  implements  SortUtil.Sort{
  4. /* (non-Javadoc)
  5. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  6. */
  7. public   void  sort( int [] data) {
  8. int  temp;
  9. for ( int  i= 1 ;i<data.length;i++){
  10. for ( int  j=i;(j> 0 )&&(data[j]<data[j- 1 ]);j--){
  11. SortUtil.swap(data,j,j- 1 );
  12. }
  13. }
  14. }
  15. }

冒泡排序:

 

  1. package  org.rut.util.algorithm.support;
  2. import  org.rut.util.algorithm.SortUtil;
  3. public   class  BubbleSort  implements  SortUtil.Sort{
  4. /* (non-Javadoc)
  5. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  6. */
  7. public   void  sort( int [] data) {
  8. int  temp;
  9. for ( int  i= 0 ;i<data.length;i++){
  10. for ( int  j=data.length- 1 ;j>i;j--){
  11. if (data[j]<data[j- 1 ]){
  12. SortUtil.swap(data,j,j- 1 );
  13. }
  14. }
  15. }
  16. }
  17. }

选择排序:

  1. package  org.rut.util.algorithm.support;
  2. import  org.rut.util.algorithm.SortUtil;
  3. public   class  SelectionSort  implements  SortUtil.Sort {
  4. /*
  5. * (non-Javadoc)
  6. *
  7. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  8. */
  9. public   void  sort( int [] data) {
  10. int  temp;
  11. for  ( int  i =  0 ; i < data.length; i++) {
  12. int  lowIndex = i;
  13. for  ( int  j = data.length -  1 ; j > i; j--) {
  14. if  (data[j] < data[lowIndex]) {
  15. lowIndex = j;
  16. }
  17. }
  18. SortUtil.swap(data,i,lowIndex);
  19. }
  20. }
  21. }

Shell排序:

  1. package  org.rut.util.algorithm.support;
  2. import  org.rut.util.algorithm.SortUtil;
  3. public   class  ShellSort  implements  SortUtil.Sort{
  4. /* (non-Javadoc)
  5. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  6. */
  7. public   void  sort( int [] data) {
  8. for ( int  i=data.length/ 2 ;i> 2 ;i/= 2 ){
  9. for ( int  j= 0 ;j<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<data.length;i+=inc){
  23. for ( int  j=i;(j>=inc)&&(data[j]<data[j-inc]);j-=inc){
  24. SortUtil.swap(data,j,j-inc);
  25. }
  26. }
  27. }
  28. }

快速排序:

  1. package  org.rut.util.algorithm.support;
  2. import  org.rut.util.algorithm.SortUtil;
  3. public   class  QuickSort  implements  SortUtil.Sort{
  4. /* (non-Javadoc)
  5. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  6. */
  7. public   void  sort( int [] data) {
  8. quickSort(data, 0 ,data.length- 1 );
  9. }
  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)> 1 ) quickSort(data,i,k- 1 );
  17. if ((j-k)> 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]<pivot);
  28. while ((r!= 0 )&&data[--r]>pivot);
  29. SortUtil.swap(data,l,r);
  30. }
  31. while (l<r);
  32. SortUtil.swap(data,l,r);
  33. return  l;
  34. }
  35. }

改进后的快速排序:

  1. package  org.rut.util.algorithm.support;
  2. import  org.rut.util.algorithm.SortUtil;
  3. public   class  ImprovedQuickSort  implements  SortUtil.Sort {
  4. private   static   int  MAX_STACK_SIZE= 4096 ;
  5. private   static   int  THRESHOLD= 10 ;
  6. /* (non-Javadoc)
  7. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  8. */
  9. 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> 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]<pivot);
  27. while ((r!= 0 )&&(data[--r]>pivot));
  28. SortUtil.swap(data,l,r);
  29. }
  30. while (l<r);
  31. SortUtil.swap(data,l,r);
  32. SortUtil.swap(data,l,j);
  33. if ((l-i)>THRESHOLD){
  34. stack[++top]=i;
  35. stack[++top]=l- 1 ;
  36. }
  37. if ((j-l)>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<data.length;i++){
  51. for ( int  j=i;(j> 0 )&&(data[j]<data[j- 1 ]);j--){
  52. SortUtil.swap(data,j,j- 1 );
  53. }
  54. }
  55. }
  56. }

归并排序:

  1. package  org.rut.util.algorithm.support;
  2. import  org.rut.util.algorithm.SortUtil;
  3. public   class  MergeSort  implements  SortUtil.Sort{
  4. /* (non-Javadoc)
  5. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  6. */
  7. public   void  sort( int [] data) {
  8. int [] temp= new   int [data.length];
  9. 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<=r;i++){
  17. temp[i]=data[i];
  18. }
  19. int  i1=l;
  20. int  i2=mid+ 1 ;
  21. for ( int  cur=l;cur<=r;cur++){
  22. if (i1==mid+ 1 )
  23. data[cur]=temp[i2++];
  24. else   if (i2>r)
  25. data[cur]=temp[i1++];
  26. else   if (temp[i1]<temp[i2])
  27. data[cur]=temp[i1++];
  28. else
  29. data[cur]=temp[i2++];
  30. }
  31. }
  32. }

改进后的归并排序:

 

  1. package  org.rut.util.algorithm.support;
  2. import  org.rut.util.algorithm.SortUtil;
  3. public   class  ImprovedMergeSort  implements  SortUtil.Sort {
  4. private   static   final   int  THRESHOLD =  10 ;
  5. /*
  6. * (non-Javadoc)
  7. *
  8. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  9. */
  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) >= THRESHOLD)
  20. mergeSort(data, temp, l, mid);
  21. else
  22. insertSort(data, l, mid - l +  1 );
  23. if  ((r - mid) > THRESHOLD)
  24. mergeSort(data, temp, mid +  1 , r);
  25. else
  26. insertSort(data, mid +  1 , r - mid);
  27. for  (i = l; i <= mid; i++) {
  28. temp[i] = data[i];
  29. }
  30. for  (j =  1 ; j <= 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 <= r; k++) {
  36. if  (a < 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<start+len;i++){
  52. for ( int  j=i;(j>start) && data[j]<data[j- 1 ];j--){
  53. SortUtil.swap(data,j,j- 1 );
  54. }
  55. }
  56. }
  57. }

堆排序:

  1. package  org.rut.util.algorithm.support;
  2. import  org.rut.util.algorithm.SortUtil;
  3. public   class  HeapSort  implements  SortUtil.Sort{
  4. /* (non-Javadoc)
  5. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  6. */
  7. public   void  sort( int [] data) {
  8. MaxHeap h= new  MaxHeap();
  9. h.init(data);
  10. for ( int  i= 0 ;i<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<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 <<  1 ) <= size) {
  35. if  (j < size && queue[j]<queue[j+ 1 ])
  36. j++;
  37. if  (queue[k]>queue[j])  //不用交换
  38. break ;
  39. SortUtil.swap(queue,j,k);
  40. k = j;
  41. }
  42. }
  43. private   void  fixUp( int  k) {
  44. while  (k >  1 ) {
  45. int  j = k >>  1 ;
  46. if  (queue[j]>queue[k])
  47. break ;
  48. SortUtil.swap(queue,j,k);
  49. k = j;
  50. }
  51. }
  52. }
  53. }

SortUtil:

  1. package  org.rut.util.algorithm;
  2. import  org.rut.util.algorithm.support.BubbleSort;
  3. import  org.rut.util.algorithm.support.HeapSort;
  4. import  org.rut.util.algorithm.support.ImprovedMergeSort;
  5. import  org.rut.util.algorithm.support.ImprovedQuickSort;
  6. import  org.rut.util.algorithm.support.InsertSort;
  7. import  org.rut.util.algorithm.support.MergeSort;
  8. import  org.rut.util.algorithm.support.QuickSort;
  9. 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. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。