用java写的三种排序的程序,初学练手
来源:互联网 发布:手机淘宝店铺网址 编辑:程序博客网 时间:2024/05/17 04:53
import java.util.Random;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Sort {
public static void quickSort(int a[],int p,int r)
{
if(p<r)
{
int q=Partition(a,p,r);
quickSort(a,p,q-1);
quickSort(a,q+1,r);
}
}
public static int Partition(int a[],int p,int r)
{
int i=p+1;
int j=r;
int x = a[p];
//将<x的元素交换到左边区域
//将>x的元素交换到右边区域
while(true)
{
while(a[i]<x)
{
i=i+1;
}
while(a[j]>x)
{
j=j-1;
}
if(i>=j)
break;
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
a[p]=a[j];
a[j]=x;
return j;
}
public static void InsertionSort(int arr[],int start,int end)
{
for(int p=start;p<=end;p++)
{
int temp = arr[p];
int pt = p-1;
while(pt>=1&&arr[pt]>temp)
{
arr[pt+1] = arr[pt];
pt = pt-1;
}
arr[pt+1] = temp;
}
}
public static void HeapSort(int arr[],int node_number)
{
for(int parents= node_number/2;parents>=1;parents--)
{
int k = parents;
int v = arr[k];
boolean heap = false;
while(!heap&&2*k<=node_number)
{
int j = 2*k;
if(j<node_number)
{
if(arr[j]<arr[j+1])
j = j + 1;
}
if(v>=arr[j])
heap = true;
else
{
arr[k] = arr[j];
k = j;
continue;
}
}
arr[k] = v;
}
}
public static void HeapOut(int arr[]) //将构造的堆取出根节点,送往临时数组
{
int sorted_arr[] = new int [100];
int p = 99;
int node_number = 100;
while(node_number>0&&p>=0)
{
sorted_arr[p] = arr[1];
arr[1] = arr[node_number]; //将最后一个元素赋给根节点
node_number = node_number - 1; //节点数减1
p = p-1;
HeapSort(arr,node_number); //反复构造堆
}
for(int i=1;i<100;i++)
{
arr[i] = sorted_arr[i-1];
}
}
public static void welcome() //欢迎界面
{
System.out.println("~~~~~~~~Sort the array~~~~~~~~~");
System.out.println(" Please choose :");
System.out.println(" 1.Quick Sort.");
System.out.println(" 2.Insertion Sort.");
System.out.println(" 3.Heap Sort.");
}
public static void makearr(int arr[]) //随机数生成器
{
long seed = 9;
Random rnums = new Random(seed);
for(int j=0;j<101;j++)
{
arr[j] = rnums.nextInt(1000);
}
}
public static void main(String[] args)
{
int arr[] = new int [101];
welcome(); //欢迎界面
boolean cnti = true; //用户选择是否尝试另一种排序方法
boolean sorted = false; //用户是否选择了三种排序中的一种并顺利排序
char choice; //键盘输入
while(cnti==true)
{
makearr(arr);
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
choice = (char)in.read();
switch(choice)
{
case '1':
quickSort(arr, 1, 100);
sorted = true;
break;
case '2':
InsertionSort(arr,1,100);
sorted = true;
break;
case '3':
HeapSort(arr,100);
HeapOut(arr);
sorted = true;
break;
default:
System.out.println("Please input your choice in 1,2,3");
break;
}
}
catch(IOException e)
{}
if(sorted==true) //如果选择正确,输出排序结果
{
for(int i=1;i<100;i++)
{
System.out.print(arr[i]);
System.out.print(' ');
}
System.out.println();
}
System.out.println("Continue trying? [Y/N]");
char choice1 = 0;
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
choice1 = (char)in.read();
}
catch(IOException e) {}
if(choice1=='Y'||choice1=='y')
{
welcome();
cnti = true;
sorted = false;
continue;
}
else
cnti = false;
//}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Sort {
public static void quickSort(int a[],int p,int r)
{
if(p<r)
{
int q=Partition(a,p,r);
quickSort(a,p,q-1);
quickSort(a,q+1,r);
}
}
public static int Partition(int a[],int p,int r)
{
int i=p+1;
int j=r;
int x = a[p];
//将<x的元素交换到左边区域
//将>x的元素交换到右边区域
while(true)
{
while(a[i]<x)
{
i=i+1;
}
while(a[j]>x)
{
j=j-1;
}
if(i>=j)
break;
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
a[p]=a[j];
a[j]=x;
return j;
}
public static void InsertionSort(int arr[],int start,int end)
{
for(int p=start;p<=end;p++)
{
int temp = arr[p];
int pt = p-1;
while(pt>=1&&arr[pt]>temp)
{
arr[pt+1] = arr[pt];
pt = pt-1;
}
arr[pt+1] = temp;
}
}
public static void HeapSort(int arr[],int node_number)
{
for(int parents= node_number/2;parents>=1;parents--)
{
int k = parents;
int v = arr[k];
boolean heap = false;
while(!heap&&2*k<=node_number)
{
int j = 2*k;
if(j<node_number)
{
if(arr[j]<arr[j+1])
j = j + 1;
}
if(v>=arr[j])
heap = true;
else
{
arr[k] = arr[j];
k = j;
continue;
}
}
arr[k] = v;
}
}
public static void HeapOut(int arr[]) //将构造的堆取出根节点,送往临时数组
{
int sorted_arr[] = new int [100];
int p = 99;
int node_number = 100;
while(node_number>0&&p>=0)
{
sorted_arr[p] = arr[1];
arr[1] = arr[node_number]; //将最后一个元素赋给根节点
node_number = node_number - 1; //节点数减1
p = p-1;
HeapSort(arr,node_number); //反复构造堆
}
for(int i=1;i<100;i++)
{
arr[i] = sorted_arr[i-1];
}
}
public static void welcome() //欢迎界面
{
System.out.println("~~~~~~~~Sort the array~~~~~~~~~");
System.out.println(" Please choose :");
System.out.println(" 1.Quick Sort.");
System.out.println(" 2.Insertion Sort.");
System.out.println(" 3.Heap Sort.");
}
public static void makearr(int arr[]) //随机数生成器
{
long seed = 9;
Random rnums = new Random(seed);
for(int j=0;j<101;j++)
{
arr[j] = rnums.nextInt(1000);
}
}
public static void main(String[] args)
{
int arr[] = new int [101];
welcome(); //欢迎界面
boolean cnti = true; //用户选择是否尝试另一种排序方法
boolean sorted = false; //用户是否选择了三种排序中的一种并顺利排序
char choice; //键盘输入
while(cnti==true)
{
makearr(arr);
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
choice = (char)in.read();
switch(choice)
{
case '1':
quickSort(arr, 1, 100);
sorted = true;
break;
case '2':
InsertionSort(arr,1,100);
sorted = true;
break;
case '3':
HeapSort(arr,100);
HeapOut(arr);
sorted = true;
break;
default:
System.out.println("Please input your choice in 1,2,3");
break;
}
}
catch(IOException e)
{}
if(sorted==true) //如果选择正确,输出排序结果
{
for(int i=1;i<100;i++)
{
System.out.print(arr[i]);
System.out.print(' ');
}
System.out.println();
}
System.out.println("Continue trying? [Y/N]");
char choice1 = 0;
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
choice1 = (char)in.read();
}
catch(IOException e) {}
if(choice1=='Y'||choice1=='y')
{
welcome();
cnti = true;
sorted = false;
continue;
}
else
cnti = false;
//}
}
}
}
- 用java写的三种排序的程序,初学练手
- 初学遗传算法,一个练手的小程序
- 练手的程序
- 一个swing客户端程序(自己练手写的)
- 初学java时写的
- 初学C++,用C++写的冒泡排序
- java初学基础班经典练手小程序300例
- 关于归并排序的练手
- 字符串的小程序练手
- 链表(随便写的练手的)
- 初学java写的一个大数计算器
- 初学java,写个期末考试的题
- 练手用的
- 初学java所写的ATM程序,望各位指教指教。。。。
- 初学java,写的购物平台控制台小程序。ps:只要努力就会有收获。
- 帮同学写的一个简单地随机数学题生成程序,非常简单,适合练手
- 【初学java】关于java程序的结构
- java练手小程序
- 成年人必看的五个故事
- 转载:一个Delphi程序员的共享经验分享(一个小软件=6万美金)
- c/c++ 网络资源
- 根据用户配置生成EXE
- 掌握 Ajax,第 2 部分: 使用 JavaScript 和 Ajax 发出异步
- 用java写的三种排序的程序,初学练手
- 掌握 Ajax,第 3 部分: Ajax 中的高级请求和响应
- 不患人之不己知,患不知人也。
- 掌握 Ajax,第 4 部分: 利用 DOM 进行 Web 响应
- 掌握 Ajax,第 5 部分: 操纵 DOM
- 笔记 2006.6.2 启动路由器
- JavaScript 参考教程 之一:JavaScript 简介
- JavaScript 参考教程 之二:对象化编程
- 上班族