知识大全 java的排序算法

Posted

篇首语:少年乘勇气,百战过乌孙。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 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/26560

相关参考

知识大全 各种排序算法的Java实现

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

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

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

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

   publicclassSort           &

知识大全 Shell排序

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

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

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

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

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

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

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

知识大全 十大排序算法面试题

选择排序选择排序的基本思想是对待排序的记录序列进行n遍的处理第i遍处理是将L[in]中最小者与L[i]交换位置这样经过i遍处理之后前i个记录的位置已经是正确的了选择排序是不稳定的算法复杂度是O(n^)

知识大全 c#与算法--快速排序

  从事net工作两年当初学到的数据结构算法一直没有在实际工作中用到近日闲来无事突发奇想要温习一下简单的数据结构算法今日用了一个下午的时间完成了排序中的快速排序以此作为入驻博客园的首篇随笔!思想向后是