2013-07-02 3 views
-5

배열을 오름차순과 내림차순으로 정렬하는 방법을 알고 있지만 만들려는 특정 패턴이 있습니다. 예를 들어, 임의의 순서로 배열이 있습니다. 패턴에서이 배열을 어떻게 정렬합니까? "가장 작은 것, 가장 큰 것, 두 번째로 작은 것, 두 번째로 큰 것, 세 번째로 작은 것, 세 번째로 큰 것 ..."등 어떤 아이디어입니까?Java : 배열 정렬 (2 부)

public class Test2 { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    int[] array = {1,4,2,6,9,3,65,77,33,22}; 
    for(int i = 0; i < array.length; i++){ 
     System.out.print(" " + array[i]); 
}  
    wackySort(array); 

} 


public static void wackySort(int[] nums){ 
    int sign = 0; 
    int temp = 0; 
    int temp2 = 0; 
//This sorts the array 
    for (int i = 0; i < nums.length; i++){ 
     for (int j = 0; j < nums.length -1; j++){ 
      if (nums[j] > nums[j+1]){ 
       temp = nums[j]; 
       nums[j] = nums[j+1]; 
       nums[j+1] = temp; 

     } 
    } 

} 
// This prints out the new array 
      System.out.println(); 
      for(int i = 0; i < nums.length; i++){ 
       System.out.print(" " + nums[i]); 
      } 
      System.out.println(); 

//This part attempts to fix the array into the order I want it to 
      int firstPointer = 0; 
      int secondPointer = nums.length -1; 
      int[] newarray = new int[nums.length]; 
    for (int i = 0; i < nums.length -1; i+=2){ 
     newarray[i] = nums[firstPointer++]; 
        newarray[i] = nums[secondPointer--]; 
} 
      for(int i = 0; i < newarray.length; i++){ 
       System.out.print(" " + newarray[i]); 
} 


} 
} 
+0

여기에 같은 질문을 게시해서는 안됩니다. 마지막 게시글의 업데이트 된 답변 확인 – stinepike

+0

http://www.digizol.com/2008/07/java-sorting-comparator-vs-comparable.html –

+0

감사합니다. 이 게시물을 삭제하려면 어떻게해야합니까? – Binka

답변