在Java中的ArrayList中合并排序

时间:2015-04-02 13:58:52

标签: java sorting arraylist mergesort

我正在学习如何在Java中实现基本算法,所以我是这个环境中的新手。我正在尝试使用ArrayList实现Merge Sort算法,其中程序将从文件中读取数据(每行中的整数)并使用Merge Sort生成排序结果。但是,我的代码显示相同的结果,因为它没有排序任何东西!如果有人能够确定我的错误在哪里,我会很高兴的。由于我是初学者,代码非常简单,没有优化,可能性能也不是很快。

这是我的代码:

public class MergeSortExp1 {

    public static void main(String[] args) {

        ArrayList<Integer>number = new ArrayList<Integer>();

        Scanner myScanner = null;
        try {
        myScanner = new Scanner(new File("/Users/Sabbir/Desktop/workload.txt"));
        } catch (FileNotFoundException e) {

        e.printStackTrace();
        }

        while(myScanner.hasNextInt()){
            number.add(myScanner.nextInt());
        }

        System.out.println("Before sorting" +number);
        number=mergeSort(number);
        System.out.println("Sorted Array =" +number);
    }

        public static ArrayList<Integer> mergeSort( ArrayList<Integer> Input)
        {
            if (Input.size() ==1){
                return Input;
            }
            else {
                int mid= Input.size()/2;
                ArrayList<Integer> left= new ArrayList<Integer>(mid);
                ArrayList<Integer> right=new ArrayList<Integer>(Input.size()-mid);

               for (int i = 0; i < mid; i++) {
                   left.add(Input.get(i));
                   } 

               for (int i = 0; i < Input.size()-mid; i++) {
                   right.add(Input.get(i));
               } 

               left=mergeSort(left); 
               right=mergeSort(right);
               merge(left,right,Input);
            }
               return Input;
        }


        public static void merge (ArrayList<Integer>left,ArrayList<Integer>right,ArrayList<Integer>Input)
        {
            int i1=0;// left Index
            int i2=0;// right Index
            int InputIndex=0;

            for (int i = 0; i < Input.size(); i++) {
                if (i2>=right.size() || (i1<left.size() && left.get(i)<=right.get(i))) 
                {
                    Input.set(InputIndex,left.get(i1));
                    InputIndex++;
                }
                else {
                    Input.set(InputIndex, right.get(i2));
                    InputIndex++;
                }
            }

        }
}

2 个答案:

答案 0 :(得分:0)

如果你的合并方法是好的(我不测试它),你会忘记左右合并输入,编辑你的代码,如下所示,然后重试:

 // This is called recursion. Calling a method again within the  
 //method until the value of left and right becomes 1.
 left=mergeSort(left); 
 right=mergeSort(right);

 merge(left,right,Input);

希望帮到你!

答案 1 :(得分:-2)

import java.util.ArrayList;
import java.io.*;
import java.util.*;

public class Mergesort1 {

    public static void main(String[] args) {
        ArrayList<Integer> values = new ArrayList<Integer>();
        int zeilen = 0;

        try{
            FileInputStream in = new FileInputStream(args[0]);
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;
            while((line = reader.readLine()) != null){
                values.add(Integer.parseInt(line));
                zeilen++;
            }
            try{  
                FileOutputStream out = new FileOutputStream(args[1]);
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));

                sort(values); //sortien array list
                for (int i=0; i<values.size(); i++){
                    writer.write("" + values.get(i));
                    writer.newLine();
                }
                writer.close();
            }
            catch(Exception e){
                System.out.println(e);
            }
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
      public static ArrayList<Integer> sort( ArrayList<Integer> values)
        {
            if (values.size() ==1){
                return values;
            }
            else {
                int mid= values.size()/2;
                ArrayList<Integer> left= new ArrayList<Integer>(mid);
                ArrayList<Integer> right=new ArrayList<Integer>(values.size()-mid);

               for (int i = 0; i < mid; i++) {
                   left.add(values.get(i));
                   } 

               for (int i = mid; i < values.size(); i++) {
                   right.add(values.get(i));
               } 

               left=sort(left); 
               right=sort(right);
               merge(left,right,values);
            }
               return values;
        }


        public static void merge (ArrayList<Integer>left,ArrayList<Integer>right,ArrayList<Integer>values)
        {
            int i1=0;// left Index
            int i2=0;// right Index
            int InputIndex=0;



            for (int i = 0; i < values.size(); i++) {

                if(i1==left.size()){
                    values.set(i, right.get(i2));
                    i2++;
                }
                else{
                    if (i2==right.size()){
                        values.set(i,left.get(i1));
                        i1++;
                    }
                    else{
                        if (left.get(i1)<=right.get(i2)) {
                            values.set(i,left.get(i1));
                            i1++;
                        }
                        else {
                            if (left.get(i1)>=right.get(i2)) {
                            values.set(i, right.get(i2));
                            i2++;
                            }
                        }
                    }
                }

            }
        }
}