知识大全 快速排序Java实现

Posted 文字

篇首语:忧劳可以兴国,逸豫可以亡身。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 快速排序Java实现相关的知识,希望对你有一定的参考价值。

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

  About this application:

  This application implements Quick Sort algorithm which is described like this:

  Using the Divide and conquer method the Quick Sort first choose a pivot number then divide the numbers which wait for sort into o parts the left part are all smaller than the pivot number while the right part are all bigger than the pivot number Like this then choose a new pivot for the left part and divide it into o parts too Also the left part smaller than the pivot and the right part bigger than the pivot Using this iteration until there is only one number in the divided part At this time your function can return

  Come to one specific sort there should be o pointers one point to the start while another point to the end If number in the start bigger than pivot then put it in behind or else start pointer move towards afterward If number in the end smaller than pivot then put it in before or else end pointer move towards forward

  In order to improve the algorithm you can first put the pivot in a temp and after the numbers have been divided then put back the pivot to the position where start and end position are in Also maybe you can use some strategies to use a better pivot

  Source Code:

  package quick sort;

  import java util ArrayList;

  import java util Scanner;

  /**

  * Sort the numbers using quick sort algorithm If there is any suggestion about

  * this program please email to Author: vivien

  * Date:

  */

  public class QuickSort

  public ArrayList<Double> storeNumbers;

  public static QuickSort quickSort;

  public boolean outputEnable;

  public static void main(String args[])

  System out println( Wele to quick sort! );

  quickSort = new QuickSort();

  quickSort outputEnable = true;

  quickSort inputNumbers();

  

  /**

  * Input the numbers you want to sort

  */

  public void inputNumbers()

  Scanner in = new Scanner(System in);

  String s;

  storeNumbers = new ArrayList<Double>();

  while (true)

  System out

   println( Please input the numbers you need to sort(Note: seperate the numbers using ma ): );

  s = in nextLine();

  // If the input is empty

  if (s trim() isEmpty())

  System out println( The numbers you input are not correct );

  outputEnable = false;

  continue;

   else

  String[] a = s split( );

  // If the length is

  if (a length == )

  System out

   println( The numbers you input are not correct );

  outputEnable = false;

  continue;

  

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

  try

  storeNumbers add(Double valueOf(a[i]));

  outputEnable = true;

   catch (NumberFormatException e)

  System out

   println( The numbers you input are not correct );

  outputEnable = false;

  storeNumbers clear();

  break;

  

  

  

  // Sort the numbers

  sort( storeNumbers size() );

  // Output the results

  output();

  storeNumbers clear();

  

  

  /**

  * Get the pivot of the numbers

  *

  * @param start

  *            The start position of the numbers waiting for sort

  * @param end

  *            The end position of the numbers waiting for sort

  * @return the pivot of the numbers

  */

  public int getPartitionPoint(int start int end)

  return (start + end) >> ; // Get the number in the middle

  

  /**

  * Partition the numbers waiting for sort make the left numbers smaller

  * than the pivot while the right numbers bigger than the pivot

  *

  * @param before

  *            The start position of the numbers waiting for sort

  * @param after

  *            The end position of the numbers waiting for sort

  * @return the position of the pivot

  */

  public int partition(int before int after)

  double temp;

  int PartitionPoint = getPartitionPoint(before after);

  temp = storeNumbers get(PartitionPoint);

  // Put the first number in the position of pivot

  storeNumbers set(PartitionPoint storeNumbers get(before));

  // Compare and exchange until before and after are equal

  while (before != after)

  while ((before != after) && (storeNumbers get(after) >= temp))

  after ;

  

  if (before < after)

  storeNumbers set(before storeNumbers get(after));

  before++;

  

  while ((before != after) && (storeNumbers get(before) <= temp))

  before++;

  

  if (before < after)

  storeNumbers set(after storeNumbers get(before));

  after ;

  

  

  storeNumbers set(before temp);

  return before;

  

  /**

  * Sort the numbers using iteration

  *

  * @param start

  *            The start position of the numbers waiting for sort

  * @param end

  *            The end position of the numbers waiting for sort

  */

  public void sort(int start int end)

  if (end start < )

  return;

  int positionPoint = partition(start end);

  sort(start positionPoint );

  sort(positionPoint + end);

  

  /**

  * Output the results

  */

  public void output()

  if (outputEnable == true)

  System out println( The numbers after sort are: );

  for (int i = ; i < storeNumbers size(); i++)

  System out print(storeNumbers get(i) + );

  

  System out println();

   else

  return;

  

  

  Black box Test Case:

   ) All numbers are zero:

  Please input the numbers you need to sort(Note: seperate the numbers using ma ):

  

  The numbers after sort are:

  

   ) All numbers are integer:

  Please input the numbers you need to sort(Note: seperate the numbers using ma ):

   +

  The numbers after sort are:

  

   ) All number are double:

  Please input the numbers you need to sort(Note: seperate the numbers using ma ):

   + +

  The numbers after sort are:

  

   ) Numbers with characters which are not digit numbers;

  Please input the numbers you need to sort(Note: seperate the numbers using ma ):

  we

  The numbers you input are not correct

  Please input the numbers you need to sort(Note: seperate the numbers using ma ):

   er t

  The numbers you input are not correct

   ) Numbers separated by other signs:

  Please input the numbers you need to sort(Note: seperate the numbers using ma ):

   ; ; ;

  The numbers you input are not correct

   ) Numbers are only mas:

  Please input the numbers you need to sort(Note: seperate the numbers using ma ):

  

  The numbers you input are not correct

  Please input the numbers you need to sort(Note: seperate the numbers using ma ):

  

  The numbers you input are not correct

   ) Input nothing but just a return:

  Please input the numbers you need to sort(Note: seperate the numbers using ma ):

