【数据结构】快速排序算法(Java实现)
2017-07-28 00:00
162 查看
![](https://static.oschina.net/uploads/space/2017/0728/212752_YqG9_3030978.png)
package com.paixu; class shuzu1 { int length; int r[] = new int[100]; // 创建数组 public void create(int a[]) { for (int i = 0; i <= length; i++) { r[i] = a[i]; } } // 展示数组 public void display() { System.out.println("这里是展示数组:"); for (int i = 1; i <= length; i++) { System.out.print(r[i] + " "); } System.out.println(); System.out.println(); } public void swap(int low,int high){ int temp; temp = r[low]; r[low] = r[high]; r[high] = temp; System.out.println("此时位置"+low+"和位置"+high+"交换"); display(); } //快速排序 public void Qsort(int low,int high){ int pivot; if (low<high){ pivot = Partition(low, high); System.out.println("此时pivot为:"+pivot); Qsort(low, pivot-1); Qsort(pivot+1,high); } } int Partition(int low,int high){ int pivotkey = r[low]; while (low<high){ while(low<high&&r[high]>=pivotkey){ high--; } swap(low,high); while (low<high&&r[low]<=pivotkey){ low++; } swap(low,high); } System.out.println("-------------------"); return low; } } public class kuaipai { public static void main(String[] args) { // TODO Auto-generated method stub shuzu1 sz = new shuzu1(); sz.length = 10; int a[] = { 0, 5, 8, 9, 7, 22, 20, 16, 200, 65, 98 }; sz.create(a); sz.display(); sz.Qsort(1, 10); sz.display(); } }
相关文章推荐
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)
- 数据结构之优先队列--二叉堆(Java实现)
- (Java)单链表Java语言链式结构实现(数据结构四)
- Json树形结构数据转Java对象并存储到数据库的实现-超简单的JSON复杂数据处理 .
- java实现tree型的数据结构
- 优先队列的实现 Java数据结构与算法
- 数据结构之哈希表的java实现
- java 二叉树 实现 数据结构 笔试
- Java单链表顺序和链式实现(数据结构五)
- 数据结构书中基于整数的简单排序Java实现,巩固一下基础
- Java5 下实现锁无关数据结构
- 数据结构之图的Java实现
- 数据结构——栈—— 顺序栈(附java实现)
- 数据结构——快速排序原理及算法Java实现
- 数据结构之队列的Java实现
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)
- 数据结构之队列的java实现
- 数据结构—顺序表(自己实现Java的ArrayList)
- Java 下实现锁无关数据结构