簡體   English   中英

Quicksort陣列打印不正確嗎? (java)

[英]Quicksort array is not printing out correctly? (java)

我目前正在從事大學作業,其中涉及實施排序算法。 我相信我已經正確實現了quicksort算法,但是在測試類中,該方法只是打印出要讀取的數組,而沒有對其進行排序。 下面是來自測試類的代碼,以及實際quicksort的代碼(位於單獨的類“ sort”中)。 有人知道我在做什么錯嗎?

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);


}

您的問題之一是getLeftElement()返回數組中位置0的值,而不僅僅是返回0 在數組中第一個元素的值大於數組大小的情況下,將不進行排序。

另外,我認為您quick實現該方法是不正確的。 在該方法中,您遞歸調用quick(L, pLoc - 1)quick(pLoc + 1, R) 通過以這種方式調用,您不會遍歷數組中數組的所有索引。 (例如,如果L0R10pLoc5 ,則在數組排序中不涉及索引5。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM