简体   繁体   English

"使用插入排序和归并排序的混合排序"

[英]Hybrid sorting using insertion sort and merge sort

I wish to implement a hybrid algorithm that switches from insertion sort to merge sort once the input array size becomes too big.我希望实现一种混合算法,一旦输入数组大小变得太大,它就会从插入排序切换到合并排序。

This is my main function (I fixed my input array size at 30 currently as I wish to test my merge sort function) :这是我的主要功能(我目前将输入数组大小固定为 30,因为我希望测试我的合并排序功能):

    public static void main(String[] args) { 
        int[] a = genrandarray(30);
        
        long bgn, end;
        bgn = System.currentTimeMillis();
        
        imsort(a, 0, a.length, 30);
        
        end = System.currentTimeMillis();
        
        for(int i = 1; i < a.length; i++){
            if(a[i-1] > a[i]){
                System.out.println("failed");
                break;
            }
        }
        System.out.println("milliseconds " + (end-bgn));
    }

a.length returns you 30 which is the length of your random array from the genrandarray method i believe. a.length 返回 30,这是我相信的 genrandarray 方法中随机数组的长度。 And your array is indexed 0 through 29. Try changing the main method like this and it will work out并且您的数组索引为 0 到 29。尝试像这样更改 main 方法,它会成功

public static void main(String[] args) { 
        int[] a = genrandarray(30);
        
        long bgn, end;
        bgn = System.currentTimeMillis();
        
        imsort(a, 0, a.length-1, 30);
        
        end = System.currentTimeMillis();
        
        for(int i = 1; i < a.length; i++){
            if(a[i-1] > a[i]){
                System.out.println("failed");
                break;
            }
        }
        System.out.println("milliseconds " + (end-bgn));
    }

First, let me congratulate on a very good post for someone new.首先,让我祝贺新人发表了一篇非常好的帖子。 You didn't post the line of code where the error is happening and code isn't complete, so I filled in some blanks and executed it:您没有发布发生错误且代码不完整的代码行,因此我填写了一些空白并执行了它:

import java.util.Arrays;
import java.util.Random;

public class Test {

    public static int[] genrandarray(int n)
    {
        int[] a = new int[n];
        Random r = new Random();
        for(int i=0;i<n;i++) a[i] = r.nextInt();
        return a;
    }
    
    public static void main(String[] args) { 
        int[] a = genrandarray(30);
        
        long bgn, end;
        bgn = System.currentTimeMillis();
        
        imsort(a, 0, a.length, 30);
        
        end = System.currentTimeMillis();
        
        for(int i = 1; i < a.length; i++){
            if(a[i-1] > a[i]){
                System.out.println("failed");
                break;
            }
        }
        System.out.println("milliseconds " + (end-bgn));
    }

    public static void imsort(int [] slot, int b, int e, int size) {
        
        //if smaller than 20, use insertion sort
        if (e-b<=20) {
            Arrays.sort(slot, 0, e);
           System.out.println("imsort entered!");
        }
        

        else {
            mergesort(b, e, slot);
            
            System.out.println("mergesort entered!");
        }
    }

    public static int merge(int n, int m, int[] slot) {
        int mid = (n+m)/2;
        int a = n, b = mid+1, i, tmp, cmp=0, comp=0;
        
        //sanity check 
        if (m-n <= 0) return -1000;
        
        while (a <= mid && b <= m) {
            cmp = slot[a] - slot[b]; 
            comp++;
            
            if (cmp > 0) { //slot[a] > slot[b]
                tmp = slot[b++];
                for (i = ++mid; i > a; i--)
                    slot[i] = slot[i-1];
                    slot[a++] = tmp;
            } 
            
            else if (cmp < 0) //slot[a] < slot[b] 
                    a++;
            
            else {
                //slot[a] == slot[b]
                if (a == mid && b == m) break;
                tmp = slot[b++];
                a++;
                for (i = ++mid; i > a; i--)
                slot[i] = slot[i-1]; slot[a++] = tmp;
            }
        } // end of while loop;
        
        return comp;
    } // end of merge   

    public static int mergesort(int s, int e, int[] slot) {
        //int comp =0;
        int mid = (s+e)/2;  
        int right=0, left=0;
        
        if(e-s>0) {
            //comp++;
            left = mergesort(s,mid, slot);
            //comp++;
            right = mergesort(mid+1,e, slot);
        }
        return right + left + merge(s,e,slot);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM