面试算法题6
面試算法題6
做這些題只是為了愛好,收集供所有找工作的學生和愛好者共享。
?package mymethod;
/**
?* 已知一組數字:21,25,11,32,12,35,55,77,66 要求按以下規則進行排序:第一個數最大,第二個數最小,
?* 第三個數是剩下中的最大的,第四個數是剩下中的最小的,依此類推,請設計一個字符界面的程序解決之.
?* @author acer
?*/
public class Method2 {
?/*
? * 算法思路: 用一個整數指針指向數組的位置,同時實現奇偶轉換.
? * 反復排序,把數組第一個數實現最大,接著整數指針指向第2個位置,排序實現剩余數最小的到第2個數位置.
? * 依次類推.
? */
?private static int[] re={21,25,11,32,12,35,55,77,66};
?private static int p=0;??//整數指針指向數組的位置
?public static void main(String[] args){
??int t=0;
??System.out.println("排序前的數組:");
??for(int i=0;i<re.length;i++)
???System.out.print(re[i]+"/t");
??System.out.println();
??while(p<re.length){
???if(p%2==0){
????t=maxNum(re,p);
????swapNum(re,p,t);
???}else{
????t=minNum(re,p);
????swapNum(re,p,t);
???}
???++p;
??}
??System.out.println("排序后的數組:");
??for(int i=0;i<re.length;i++)
???System.out.print(re[i]+"/t");
?}
?public static int maxNum(int[] r, int t){
??int key=t,temp=r[t];
??for(int i=t+1;i<r.length;i++)
????if(temp<r[i]){
?????key=i;
?????temp=r[i];
????}
??return key;
?}
?public static int minNum(int[] r, int t){
??int key=t,temp=r[t];
??for(int i=t+1;i<r.length;i++)
????if(temp>r[i]){
?????key=i;
?????temp=r[i];
????}
??return key;
?}
?public static void swapNum(int[] r,int p,int k){
??int temp=r[p];
??r[p]=r[k];
??r[k]=temp;
?}
}
轉載于:https://www.cnblogs.com/yangjin-55/archive/2007/04/21/2787020.html
總結
- 上一篇: Js+DVML:很酷实用的右键弹出菜单
- 下一篇: 刚回到北京,倒时差中……