在这里我只给出最普通的一种方法,其他最优的请自行考虑:
import java.util.*;
public class BianChengZhiMei {
public static void main(String args[])
{
int[] st = {3,2,1,6,5,4,9,8,5,7,0,15,25,10,66,24,25,20,99,70};
st=sort_bing(st,st.length);
System.out.print("最终结果: ");
for(int i=0;i<st.length;i++)
{
System.out.print(st[i]+" ");
}
}
public static int[] sort_bing(int[] array,int n)
{
System.out.print("第"+(array.length-n)+"次翻饼过程: ");
for(int i=0;i<array.length;i++)
{
System.out.print(array[i]+" ");
}
System.out.println();
Stack<Integer> stack = new Stack<Integer>();
int[] arry = array;
int max=arry[0],j=0,k =n;
if(n==1)
return arry;
for(int i=0;i<k;i++)
{
if(max<=arry[i])
{
max = arry[i];
j=i;
}
}
for(int i=0;i<=j;i++)
{
stack.push(arry[i]);
}
for(int i=0;i<=j;i++)
{
arry[i]=stack.pop();
}
for(int i=0;i<k;i++)
{
stack.push(arry[i]);
}
for(int i=0;i<k;i++)
{
arry[i]=stack.pop();
}
return sort_bing(arry,k-1);
}
}
结果如下:
第0次翻饼过程: 3 2 1 6 5 4 9 8 5 7 0 15 25 10 66 24 25 20 99 70
第1次翻饼过程: 70 3 2 1 6 5 4 9 8 5 7 0 15 25 10 66 24 25 20 99
第2次翻饼过程: 20 25 24 66 10 25 15 0 7 5 8 9 4 5 6 1 2 3 70 99
第3次翻饼过程: 3 2 1 6 5 4 9 8 5 7 0 15 25 10 20 25 24 66 70 99
第4次翻饼过程: 24 3 2 1 6 5 4 9 8 5 7 0 15 25 10 20 25 66 70 99
第5次翻饼过程: 20 10 24 3 2 1 6 5 4 9 8 5 7 0 15 25 25 66 70 99
第6次翻饼过程: 15 0 7 5 8 9 4 5 6 1 2 3 20 10 24 25 25 66 70 99
第7次翻饼过程: 10 15 0 7 5 8 9 4 5 6 1 2 3 20 24 25 25 66 70 99
第8次翻饼过程: 3 2 1 6 5 4 9 8 5 7 0 10 15 20 24 25 25 66 70 99
第9次翻饼过程: 3 2 1 6 5 4 9 8 5 7 0 10 15 20 24 25 25 66 70 99
第10次翻饼过程: 0 7 5 8 3 2 1 6 5 4 9 10 15 20 24 25 25 66 70 99
第11次翻饼过程: 4 5 6 1 2 3 0 7 5 8 9 10 15 20 24 25 25 66 70 99
第12次翻饼过程: 5 4 5 6 1 2 3 0 7 8 9 10 15 20 24 25 25 66 70 99
第13次翻饼过程: 0 3 2 1 5 4 5 6 7 8 9 10 15 20 24 25 25 66 70 99
第14次翻饼过程: 0 3 2 1 5 4 5 6 7 8 9 10 15 20 24 25 25 66 70 99
第15次翻饼过程: 4 0 3 2 1 5 5 6 7 8 9 10 15 20 24 25 25 66 70 99
第16次翻饼过程: 1 2 3 0 4 5 5 6 7 8 9 10 15 20 24 25 25 66 70 99
第17次翻饼过程: 0 1 2 3 4 5 5 6 7 8 9 10 15 20 24 25 25 66 70 99
第18次翻饼过程: 0 1 2 3 4 5 5 6 7 8 9 10 15 20 24 25 25 66 70 99
第19次翻饼过程: 0 1 2 3 4 5 5 6 7 8 9 10 15 20 24 25 25 66 70 99
最终结果: 0 1 2 3 4 5 5 6 7 8 9 10 15 20 24 25 25 66 70 99