知识大全 java的各种排序算法

Posted

篇首语:寸阳分阴须爱惜,休负春色与时光。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 java的各种排序算法相关的知识,希望对你有一定的参考价值。

  Java代码

   插入排序:

  

   package rut util algorithm support;

   import rut util algorithm SortUtil;

   public class InsertSort implements SortUtil Sort

       /* (non Javadoc)

        * @see rut util algorithm SortUtil Sort#sort(int[])

        */

       public void sort(int[] data)

           int temp;

           for(int i= ;i<data length;i++)

               for(int j=i;(j> )&&(data[j]<data[j ]);j )

                   SortUtil swap(data j j );

              

          

      

  

   冒泡排序:

  

   package rut util algorithm support;

   import rut util algorithm SortUtil;

   public class BubbleSort implements SortUtil Sort

       /* (non Javadoc)

        * @see rut util algorithm SortUtil Sort#sort(int[])

        */

       public void sort(int[] data)

           int temp;

           for(int i= ;i<data length;i++)

               for(int j=data length ;j>i;j )

                   if(data[j]<data[j ])

                       SortUtil swap(data j j );

                  

              

          

      

  

   选择排序:

   package rut util algorithm support;

   import rut util algorithm SortUtil;

   public class SelectionSort implements SortUtil Sort

       /*

        * (non Javadoc)

        *

        * @see rut util algorithm SortUtil Sort#sort(int[])

        */

       public void sort(int[] data)

           int temp;

           for (int i = ; i < data length; i++)

               int lowIndex = i;

               for (int j = data length ; j > i; j )

                   if (data[j] < data[lowIndex])

                       lowIndex = j;

                  

              

               SortUtil swap(data i lowIndex);

          

      

  

   Shell排序:

   package rut util algorithm support;

   import rut util algorithm SortUtil;

   public class ShellSort implements SortUtil Sort

       /* (non Javadoc)

        * @see rut util algorithm SortUtil Sort#sort(int[])

        */

       public void sort(int[] data)

           for(int i=data length/ ;i> ;i/= )

               for(int j= ;j<i;j++)

                   insertSort(data j i);

              

          

           insertSort(data );

      

       /**

        * @param data

        * @param j

        * @param i

        */

       private void insertSort(int[] data int start int inc)

           int temp;

           for(int i=start+inc;i<data length;i+=inc)

               for(int j=i;(j>=inc)&&(data[j]<data[j inc]);j =inc)

                   SortUtil swap(data j j inc);

              

          

      

  

   快速排序:

   package rut util algorithm support;

   import rut util algorithm SortUtil;

   public class QuickSort implements SortUtil Sort

       /* (non Javadoc)

        * @see rut util algorithm SortUtil Sort#sort(int[])

        */

       public void sort(int[] data)

           quickSort(data data length );

      

       private void quickSort(int[] data int i int j)

           int pivotIndex=(i+j)/ ;

           //swap         SortUtil swap(data pivotIndex j);

  

           int k=partition(data i j data[j]);

           SortUtil swap(data k j);

           if((k i)> ) quickSort(data i k );

           if((j k)> ) quickSort(data k+ j);

  

      

       /**

        * @param data

        * @param i

        * @param j

        * @return

        */

       private int partition(int[] data int l int r int pivot)

           do

              while(data[++l]<pivot);

              while((r!= )&&data[ r]>pivot);

              SortUtil swap(data l r);

          

           while(l<r);

           SortUtil swap(data l r);

           return l;

      

  

   改进后的快速排序:

   package rut util algorithm support;

   import rut util algorithm SortUtil;

   public class ImprovedQuickSort implements SortUtil Sort

       private static int MAX_STACK_SIZE= ;

       private static int THRESHOLD= ;

       /* (non Javadoc)

        * @see rut util algorithm SortUtil Sort#sort(int[])

        */

       public void sort(int[] data)

           int[] stack=new int[MAX_STACK_SIZE];

  

           int top= ;

           int pivot;

           int pivotIndex l r;

  

           stack[++top]= ;

           stack[++top]=data length ;

  

           while(top> )

               int j=stack[top ];

               int i=stack[top ];

  

               pivotIndex=(i+j)/ ;

               pivot=data[pivotIndex];

  

               SortUtil swap(data pivotIndex j);

  

               //partition             l=i ;

               r=j;

               do

                   while(data[++l]<pivot);

                   while((r!= )&&(data[ r]>pivot));

                   SortUtil swap(data l r);

              

               while(l<r);

               SortUtil swap(data l r);

               SortUtil swap(data l j);

  

               if((l i)>THRESHOLD)

                   stack[++top]=i;

                   stack[++top]=l ;

              

               if((j l)>THRESHOLD)

                   stack[++top]=l+ ;

                   stack[++top]=j;

              

  

          

           //new InsertSort() sort(data);         insertSort(data);

      

       /**

        * @param data

        */

       private void insertSort(int[] data)

           int temp;

           for(int i= ;i<data length;i++)

               for(int j=i;(j> )&&(data[j]<data[j ]);j )

                   SortUtil swap(data j j );

              

          

      

  

   归并排序:

   package rut util algorithm support;

   import rut util algorithm SortUtil;

   public class MergeSort implements SortUtil Sort

       /* (non Javadoc)

        * @see rut util algorithm SortUtil Sort#sort(int[])

        */

       public void sort(int[] data)

           int[] temp=new int[data length];

           mergeSort(data temp data length );

      

  

       private void mergeSort(int[] data int[] temp int l int r)

           int mid=(l+r)/ ;

           if(l==r) return ;

           mergeSort(data temp l mid);

           mergeSort(data temp mid+ r);

           for(int i=l;i<=r;i++)

               temp[i]=data[i];

          

           int i =l;

           int i =mid+ ;

           for(int cur=l;cur<=r;cur++)

               if(i ==mid+ )

                   data[cur]=temp[i ++];

               else if(i >r)

                   data[cur]=temp[i ++];

               else if(temp[i ]<temp[i ])

                   data[cur]=temp[i ++];

               else

                   data[cur]=temp[i ++];

          

      

  

   改进后的归并排序:

  

   package rut util algorithm support;

   import rut util algorithm SortUtil;

   public class ImprovedMergeSort implements SortUtil Sort

       private static final int THRESHOLD = ;

       /*

        * (non Javadoc)

        *

        * @see rut util algorithm SortUtil Sort#sort(int[])

        */

       public void sort(int[] data)

           int[] temp=new int[data length];

           mergeSort(data temp data length );

      

       private void mergeSort(int[] data int[] temp int l int r)

           int i j k;

           int mid = (l + r) / ;

           if (l == r)

               return;

           if ((mid l) >= THRESHOLD)

               mergeSort(data temp l mid);

           else

               insertSort(data l mid l + );

           if ((r mid) > THRESHOLD)

               mergeSort(data temp mid + r);

           else

               insertSort(data mid + r mid);

           for (i = l; i <= mid; i++)

               temp[i] = data[i];

          

           for (j = ; j <= r mid; j++)

               temp[r j + ] = data[j + mid];

          

           int a = temp[l];

           int b = temp[r];

           for (i = l j = r k = l; k <= r; k++)

               if (a < b)

                   data[k] = temp[i++];

                   a = temp[i];

               else

                   data[k] = temp[j ];

                   b = temp[j];

              

          

      

       /**

        * @param data

        * @param l

        * @param i

        */

       private void insertSort(int[] data int start int len)

           for(int i=start+ ;i<start+len;i++)

               for(int j=i;(j>start) && data[j]<data[j ];j )

                   SortUtil swap(data j j );

              

          

      

  

   堆排序:

   package rut util algorithm support;

   import rut util algorithm SortUtil;

   public class HeapSort implements SortUtil Sort

       /* (non Javadoc)

        * @see rut util algorithm SortUtil Sort#sort(int[])

        */

       public void sort(int[] data)

           MaxHeap h=new MaxHeap();

           h init(data);

           for(int i= ;i<data length;i++)

               h remove();

           System arraycopy(h queue data data length);

      

        private static class MaxHeap

  

  

           void init(int[] data)

               this queue=new int[data length+ ];

               for(int i= ;i<data length;i++)

                   queue[++size]=data[i];

                   fixUp(size);

              

          

  

           private int size= ;

           private int[] queue;

  

           public int get()

               return queue[ ];

          

           public void remove()

               SortUtil swap(queue size );

               fixDown( );

          

           //fixdown         private void fixDown(int k)

               int j;

               while ((j = k << ) <= size)

                   if (j < size && queue[j]<queue[j+ ])

                       j++;

                   if (queue[k]>queue[j]) //不用交换                     break;

                   SortUtil swap(queue j k);

                   k = j;

              

          

           private void fixUp(int k)

               while (k > )

                   int j = k >> ;

                   if (queue[j]>queue[k])

                       break;

                   SortUtil swap(queue j k);

                   k = j;

              

          

      

  

   SortUtil:

   package rut util algorithm;

   import rut util algorithm support BubbleSort;

   import rut util algorithm support HeapSort;

   import rut util algorithm support ImprovedMergeSort;

   import rut util algorithm support ImprovedQuickSort;

   import rut util algorithm support InsertSort;

   import rut util algorithm support MergeSort;

   import rut util algorithm support QuickSort;

   import rut util algorithm support SelectionSort;

   import rut util algorithm support ShellSort;

   public class SortUtil

       public final static int INSERT = ;

       public final static int BUBBLE = ;

       public final static int SELECTION = ;

       public final static int SHELL = ;

       public final static int QUICK = ;

       public final static int IMPROVED_QUICK = ;

       public final static int MERGE = ;

       public final static int IMPROVED_MERGE = ;

       public final static int HEAP = ;

       public static void sort(int[] data)

           sort(data IMPROVED_QUICK);

      

       private static String[] name=

               insert bubble selection shell quick improved_quick merge improved_merge heap

       ;

  

       private static Sort[] impl=new Sort[]

               new InsertSort()

               new BubbleSort()

               new SelectionSort()

               new ShellSort()

               new QuickSort()

               new ImprovedQuickSort()

               new MergeSort()

               new ImprovedMergeSort()

               new HeapSort()

       ;

       public static String toString(int algorithm)

           return name[algorithm ];

      

  

       public static void sort(int[] data int algorithm)

           impl[algorithm ] sort(data);

      

       public static interface Sort

           public void sort(int[] data);

      

       public static void swap(int[] data int i int j)

           int temp = data[i];

           data[i] = data[j];

           data[j] = temp;

      

cha138/Article/program/Java/hx/201311/25756

相关参考

知识大全 常见排序算法的java实现

  最近在面试遇到很多排序算法问题总结一下  定义数组如下  [java]  int[]array=newint[];  int[]array=newint[];  首先是插入排序  [java]  

知识大全 java的排序算法

  插入排序:    packagerututilalgorithmsupport;  importrututilalgorithmSortUtil;  publicclassInsertSortim

知识大全 面试笔试必用-必须掌握的Java排序算法

面试笔试必用-必须掌握的Java排序算法  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!Java排序

知识大全 使用java编写排序算法

   publicclassSort           &

知识大全 关于各种排列组合java算法实现方法

  一利用二进制状态法求排列组合此种方法比较容易懂但是运行效率不高小数据排列组合可以使用复制代码代码如下:importjavautilArrays;  //利用二进制算法进行全排列//count://

知识大全 Shell排序

排序算法(Java实现):Shell排序  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  希尔排序

知识大全 排序 - 各种内部排序方法的比较和选择(二)

  ()在基于比较的排序方法中每次比较两个关键字的大小之后仅仅出现两种可能的转移因此可以用一棵二叉树来描述比较判定  过程  当文件的n个关键字随机分布时任何借助于比较的排序算法至少需要O(nlgn)

知识大全 排序算法的各趟排序算法

  以关键字序列()为例分别写出执行以下排序算法的各趟排序结束时关键字序列的状态  ()直接插入排序()希尔排序()冒泡排序()快速排序  ()直接选择排序()堆排序()归并排序()基数排序  上述方

知识大全 排序算法的各趟排序算法

  以关键字序列()为例分别写出执行以下排序算法的各趟排序结束时关键字序列的状态  ()直接插入排序()希尔排序()冒泡排序()快速排序  ()直接选择排序()堆排序()归并排序()基数排序  上述方

知识大全 第8章排序(算法设计)习题练习

将哨兵放在R[n]中被排序的记录放在R[n]中重写直接插入排序算法以单链表作为存储结构实现直接插入排序算法 设计一算法使得在尽可能少的时间内重排数组将所有取负值的关键字放在所有取非负值的关键