2016-09-23 24 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

这不工作,因为它会排序已经排序的数组? –