Java 排序比较计数器

Java 排序比较计数器,java,sorting,merge,Java,Sorting,Merge,我有一段代码,它对填充了随机数的数组进行排序,并计算完成排序所需的数字比较。我正在使用排序方法选择气泡和合并排序。我有选择和气泡的计数器,但没有合并我不知道放在哪里。这可能是一个简单的答案,但我就是无法让它发挥作用 代码: /*********************************************************************** * *选择排序: *读取数组,然后搜索最大的数字。 *在找到最大的数字后,它将该数字与 *数组的最后一个数字 *前提条件:接受一

我有一段代码,它对填充了随机数的数组进行排序,并计算完成排序所需的数字比较。我正在使用排序方法选择气泡和合并排序。我有选择和气泡的计数器,但没有合并我不知道放在哪里。这可能是一个简单的答案,但我就是无法让它发挥作用

代码:

/***********************************************************************
* 
*选择排序:
*读取数组,然后搜索最大的数字。
*在找到最大的数字后,它将该数字与
*数组的最后一个数字
*前提条件:接受一个“n”项数组,在本例中
*大小写是数组中的2000、4000、6000、8000和10000个随机项
*后置条件:所有数字均按升序排序
* 
**********************************************************************/
公共静态整数选择排序(整数[]数组){
//将比较的初始计数设置为0
比较=0;//进行的交换数量
对于(int last=intArray.length-1;last>0;last--){
int largestIndex=last;//将最大数字放在数组末尾的int
//求最大数
for(int i=0;iintArray[largestIndex]){
largestIndex=i;//切换最后一个数字和i
}//如果结束,则结束
//比较+1
比较++;
}//结束
//将最后一个元素与最大元素交换
int最大=intArray[最后];
intArray[最后]=intArray[最大索引];
intArray[最大指数]=最大;
}
//返回比较计数器
回报比较;
}
/***********************************************************************
* 
*气泡排序:
*获取一个随机整数数组,并通过比较相邻整数对其排序
*数字相互关联。取相邻数中较大者为准
*Sort将其切换到相邻数字的后端。是的
*直到列表完全排序为止。
*前提条件:接受随机整数数组
*后置条件:数组从最小到最大排序
* 
**********************************************************************/
公共静态int BubbleSort(int[]intArray){
//实例变量
int n=intArray.length;
//布尔交换;
比较=0;
//交换=假;
//因为i从0开始,当i小于数组长度时,i++直到达到数组长度
对于(int i=0;iintArray[j]){
//交换元素
内部温度=内部温度[j];
intArray[j]=intArray[j+1];
内阵列[j+1]=温度;
//swap=true;
} 
//比较得到+1,直到for循环完成排序
比较++;
}//循环结束
}
//返回比较计数器
回报比较;
}
/************************************************************************************
* 
*合并排序:
*此方法将随机数组拆分为两半。一旦阵列
*一分为二,创建临时数组。此临时阵列是由
*该方法搜索原始数组的两部分并放置信息
*按顺序存储在临时数组中。一旦所有的数字都按顺序排列好了
*然后将临时数组复制回原始数组。
*先决条件:接受随机整数数组
*后置条件:返回按升序排序的随机数组
* 
**********************************************************************************/
公共静态整数合并排序(整数[]整数数组){
如果(intArray.length>=2){
int mid=intArray.length/2;
//创建2个数组,以在每个数组中存储一半的数据
int[]first=new int[mid];//保存数组的前半部分
int[]second=new int[intArray.length-mid];//保存数组的后半部分
对于(int i=0;i/***********************************************************************
     * 
     * Selection Sort:
     * Reads in the array and then searches for the largest number. 
     * After it finds the largest number, it then swaps that number with the
     * last number of the array
     * Precondition: takes in an array of "n" items, which in this particular
     * case is 2000, 4000, 6000, 8000, and 10000 random items in an array
     * Postcondition: all numbers are sorted in ascending order
     * 
     **********************************************************************/
    public static int SelectionSort (int[] intArray) {

        //Set initial count of comparisons at 0         
        comparisons= 0;        //Number of swaps made


        for(int last = intArray.length - 1; last > 0; last--) {

            int largestIndex = last;    //Int which places the largest number at the end of the array

            // Find largest number
            for(int i = 0; i < last; i++) {

                //if i > the last number
                if (intArray[i] > intArray[largestIndex]){
                    largestIndex = i;       //switch the last number and i
                } // end if

                //Comparison+1 
                comparisons++;

            } // end for

            // Swap last element with largest element
            int largest = intArray[last];
            intArray[last] = intArray[largestIndex];
            intArray[largestIndex] = largest;

        }
        //Return comparison counter
        return comparisons;
    }

    /***********************************************************************
     * 
     * Bubble Sort:
     * Takes an array of random integers and sorts them by comparing adjacent
     * numbers to one another. Whichever the larger adjacent number, Bubble
     * Sort switches it towards the back end of the adjacent numbers. It does
     * this until the list is fully sorted.
     * Precondition: takes in a random array of integers
     * Postcondition: array is sorted from smallest to largest
     * 
     **********************************************************************/
    public static int BubbleSort (int[] intArray) {

        //Instance Variables    
        int n = intArray.length;
        //boolean swap;   
        comparisons = 0;

        //swap = false;
        //for i starts at 0, when i is less than array length, i++ until reach array length
        for(int i=0; i < n; i++) {

            for(int j=1; j < (n-i); j++) {

                if(intArray[j-1] > intArray[j]) {

                    //Swap the elements
                    int temp = intArray[j];
                    intArray[j] = intArray[j+1];
                    intArray[j+1] = temp;
                    //swap = true;

                } 
            //comparisons get +1 until the for loop is done sorting
            comparisons++;
           }  //End for loop
        }
        //Return the comparison counter
         return comparisons;
    }

    /************************************************************************************
     * 
     * Merge Sort:
     * This method takes a random array and splits it in half. Once the array is
     * split in half, it creates a temp0rary array. This temporary array is built by
     * the method searching the two halves of the original array and puts the information
     * in order stored in the temporary array. Once all the numbers are in order, the 
     * temporary array is then copied back to the original array.
     * Precondition: take in an array of random integers
     * Postcondition: return the random array sorted in ascending order
     * 
     **********************************************************************************/
    public static int mergeSort(int[] intArray) {

        if(intArray.length >= 2) {

            int mid = intArray.length / 2;
            //Create 2 arrays to store half of the data in each
            int[] first = new int[mid];     //holds first half of array
            int[] second = new int[intArray.length - mid];  //holds second half of array

            for(int i = 0; i < first.length; i++) { 
                first[i] = intArray[i];     
            }

            for(int i = 0; i < second.length; i++) {
                second[i] = intArray[mid+i];
            }

            mergeSort(first);
            mergeSort(second);
            merge(first, second, intArray);     //Merge all together

        }

        return comparisons;
    }

    //merging first and second halves of mergeSort array
    public static int merge(int[] first, int[] second, int[] intArray) {

        int iFirst = 0;
        int iSecond = 0;
        int i = 0; 

        //moving the smaller element into intArray
        while(iFirst < first.length && iSecond < second.length) {

            comparisons++;

            if(first[iFirst] < second[iSecond]) {
                intArray[i] = first[iFirst];
                iFirst++;
            }

            else {
                intArray[i] = second[iSecond];
                iSecond++;
            }
            i++;
        }


        //copying the remaining of first array
        while(iFirst < first.length) {
            intArray[i] = first[iFirst];
            iFirst++; i++; 
        }

        //copying the remaining of second array
        while(iSecond < second.length)
        {
            intArray[i] = second[iSecond];
            iSecond++; i++; 
        } 

        return comparisons;
    }

    /**************************************************************************************
     * Instance methods:
     * These methods perform actions to the array.
     * 
     * copyArray()--makes a copy of the array to be used in the main class
     *              so that each method is able to create the same array
     * 
     * printArray()--prints out the array for display
     * 
     * randomArray()--creates a random integer array used by all three sorting methods
     * 
     **************************************************************************************/

    public static int[] copyArray(int[] intArray) {

        //Constructor that creates copyArray
        int[] copyArray = new int[intArray.length];     //siz equal to the length of the array

        for(int i = 0; i < intArray.length; i++){
            copyArray[i] = intArray[i];
        } // end for

        return copyArray;

    } // end copyArray

    //Prints out array
    public static void printArray(int[] intArray){
        //Preconditions
        //  Input: unsorted integer array   
        //  Assumptions: array is full
        //Postconditions
        //  Output: none
        //  Actions: array is displayed on screen

        System.out.print("Array==> ");
        for(int i = 0; i < intArray.length; i++){
            System.out.print(intArray[i] + " ");
        } // end for

        System.out.println(" ");

    } // end printArray

    //Creates a random array that is used for each sorting method
    public static int[] randomArray(int array, double range){
        //Preconditions
        //  Input: size of array(n), range of integers (0 to range)
        //  Assumptions: none
        //Postconditions
        //  Output: array of random integers 0 to floor(range) 
        //  Actions: none

        int[] intArray = new int[array];

        for(int i = 0; i < array; i++){
            intArray[i] = (int)(Math.random() * range);
        } // end for

        return intArray;

    } // end randomIntArray


}
public static int mergeSort(int[] intArray, int comparisons) {

    if(intArray.length >= 2) {

        int mid = intArray.length / 2;
        //Create 2 arrays to store half of the data in each
        int[] first = new int[mid];     //holds first half of array
        int[] second = new int[intArray.length - mid];  //holds second half of array

        for(int i = 0; i < first.length; i++) { 
            first[i] = intArray[i];     
        }

        for(int i = 0; i < second.length; i++) {
            second[i] = intArray[mid+i];
        }

        comparisons = mergeSort(first, comparisons);
        comparisons = mergeSort(second, comparisons);
        comparisons = merge(first, second, intArray, comparisons);     //Merge all together

    }

    return comparisons;
}

//merging first and second halves of mergeSort array
public static int merge(int[] first, int[] second, int[] intArray, int comparisons) {

    int iFirst = 0;
    int iSecond = 0;
    int i = 0; 

    //moving the smaller element into intArray
    while(iFirst < first.length && iSecond < second.length) {

        comparisons++;

        if(first[iFirst] < second[iSecond]) {
            intArray[i] = first[iFirst];
            iFirst++;
        }

        else {
            intArray[i] = second[iSecond];
            iSecond++;
        }
        i++;
    }


    //copying the remaining of first array
    while(iFirst < first.length) {
        intArray[i] = first[iFirst];
        iFirst++; i++; 
    }

    //copying the remaining of second array
    while(iSecond < second.length)
    {
        intArray[i] = second[iSecond];
        iSecond++; i++; 
    } 

    return comparisons;
}