62,625
社区成员
发帖
与我相关
我的任务
分享package test;
import java.util.Arrays;
public class SortTest {
public static void main(String[] args) {
// int[] num = {5, 2, 8, 3, 7, 1, 9, 4, 0, 6};
int[] num = {5, 2, 8, 3, 7, 1, 9, 4};
System.out.println("排序前: " + Arrays.toString(num));
num = QuickSort(num, 0, 7);
System.out.println("排序后:" + Arrays.toString(num));
}
// 快速排序
private static int[] QuickSort(int[] num, int low, int high) {
int i = low, j = high;
int temp = num[i];
if (low < high) {
while (i < j) {
while ((num[j] >= temp) && (i < j)) {
j--;
}
num[i] = num[j];
while ((num[i] <= temp) && (i < j)) {
i++;
}
num[j] = num[i];
}
num[i] = temp;
QuickSort(num, low, i );
QuickSort(num, j + 1, high);
}
return num;
}
}

public class TestPaiXu {
public static void main(String[] args) {
int[] array = {3,2,6,1,5,9,8,-2};
zhijie(array);
maopao(array);
}
//直接排序
public static void zhijie(int[] array){
for(int i=0;i<array.length-1;i++){
int temp;
for(int j = i+1;j<array.length;j++){
if(array[i]>array[j]){
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
for(int a : array){
System.out.println(a);
}
}
//冒泡排序
public static void maopao(int[] array){
for(int i=0;i<array.length-1;i++){
int temp;
for(int j = 0;j<array.length-1-i;j++){
if(array[j]>array[j+1]){
temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
}
}
}
for(int a : array){
System.out.println(a);
}
}
}
public class quickSort {
inta[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
public quickSort(){
quick(a);
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
publicint getMiddle(int[] list, int low, int high) {
int tmp = list[low]; //数组的第一个作为中轴
while (low < high) {
while (low < high && list[high] >= tmp) {
high--;
}
list[low] = list[high]; //比中轴小的记录移到低端
while (low < high && list[low] <= tmp) {
low++;
}
list[high] = list[low]; //比中轴大的记录移到高端
}
list[low] = tmp; //中轴记录到尾
return low; //返回中轴的位置
}
publicvoid _quickSort(int[] list, int low, int high) {
if (low < high) {
int middle = getMiddle(list, low, high); //将list数组进行一分为二
_quickSort(list, low, middle - 1); //对低字表进行递归排序
_quickSort(list, middle + 1, high); //对高字表进行递归排序
}
}
publicvoid quick(int[] a2) {
if (a2.length > 0) { //查看数组是否为空
_quickSort(a2, 0, a2.length - 1);
}
}
}