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;
//}
}
}
}