使用递归方法对数组进行排序

时间:2018-10-11 02:56:43

标签: java arrays recursion

我正在尝试编写一个程序,其中用户将输入6个字符串,然后将使用递归方法以相反的字母顺序对数组进行排序。尽管有许多视频,阅读和尝试,但我仍然不明白这是一个概念。非常感谢任何支持和见识。谢谢。

import java.util.Arrays;
import java.util.Scanner;

public class SRecusion {


    public static void sort2 (String[] sort2) {
        int i;
        int min = 0;
        int max;

        for (i = 0; i <sort2.length -1; i++) {
            if (sort2[i].charAt(0)> sort2[i=1].charAt(0)) {
                sort2[i] = sort2[min];
            }
            else {
                min = (sort2(sort2[i-1]));
            }
        }
    }



    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String [] test = new String[6];
        Scanner scnr = new Scanner(System.in);
        String userEntry = "";

        for(int i = 0; i <= test.length - 1; i++) {
            System.out.println("Please enter a word:");
            test[i] = scnr.nextLine();
        }

        sort2(test);




            System.out.println("your list is" + Arrays.asList(test));
            System.out.println();

        }

}

2 个答案:

答案 0 :(得分:1)

排序是一个非常广泛的主题,因为有许多不同的排序方法(快速排序,合并排序等)。但是,非常基本且简单的排序方法是冒泡排序。尽管它不是最快的方法,但使用递归很容易理解和编码。

本质上,冒泡排序以2为对的元素进行迭代,如果两个元素的顺序错误,则交换它们。

例如,让我们使用气泡排序进行排序(3、2、5、4、1)。

2,3 ,5,4,1)首先,它将查看前两个元素是否需要交换它们。由于3大于2,它将交换它们。

(2, 3,5 ,4,1)接下来,将看3和5。由于3小于5,因此无需交换

(2,3, 4,5 ,1)现在看5和4并交换它们。

(2,3,4, 1,5 )最后,它查看5和1并交换它们。

现在从头开始,并重复整个过程。如果在迭代过程中恰好进行了0次交换,则排序结束。

如果您仍然有些困惑,请尝试观看有关冒泡排序的教程或访问此link

答案 1 :(得分:0)

因此,根据我在上面询问的原因,为什么需要递归排序算法在这里,我将尝试解释递归排序的工作原理。我花了一些时间弄清楚这个问题,因为我相信大多数初次接触它的人都会这样做。

public static void Qsort(int[] array, int start, int end)
{
    //find the current center of the whole or parital array part I am working on.
    int center = (start+end)/2;
    ///System.out.println("\n This is the center : " + center);
    int pivot, i, pivotplace;
    i = 0;
    pivot = 0;
    pivotplace = 0;
    //if start = end then we are at a single element.  just return to the previous iterative call.
    if(start == end)
    {
       // System.out.println("\n Inside base case return :");
        return;
    }
    //find the pivot value we are using.  using a 3 prong selection we are assured to at least get some type of median value and avoid the N^2 worst case.
    pivot = getpivot(array[start], array[center], array[end]); //gets median value of start, center and end values in the array.
   // System.out.println("\n pivotvalue is  : " + pivot);
    //find where the current pivot is located and swap it with the last element in the current portion of the array.
    if(array[start] == pivot)
    {
        //System.out.print("\n Inside pivot at start");
        swap(array, start, end);
    }
    else
    {
        if(array[center] == pivot)
        {
            //System.out.print("\n Inside pivot at center");
            swap(array, center, end);
        }
    }
    //due to iteration the pivot place needs to start at the passed in value of 'start' and not 0.
    pivotplace = start;
    //due to iteration the loop needs to go from the passed in value of start and not 0 and needs to go 
    //until it reaches the end value passed in.
    for(i = start; i < end; i++)
    {
        //if the current slot of the array is less than then pivot swap it with the current pivotplace holder
        //since the pivotplace keeps getting iterated up be each swap the final place of pivot place
        //is where the pivot will actually be swapped back to after the loop cpompletes.
        if(array[i] < pivot)
        {
            //System.out.print("\n Swapping");
            swap(array, i, pivotplace);
            pivotplace++;
        }
    }
    //loop is finished, swap the pivot into the spot it belongs in.
    swap(array, pivotplace, end);
//there are 2 cases for recursive iteration.
//The first is from the start to the slot before the pivot
if(start < pivotplace){Qsort(array, start, pivotplace-1);}
//the second is from the slot after the pivot to the end.
if(pivotplace+1 < end){Qsort(array, pivotplace+1, end);}

}

public static int getpivot(int a, int b, int c)
{
    if((a > b)  && (a < c))
    {
        return a;
    }
    if((b > a)  && (b < c))
    {
        return b;
    }
    return c;
}
public static void swap(int[] array, int posa, int posb)
{
    int temp;
    temp = array[posa];
    array[posa] = array[posb];
    array[posb] = temp;
}

这是我在编程类中编写的基本快速排序或递归排序。在处理少量字符串时,您可能不需要使用getpivot代码,但是如果您进行一些研究,由于递归树的平衡工作负载,您会发现使用3个可能的样本可以大大加快递归速度。