Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 1 Fork 0

Freeman/Java语言程序设计 编程练习

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
Sort.java 5.64 KB
Copy Edit Raw Blame History
Freeman authored 2020年11月19日 10:40 +08:00 . 并行快速排序优化
package lib;
import java.util.concurrent.*;
public class Sort {
private int[] intArray = null;
public Sort(int[] array) {
if (array == null) throw new NullPointerException();
intArray = array;
}
//插入排序
public static void insertionSort(int arr[]) {
insertionSort(arr, 0, arr.length);
}
//对数组的一部分进行插入排序
//函数处理数组的[left,right-1]部分
private static void insertionSort(int[] arr, int left, int right) {
if (right <= left) throw new IllegalArgumentException("Right index less than or equals to left index.");
int i, j;
for (i = left + 1; i <= right - 1; i++) {
int key = arr[i];
for (j = i - 1; j >= 0; j--) {
if (arr[j] > key) arr[j + 1] = arr[j];
else break;
}
arr[j + 1] = key;
}
}
//逆向插入排序
public static void reverseInsertionSort(int arr[]) {
int i, j;
for (i = arr.length - 2; i >= 0; i--) {
int key = arr[i];
for (j = i + 1; j <= arr.length - 1; j++) {
if (arr[j] > key) arr[j - 1] = arr[j];
else break;
}
arr[j - 1] = key;
}
}
//快速排序
//函数处理数组的[left,right-1]部分
private static void qsort(int[] arr, int left, int right) {
if (right <= left) throw new IllegalArgumentException("Right index less than or equals to left index.");
if (right - left <= 20) {
insertionSort(arr);
return;
}
int low = left;
int high = right - 1;
int medium = (low + high) / 2;
// Medium-of-Three方法选择枢轴
//目标: arr[medium] <= arr[high]
if (arr[medium] > arr[high]) {
swap(arr, medium, high);
}
//目标: arr[low] <= arr[high]
if (arr[low] > arr[high]) {
swap(arr, low, high);
}
//目标: arr[low] >= arr[medium]
if (arr[medium] > arr[low]) {
swap(arr, medium, low);
}
//分割数组
int pivot = arr[low];
// while (low != high) {
// while (low != high) {
// if (arr[high] >= pivot) high--;
// else {
// arr[low] = arr[high];
// break;
// }
// }
// while (low != high) {
// if (arr[low] <= pivot) low++;
// else {
// arr[high] = arr[low];
// break;
// }
// }
// }
while (true) {
while (low != high && arr[high] >= pivot) high--;
if (low != high) arr[low] = arr[high];
else break;
while (low != high && arr[low] <= pivot) low++;
if (low != high) arr[high] = arr[low];
else break;
}
int pivotIndex = low;
arr[pivotIndex] = pivot;
//递归
qsort(arr, left, pivotIndex);
qsort(arr, pivotIndex + 1, right);
}
//快速排序
//调用qsort(int[] arr, int left, int right)
public static void qsort(int[] arr) {
qsort(arr, 0, arr.length);
}
//并行快速排序
//基于ForkJoin框架
public void parallelQSort() {
ForkJoinPool pool = new ForkJoinPool();
ParallelQSortTask masterTask = new ParallelQSortTask(0, intArray.length);
pool.invoke(masterTask);
}
//交换数组中两个元素的位置
private static void swap(int[] arr, int indexA, int indexB) {
int tmp = arr[indexA];
arr[indexA] = arr[indexB];
arr[indexB] = tmp;
}
//用于并行快速排序的内部类
class ParallelQSortTask extends RecursiveAction {
private final int left;
private final int right;
public ParallelQSortTask(int left, int right) {
if (right <= left) throw new IllegalArgumentException("Right index less than or equals to left index.");
this.left = left;
this.right = right;
}
public void compute() {
//问题规模小,串行解决
if (right - left <= 20) {
insertionSort(intArray, left, right);
return;
}
int low = left;
int high = right - 1;
int medium = (low + high) / 2;
// Medium-of-Three方法选择枢轴
//目标: arr[medium] <= arr[high]
if (intArray[medium] > intArray[high]) {
swap(intArray, medium, high);
}
//目标: arr[low] <= arr[high]
if (intArray[low] > intArray[high]) {
swap(intArray, low, high);
}
//目标: arr[low] >= arr[medium]
if (intArray[medium] > intArray[low]) {
swap(intArray, medium, low);
}
//分割数组
int pivot = intArray[low];
while (true) {
while (low != high && intArray[high] >= pivot) high--;
if (low != high) intArray[low] = intArray[high];
else break;
while (low != high && intArray[low] <= pivot) low++;
if (low != high) intArray[high] = intArray[low];
else break;
}
int pivotIndex = low;
intArray[pivotIndex] = pivot;
ParallelQSortTask leftTask = new ParallelQSortTask(left, pivotIndex);
ParallelQSortTask rightTask = new ParallelQSortTask(pivotIndex + 1, right);
leftTask.invoke();
rightTask.invoke();
}
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

教材《Java语言程序设计(原书第10版)》(梁勇,机械工业出版社)上部分编程练习题的解答。
No labels
LGPL-2.1
Use LGPL-2.1
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/freeman449/Introduction-to-Java-Programming-Exercises.git
git@gitee.com:freeman449/Introduction-to-Java-Programming-Exercises.git
freeman449
Introduction-to-Java-Programming-Exercises
Java语言程序设计 编程练习
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /