快排中partition函数-java
来源:互联网 发布:飞机升阻比知乎 编辑:程序博客网 时间:2024/05/22 03:18
快排中partition函数非常有用, 其两种实现很难在java这种有jvm干预的情况下测出哪个更优秀, 不知道是不是测试手段比较low? 总的来说, partition2交换的次数少一下. 对算法的理解也没到化入骨髓的地步, 先备注一下. 要是有大神指点就好了.
package com.test;import java.util.Random;public class PartitionHolder { static int partition1(int[] arr, int start, int end, int pivotIndex) { int pivot = arr[pivotIndex]; swap(arr, pivotIndex, end); int storeIndex = start; for (int i = start; i < end; ++i) { if(arr[i] < pivot) { swap(arr, i, storeIndex); ++storeIndex; } } swap(arr, storeIndex, end); return storeIndex; } static int partition2(int[] arr, int start, int end, int pivotIndex) { int pivot = arr[pivotIndex]; swap(arr, pivotIndex, end); int i = start, j = end; while (i < j) { while(i < j && arr[i] <= pivot) ++i; while(i < j && arr[j] >= pivot) --j; if(i < j) swap(arr, i, j); } swap(arr, i, end); return i; } static void qsort1(int[] arr, int start, int end) { if (start < end) { int p = partition1(arr, start, end, end); qsort1(arr, start, p - 1); qsort1(arr, p + 1, end); } } static void qsort2(int[] arr, int start, int end) { if (start < end) { int p = partition2(arr, start, end, end); qsort2(arr, start, p - 1); qsort2(arr, p + 1, end); } } static void swap(int[] arr, int from, int to) { int temp = arr[from]; arr[from] = arr[to]; arr[to] = temp; ++count; } public static void main(String[] args) {// int[] arr = new int[] {7, 9, 4, 6, 10, 2, 8, 5, 1};// int[] arr = new int[] {8, 1, 3, 9, 2, 4, 1, 9, 8, 7, 5}; Random random = new Random(); int[] arr = createRandomArray(random, 100); for (int i = 0; i < 50; ++i) { int[] copy = new int[100]; System.arraycopy(arr, 0, copy, 0, 100); count = 0; start = System.nanoTime(); qsort1(copy, 0, copy.length - 1); System.out.println("count: " + count + ", useTime: " + (System.nanoTime() - start)); } for (int i = 0; i < 50; ++i) { int[] copy = new int[100]; System.arraycopy(arr, 0, copy, 0, 100); count = 0; start = System.nanoTime(); qsort2(copy, 0, copy.length - 1); System.out.println("count: " + count + ", useTime: " + (System.nanoTime() - start)); } } private static int count = 0; private static long start = 0L; private static int[] createRandomArray(Random random, int size) { int[] arr = new int[size]; for (int i = 0; i < size; ++i) { arr[i] = random.nextInt(size); } return arr; }}
0 0
- 快排中partition函数-java
- 快排中partition函数的优化
- partition函数
- Partition函数
- partition函数
- Partition函数
- 分析函数partition by
- partition函数的实现
- over partition by 函数
- 基本算法-partition函数
- [Leetcode] Partition List (Java)
- Partition List (Java)
- [LeetCode][Java] Partition List
- leetcode:Partition List 【Java】
- LeetCode-JAVA-Partition List
- Partition List leetcode java
- partition-list Java code
- Python的partition字符串函数
- HDU 4722 Good Numbers(找规律)
- 自己总结的一些常数据结构基础知识
- 23个MySQL常用查询语句
- poj 3468 线段树延迟标记
- 使用UIAppearance协议自定义视图
- 快排中partition函数-java
- WebView简单使用方法
- C++ primer 读书笔记(第4章)表达式
- 约瑟夫问题(单向循环链表应用实例)
- 数字图像处理领域的二十四个典型算法
- jQuery事件核心 - 绑定设计(一)
- winform中datagridview中的某一列visible的设置
- SQL查询返回数据多列,保存在list中时,怎么取值
- C++ socket通信,c/s模式,解决了在本机能通信,外网无法通信的问题