一 用Java实现快速排序
采用的思想是递归的思想
import java.util.Scanner;
public class quick {
private static void quickSort(int[] array, int low, int high) {
if (low >= high) {
return;
}
int i = low, j = high, temp = array[i]; // 最左边的数作为比较数
while (i < j) {
while (i < j && array[j] >= temp) { // 向左寻找第一个小于比较数的数
j--;
}
if (i < j) {
array[i++] = array[j]; // 将array[j]填入array[i],并将i向右移动
}
while (i < j && array[i] < temp) {// 向右寻找第一个大于temp的数
i++;
}
if (i < j) {
array[j--] = array[i]; // 将array[i]填入array[j],并将j向左移动
}
}
array[i] = temp;
quickSort(array, low, i - 1);
quickSort(array, i + 1, high);
}
public static void main(String[] args) {
quick q = new quick();
int a[] = new int[100];
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
quick.quickSort(a, 0, n - 1);
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
}
}
二 用Java实现桶排序
package 桶排序;
import java.util.Scanner;
public class demo {
public void main(String args[])
{
int a[]=new int[101];
for(int i=0;i<a.length;i++)
a[i]=0;
Scanner scanner=new Scanner(System.in);
System.out.println("enter n numbers:");
int n=scanner.nextInt();
for(int i=0;i<n;i++)
{
int t=scanner.nextInt();
a[t]++;
}
for(int i=0;i<101;i++)
{
for(int j=0;j<a[i];j++)
{
System.out.print(i+" ");
}
}
scanner.close();
System.out.println("桶排序就是这样");
}
}