2017-10-27 16 views
0

私は現在ソートアルゴリズムの実装を伴う大学の割り当てに取り組んでいます。私はクイックソートアルゴリズムを正しく実装していると信じていますが、テストクラスでは、配列を並べ替えずに読み込まれる配列を出力します。以下は、テストクラスのコードと、実際のクイックソートのコード( 'sort'という別のクラスにあります)です。 誰かが私が間違っていることを知っていますか?あなたの問題のクイックソートアレイが正しく印刷されていませんか? (java)

import java.io.*; 
import java.text.*; 
import java.util.*; 

public class Sort { 

/** Array of integers to sort **/ 
private int[] A; 

/** Size of the array **/ 
private int size; 

/** Number of elements actually used in array **/ 
private int usedSize; 

/** Global variables for counting sort comparisons **/ 
public int compIS; 
/** Global comparison count for Insertion Sort **/ 
public int compQS; 
/** Global comparison count for Quicksort **/ 
public int compNewS; 

/** Global comparison count for new sort **/ 

/*****************/ 
/** Constructor **/ 
/*****************/ 
Sort(int max) { 
    /** Initialiase global sort count variables **/ 
    compIS = 0; 
    compQS = 0; 
    compNewS = 0; 

    /** Initialise size variables **/ 
    usedSize = 0; 
    size = max; 

    /** Create Array of Integers **/ 
    A = new int[size]; 
} 

public int getRightElement() { 
    return usedSize - 1; 
} 

public int getLeftElement() { 
    return A[0]; 
} 

/*********************************************/ 
/*** Read a file of integers into an array ***/ 
/*********************************************/ 
public void readIn(String file) { 
    try { 
     /** Initialise loop variable **/ 
     usedSize = 0; 

     /** Set up file for reading **/ 
     FileReader reader = new FileReader(file); 
     Scanner in = new Scanner(reader); 

     /** Loop round reading in data while array not full **/ 
     while (in.hasNextInt() && (usedSize < size)) { 
      A[usedSize] = in.nextInt(); 
      usedSize++; 
     } 

    } catch (IOException e) { 
     System.out.println("Error processing file " + file); 
    } 
} 

/**********************/ 
/*** Display array ***/ 
/**********************/ 
public void display(int line, String header) { 
    /*** Integer Formatter - three digits ***/ 
    NumberFormat FI = NumberFormat.getInstance(); 
    FI.setMinimumIntegerDigits(3); 

    /** Print header string **/ 
    System.out.print("\n" + header); 

    /** Display array data **/ 
    for (int i = 0; i < usedSize; i++) { 
     /** Check if new line is needed **/ 
     if (i % line == 0) { 
      System.out.println(); 
     } 

     /** 
     * Display an ar ray element 
     **/ 
     System.out.print(FI.format(A[i]) + " "); 
    } 
} 
public void quick(int L, int R) { 
    /* ensure there is more than one element in array */ 
    if (R > L) { 
     /* split array in two */ 
     int pLoc = partition(L, R); 
     /* sort left half */ 
     quick(L, pLoc - 1); 
     /* sort right half */ 
     quick(pLoc + 1, R); 
    } 

    System.out.println("\n\nAfter QuickSort: "); 
    for (int i = 0; i < usedSize; i++) { 
     System.out.println(A[i] + " "); 
    } 
} 

/* partitions array for quicksort */ 
public int partition(int L, int R) { 
    /* Select pivot */ 
    int pivot = A[R]; 
    /* initialise scanning pointers */ 

    int pR = R; 
    int pL = L; 
    /* repeat until pointers cross */ 
    while (pL < pR) { 
     compQS++; 
     /* move left pointer */ 
     while (A[pL] < pivot) { 
      pL++; 
     } 
     /* move right pointer */ 
     while ((A[pR] >= pivot) && (pR > L)) { 
      pR--; 
      //compQS++; 
     } 
     /* swap elements */ 
     //compQS++; 
     if (pL < pR) { 
      swap(pL, pR); 
      L++; 
      R--; 
     } 
    } 


    /* put pivot in correct position */ 
    swap(pL, R); 
    /* return pivot position */ 
    return pL; 

} 

/* swaps elements in quicksort */ 
public void swap(int i, int j) { 
    int temp = A[i]; 
    A[i] = A[j]; 
    A[j] = temp; 
} 


    public class TestSort 
{ 
    public static void main(String[] args) 
    { 
     Sort sortTest = new Sort(100); 



    /** Read in test data into array **/ 
    sortTest.readIn("test1.txt"); 

    /** Display array **/ 
    sortTest.display(10,"Input Array 1"); 
    /*apply insertion sort to array*/ 
    //sortTest.insertion(); 

    //sortTest.readIn("test1.txt"); 
    sortTest.quick(sortTest.getLeftElement(), sortTest.getRightElement()); 
    sortTest.newSort(); 

    /** Display comparison counters **/ 
    System.out.println("Quicksort comparison counter: " + sortTest.compQS); 
    System.out.println("\n\nInsertion sort comparison counter: " + sortTest.compIS); 


} 
+1

あなたはすべてを貼り付けることはできますか?これをコードレビューに投稿することをお勧めします。https://codereview.stackexchange.com/ – Admit

+1

実行可能コードを提供してください。 –

+1

@Admit。このコードに問題があるので、SOは適切な場所です。しかし、質問の表現は不完全です。 –

答えて

0

一つgetLeftElement()はあなたの配列内の位置0で値を返すだけではなく0を返しているということです。配列の最初の要素の値が配列のサイズよりも大きい場合、ソートは行われません。

また、quickメソッドの実装が間違っていると思います。このメソッド内で、再帰的に呼び出すのはquick(L, pLoc - 1)quick(pLoc + 1, R)です。この方法で呼び出すと、配列内の配列のすべてのインデックスをトラバースしません。 (そして、あなたは、配列のソートにインデックス5を伴わない、L0であればExが、R10あり、そしてpLoc5です。)

関連する問題