cha138/Article/program/Java/hx/201311/26043

相关参考

知识大全 快速排序的深入详解以及java实现

cha138/Article/program/Java/JSP/201311/20237

知识大全 排序 - 交换排序 - 快速排序 (一)

  快速排序(QuickSort)  算法思想  快速排序是CRAHoare于年提出的一种划分交换排序它采用了一种分治的策略通常称其为分治法(Divideand  ConquerMethod)  ()

知识大全 交换排序之快速排序

快速排序  快速排序(QuickSort)通过一趟排序将待排记录分割成独立的两部分其中一部分记录的关键字均比另一部分记录的关键字小则可分别对这两部分记录继续进行排序以达到整个序列有序快速排序三个步骤 

知识大全 排序 - 交换排序 - 快速排序 (三)

  快速排序执行过程  快速排序执行的全过程可用递归树来描述  >  >  分析  ()递归执行的路线如图中带箭头的包络线所示  ()递归树上每一结点左旁方括号表示当前待排序的区间结点内的关键字是划分

知识大全 排序 - 交换排序 - 快速排序 (四)

  算法分析  快速排序的时间主要耗费在划分操作上对长度为k的区间进行划分共需k次关键字的比较  ()最坏时间复杂度  最坏情况是每次划分选取的基准都是当前无序区中关键字最小(或最大)的记录划分的结果

知识大全 排序 - 交换排序 - 快速排序 (二)

  划分算法Partition  ()简单的划分方法  ①具体做法  第一步(初始化)设置两个指针i和j它们的初值分别为区间的下界和上界即i=lowi=high;选取无序区的第一个记录  R[i](即

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

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

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

  Java代码  插入排序:    packagerututilalgorithmsupport;  importrututilalgorithmSortUtil;  publicclassInse

知识大全 php关联数组排序(快速排序)

  使用环境和条件  有这样一种情况php里面的关联数组如果下面这样的数组数据  [php]  $array=array(  array(  name=>xiao  age=>  )  a

知识大全 Java中的排序

Java中的排序  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Java和库都缺少的一样东西是算