2016-09-23 26 views
0

第一次在這裏發表。我的程序似乎寫得很完美,但停止運行,並允許用戶輸入沒有掃描儀?

我想創建一個比較快速排序,合併排序,冒泡排序和選擇排序的類。我已經實現了所有的排序方法,並創建了一個隨機數組方法,用1000個隨機整數填充隨機數組。但是,當我運行我的程序時,我的主要方法在初始歡迎消息之後停止,並允許用戶輸入。任何幫助將不勝感激,我敢肯定它錯過了一些簡單的錯誤。

import java.util.Random; 
public class TestSort { 
private static int selectCount; 
private static int bubbleCount; 
private static int mergeCount; 
private static int quickCount; 

public static void main(String[] args){ 
    System.out.println("Welcome to the search tester. " 
      + "We are going to see which algorithm performs the best out of 20 tests"); 

    int testSelection = 0; 
    int testBubble = 0; 
    int testQuick = 0; 
    int testMerge = 0; 

    //Check tests 
     int[] a = new int[1000]; 
     populateArray(a); 

     int[] select = a; 
     int[] bubble = a; 
     int[] quick = a; 
     int[] merge = a; 

     testSelection = selectionSort(select); 
     testBubble = bubbleSort(bubble); 
     testQuick = quickSort(quick,0,0); 
     testMerge = mergeSort(merge); 

     System.out.println("Selection sort number of checks: " + testSelection); 
     System.out.println("Bubble sort number of checks: " + testBubble); 
     System.out.println("Quick sort number of checks: " + testQuick); 
     System.out.println("Merge sort number of checks: " + testMerge); 
     System.out.println(""); 
    } 


public static int[] populateArray(int[] a) 
{ 
    Random r = new Random(); 
    a = new int[1000]; 


    for(int i=0; i < a.length; i++) 
    { 
     int num = r.nextInt(1000); 
     a[i] = num; 
    } 
    return a; 
} 

//Sorting methods 

public static int selectionSort(int[] a) 
{ 
    for (int i = 0; i < a.length; i++) 
    { 
     int smallestIndex = i; 
     for (int j=i; j<a.length; j++) 
     { 
      if(a[j]<a[smallestIndex]) 
      { 
       smallestIndex = j; 
      } 
     } 
     if(smallestIndex != i) //Swap 
     { 
      int temp = a[i]; 
      a[i] = a[smallestIndex]; 
      a[smallestIndex] = temp; 
      selectCount++; 
     } 
    } 
    return selectCount; 
} 

public static int bubbleSort (int[] a) 
{ 
    boolean hasSwapped = true; 
    while (hasSwapped == true) 
    { 
     hasSwapped = true; 
     for(int i = 0; i<a.length-1; i++) 
     { 
      if(a[i] > a[i+1]) //Needs to swap 
      { 
       int temp = a[i]; 
       a[i] = a[i+1]; 
       a[i+1] = temp; 
       hasSwapped = true; 
       bubbleCount++; 
      } 
     } 
    } 
    return bubbleCount; 
} 

public static int mergeSort(int [] a) 
{ 
    int size = a.length; 
    if(size<2)//recursive halting point 
    { 
     return 0; 
    } 

    int mid = size/2; 
    int leftSize = mid; 
    int rightSize = size-mid; 
    int [] left = new int[leftSize]; 
    int [] right = new int[rightSize]; 

    for(int i = 0; i<mid; i++) 
    { 
     mergeCount++; 
     left[i] = a[i]; 
    } 
    for(int i = mid; i<size; i++) 
    { 
     mergeCount++; 
     right[i-mid]=a[i]; 
    } 
    mergeSort(left); 
    mergeSort(right); 
    //merge 
    merge(left,right,a); 
    return mergeCount; 
} 
private static void merge(int [] left, int [] right, int [] a) 
{ 
    int leftSize = left.length; 
    int rightSize = right.length; 
    int i = 0;//index of the left array 
    int j = 0; //index of right array 
    int k = 0; //index of the sorted array [a] 

    while(i<leftSize && j<rightSize) 
    { 
     if(left[i]<=right[j]) 
     { 
      a[k] = left[i]; 
      i++; 
      k++; 
     } 
     else 
     { 
      a[k] = right[j]; 
      j++; 
      k++; 
     } 
    } 
    while(i<leftSize) 
    { 
     a[k] =left[i]; 
     i++; 
     k++; 
    } 
    while(j<rightSize) 
    { 
     a[k] =right[j]; 
     j++; 
     k++; 
    } 
} 

public static int quickSort(int[] a, int left, int right) 
{ 
    //partition, where pivot is picked 
    int index = partition(a,left,right); 
    if(left<index-1)//Still elements on left to be sorted 
     quickSort(a,left,index-1); 
    if(index<right) //Still elements on right to be sorted 
     quickSort(a,index+1,right); 
     quickCount++; 

    return quickCount; 
} 
private static int partition(int[] a, int left, int right) 
{ 
    int i = left; 
    int j = right; //Left and right are indexes 
    int pivot = a[(left+right/2)]; //Midpoint, pivot 

    while(i<j) 
    { 
     while(a[i]<pivot) 
     { 
      i++; 
     } 
     while(a[j]>pivot) 
     { 
      j--; 
     } 
     if(i<=j) //Swap 
     { 
      int temp = a[i]; 
      a[i] = a[j]; 
      a[j] = temp; 
      i++; 
      j--; 
     } 
    } 
     return i; 
    } 
} 
+1

我沒有看到這個代碼的任何標準輸入。 –

+1

您的代碼中可能存在無限循環。 –

+0

可能不是真正的問題,但@MichaelMarkidis是正確的,在你的代碼中有一個無限循環。 'boolean hasValue'永遠不會設置爲false。 – Brydenr

回答

1

你的無限循環在冒泡:

public static int bubbleSort(int[] a) 
{ 
    boolean hasSwapped = true; 
    while (hasSwapped == true) 
    { 
     hasSwapped = false; // Need to set this to false 
     for (int i = 0; i < a.length - 1; i++) 
     { 
      if (a[i] > a[i + 1]) // Needs to swap 
      { 
       int temp = a[i]; 
       a[i] = a[i + 1]; 
       a[i + 1] = temp; 
       hasSwapped = true; 
       bubbleCount++; 
      } 
     } 
    } 
    return bubbleCount; 
} 
1

的問題是在你的bubbleSort()方法。 hasSwapped布爾值從不設置爲false,所以while循環無限次。

你的代碼還有另一個問題。在main方法中,您將不得不指定arraypopulateArray()方法返回到a。你在main方法中做的int[] select = a;這樣的分配不會做你想做的事情。相反,只需將數組a發送到您的排序方法。

像這樣:

int[] a = new int[1000]; 
a=populateArray(a); 

testSelection = selectionSort(a); 
testBubble = bubbleSort(a); 
testQuick = quickSort(a,0,0); 
testMerge = mergeSort(a); 
+1

這不工作,因爲它會排序已經排序的數組? –