import java.util.Random;

public class test {
public static void main(String[] args)
{
int[] arr= generatenumbers(10);
show(arr,"before sort:");
quicksort(arr,0,arr.length-1);
show(arr,"after sort:");
}

static void quicksort(int[] arr,int l, int r)
{
if(l>=r) return ;
int i=l;int j=r;
int pivot=arr[i];//pivot
while(i<j)
{
//from right to left find the number less than pivot
while(i<j && arr[j]>=pivot) j--;
if(i<j) arr[i++]=arr[j];
// now the empty position is arra[j]
//from left to right find the number bigger than pivot
while(i<j && arr[i]<=pivot) i++;
if(i<j)arr[j--]=arr[i];
}
//when i==j then the empty position is i/j so we should set the pivot
arr[i]=pivot;
//recursive call the method
quicksort(arr,l,i-1);
quicksort(arr,i+1,r);

}

static int[] generatenumbers(int length)
{
Random r = new Random();
int [] arr = new int[length];
for(int i=0;i<length;i++)
arr[i]=r.nextInt(100);
return arr;
}

static void show(int[] arr,String msg)
{
System.out.println(msg);
for(int i=0;i<arr.length;i++)
System.out.print(arr[i] + "\t");
System.out.println();
}

最新文章

  1. Oracle Sales Cloud:管理沙盒(定制化)小细节2——使用对象触发器更新数字字段
  2. maven 打包
  3. 系统右键菜单添加剪贴板清空项(隐藏DOS窗口)
  4. 优化后的 google提供的汉字转拼音类(针对某些htc等手机的不兼容情况)
  5. SQL SA密码丢失
  6. 简单的form表单
  7. 内部使用final参数的原因
  8. 在VS2012中采用C++中调用DLL中的函数 (4)
  9. 转:移动开发中一些bug及解决方案
  10. Windows I/O模型、同步/异步、阻塞/非阻塞
  11. A shallow summary of oracle log miner
  12. 两两交换链表中的节点(java实现)
  13. luogu3107
  14. 【Java】经典示例代码
  15. JAVA多线程之中断机制(stop()、interrupted()、isInterrupted())
  16. python中的center
  17. ssm中返回中文字符串时出现乱码?
  18. python 正则表达式 -- IP地址验证
  19. 1920*1080分辨率和1080p,1080i的关系
  20. python pip 升级

热门文章

  1. CSS行高line-height的理解
  2. NullPointerException at android.widget.AbsListView.obtainView at android.widget.ListView.makeAndAddView
  3. Java Map按Value排序
  4. webpack学习(入门基础)
  5. 数码管问题(c++实现)
  6. SQL Server Merge语句的使用
  7. css设置height 100%
  8. SharePoint2013 - 移动文档
  9. &ldquo;Stamping&rdquo; PDF Files Downloaded from SharePoint 2010
  10. 使用tinypng优化Android的资源图片