快速排序
来源:互联网 发布:更改防火墙端口 编辑:程序博客网 时间:2024/06/06 09:59
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class KuaisuSort {
public static void main(String[] args) throws IOException {
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String []s=in.readLine().trim().split(" ");
int a[]=new int[s.length];
for (int i = 0; i < s.length; i++) {
a[i]=Integer.parseInt(s[i]);
}
quick(a);
for (int i: a) {
System.out.print(i+" ");
}
System.out.println();
}
private static void quick(int[] a) {
// TODO Auto-generated method stub
if (a.length>0) {
quickSort(a, 0,a.length-1);
}
}
public static int getMiddle(int[] a,int low, int high) {
// TODO Auto-generated method stub
int temp=a[low];
while (low<high) {
if (low<high && a[high]>temp) {
high--;
}
a[low]=a[high];
while (low<high && a[low]<temp) {
low++;
}
a[high]=a[low];
}
a[low]=temp;
return low;
}
public static void quickSort(int[] a,int low, int high){
int middle;
if (low<high) {
middle = getMiddle(a, low, high);
quickSort(a, low, middle-1);
quickSort(a, middle+1, high);
}
}
}
import java.io.IOException;
import java.io.InputStreamReader;
public class KuaisuSort {
public static void main(String[] args) throws IOException {
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String []s=in.readLine().trim().split(" ");
int a[]=new int[s.length];
for (int i = 0; i < s.length; i++) {
a[i]=Integer.parseInt(s[i]);
}
quick(a);
for (int i: a) {
System.out.print(i+" ");
}
System.out.println();
}
private static void quick(int[] a) {
// TODO Auto-generated method stub
if (a.length>0) {
quickSort(a, 0,a.length-1);
}
}
public static int getMiddle(int[] a,int low, int high) {
// TODO Auto-generated method stub
int temp=a[low];
while (low<high) {
if (low<high && a[high]>temp) {
high--;
}
a[low]=a[high];
while (low<high && a[low]<temp) {
low++;
}
a[high]=a[low];
}
a[low]=temp;
return low;
}
public static void quickSort(int[] a,int low, int high){
int middle;
if (low<high) {
middle = getMiddle(a, low, high);
quickSort(a, low, middle-1);
quickSort(a, middle+1, high);
}
}
}
阅读全文
0 0
- 快速排序
- 快速排序
- 快速排序
- 快速排序!
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- Learnable pooling with Context Gating for video classification
- HDU 5143 NPY and arithmetic progression 思维+枚举
- 在结构体中内存对齐的规则及其重要性
- 机器学习——感知机与神经网络
- [转] 凸包求解算法
- 快速排序
- String
- 19、js三块、弹出框、open、close、定时器
- java开发 10.1/10.2/10.3
- Graph Theory NO.2 HDU_1272_小希的迷宫_并查集
- 如何将usb设置设为第一启动项
- POJ1125股票经纪人dijkstra/Floyd动态规划
- free()
- Codeforces 869C Round #439 Div2 C:排列组合计数