QuickSort的实现不正确

时间:2014-08-08 03:11:16

标签: java debugging sorting quicksort

我正在从Youtube学习Quick Sort并尝试实现在左侧标记之前用1个元素交换数据透视图的实现方式

这是QuickSort算法的伪代码

Method
Divide-and-conquer
Pick an element (pivot) from the list
Pivot is arbitrarily chosen
Normally, the first element is selected
Partition the list into two halves such that:
All the elements in the first half is smaller than the pivot
All the elements in the second half is greater than the pivot
After the rearrangement, the pivot element (pivot) occupies a proper position in a sorting of the list.
Recursively
Quick-sort the 1st half
Quick-sort the 2nd half

Java代码

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;



public class QuickSort 
{
    public static void main(String args[])
    {
        Vector<Integer> container = new Vector<Integer>();

        String userinput = "data1.txt";
        Scanner myScanner = new Scanner("foo"); // variable used to read file

        try
        {
            //open filename

            File inputfile = new File("C:\\Users\\8382c\\workspace\\AdvanceAlgorithmA3_Quicksort\\src\\" + userinput);
             myScanner = new Scanner(inputfile);

        }
        catch(FileNotFoundException e)
        {
            System.out.println("File cant be found");
        }


         String line = myScanner.nextLine(); //read 1st line which contains the number of numbers to be sorted

         while(myScanner.hasNext())
         {
             container.add(myScanner.nextInt());
         }


        System.out.println(line);

        /*container.add(7);
        container.add(2);
        container.add(3);
        container.add(4);
        container.add(8);
        container.add(6);
        container.add(8);
        container.add(9);*/

        quickSort(container,0,7);

        for (int i =0;i<container.size();i++)
        {
            System.out.println(container.get(i));
        }

        //http://www.algolist.net/Algorithms/Sorting/Quicksort




    }


    public static int partition(Vector<Integer> container, int left, int right)
    {
          int i = left, j = right;
          int tmp;


          int pivot = container.get(left);

          i++;

          while (i <= j) 
          {
                while ( container.get(i) < pivot)
                      i++;
                while ( container.get(j) > pivot)
                      j--;
                if (i <= j) 
                {
                      tmp =  container.get(i);

                      container.set(i, container.get(j));
                      container.set(j, tmp);

                      i++;
                      j--;
                }
          };

          tmp = container.get(left);

          container.set(left, container.get(i-1));
          container.set(i-1, tmp);

          return i-1;
    }

    public static void quickSort(Vector<Integer> container, int left, int right) 
    {
          int index = partition(container, left, right);
          if (left < index - 1)
                quickSort(container, left, index - 1);
          if (index+1 < right)
                quickSort(container, index+1, right);
    }


}

该算法适用于以下数字:{7,23,4,8,6,8,9}

但是,当我尝试对包含10000个数字的text file进行排序时,它不起作用

我在算法中做错了什么???

2 个答案:

答案 0 :(得分:1)

<强>已更新

首先,这句话:

quickSort(container,0,7);

应阅读:

quickSort(container,0,container.size()-1);

我不确定这是不是问题。现在让我们清理您的代码。

您的核心职能:

public static void quickSort(Vector<Integer> container, int left, int right) 
{
      int index = partition(container, left, right);
      if (left < index - 1)
            quickSort(container, left, index - 1);
      if (index+1 < right)
            quickSort(container, index+1, right);
}

对于带有索引的加/减1,似乎有一个错误。这看起来更合适:

public static void quickSort(Vector<Integer> container, int left, int right) 
{
      if (left < right)
      {
          int index = partition(container, left, right);
          quickSort(container, left, index);
          quickSort(container, index+1, right);
      }
}

你的分区功能也需要一些清理工作。我第一次发布它的时候搞砸了。现在我测试了它,我知道它可以工作。

public static int partition(Vector<Integer> container, int left, int right)
{
      int i = left-1;
      int j = right+1;

      int pivot = container.get(left);

      while (true) 
      {
          do
          {
              i++;
          } while (container.get(i) < pivot);

          do
          {
              j--;
          } while (container.get(j) > pivot);

          if (i < j) 
          {
                int tmp =  container.get(i);
                container.set(i, container.get(j));
                container.set(j, tmp);
          }
          else
          {
              break;
          }
      };
      return j;
}

答案 1 :(得分:0)

Selbie的答案确实按预期工作,但这不是我正在寻找的实施。我正在寻找快速排序的版本,其中枢轴与左标记之前的一个交换,导致在每次迭代后一个元素就位。

经过多次试验&amp;错误,我意识到我的原始算法有效,我不知道为什么当我第一次在问题中发布算法时它最初没有工作

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;



public class QuickSort 
{
    public static void main(String args[])
    {
        Vector<Integer> container = new Vector<Integer>();


        container.add(7);
        container.add(2);
        container.add(3);
        container.add(4); // 7 2 3 4 8 6 8 9
        container.add(8); // 7 2 3 4 6 8 8 9        
        container.add(6); // 6 2 3 4 7 8 8 9
        container.add(8);
        container.add(9);

        quickSort(container,0,container.size()-1);

        for (int i =0;i<container.size();i++)
        {
            System.out.println(container.get(i));
        }



    }


    public static int partition(Vector<Integer> container, int left, int right)
    {
          int i = left, j = right;
          int tmp;


          int pivot = container.get(left);

          i++;

          while (i <= j) 
          {
                while ( container.get(i) < pivot)
                      i++;
                while ( container.get(j) > pivot)
                      j--;
                if (i <= j) 
                {
                      tmp =  container.get(i);

                      container.set(i, container.get(j));
                      container.set(j, tmp);

                      i++;
                      j--;
                }
          };

          tmp = container.get(left);

          container.set(left, container.get(i-1));
          container.set(i-1, tmp);

          return i-1;





    }

    public static void quickSort(Vector<Integer> container, int left, int right) 
    {
          int index = partition(container, left, right);
          if (left < index - 1)
                quickSort(container, left, index - 1);
          if (index+1 < right)
                quickSort(container, index+1, right);



    }


}