當前位置:學問谷 >

職場範例 >面試 >

一道面試題:對一個數組進行排序,要求第一個最大、第二個最小

一道面試題:對一個數組進行排序,要求第一個最大、第二個最小

package test;
import ys;
import edList;
import ections;
public class TestSort {

一道面試題:對一個數組進行排序,要求第一個最大、第二個最小

/**
* @param args
*/
static int a[] = {12 ,24 ,32, 28,25 ,60 ,48, 9, 15} ;
public static void main(String[] args) {
for(int i=0; t(a[i]+"t") ;
}
tln();
(a) ;//對數組進行排序
for(int i=0; t(a[i]+"t") ;
}
tln();
for(int i=th-1; i>=0; i--){
t(a[i]+"t") ;
}
tln();
l_sort2(a);
for(int i=0; t(a[i]+"t") ;
}
tln();
l_sort(a);
for(int i=0; t(a[i]+"t") ;
}
tln();
String bitData ="1111010101010010101" ;
formatInt(bitData);
}
public static void l_sort(int a[]){//對排序好的數組進行兩邊彈出
(a) ;
LinkedList b = new LinkedList();
for(int i=0; (a[i]) ;
}
boolean flag = true ;
for(int i=0 ;iif(flag){
a[i] = (Integer)veLast() ;
}else{
a[i] = (Integer)veFirst() ;
}
flag = !flag ;
}
}
public static void l_sort2(int a[]){//用的另一種方法實現
boolean flag = true ;
for(int i=0 ;iif(flag){
for(int j=th-1 ;j>i ;j--){
if(a[j]>a[j-1]){
int team = a[j] ;
a[j] = a[j-1] ;
a[j-1] =team ;
}
}
}else{
for(int j=th-1 ;j>i ;j--){
if(a[j]<=a[j-1]){
int team = a[j] ;
a[j] = a[j-1] ;
a[j-1] =team ;
}
}
}
flag = !flag ;
}
}

下面的方法是我額外做的一個0、1字符轉換成十進制
//二進制數轉換成十進制數
public static void formatInt(String bitData){
double intData = 0 ;
for(int i=0 ;iintData = intData + eOf(tring(i,i+1))*(2, th()-i-1);
}
tln(eOf(intData)tring(0,(eOf(intData)th()-2)));
}
}

標籤: 面試題 數組
  • 文章版權屬於文章作者所有,轉載請註明 https://xuewengu.com/flzc/mianshi/4k6vo2.html