猿问

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

我正在尝试编写一个程序,其中用户将输入 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();


        }


}


MM们
浏览 215回答 2
2回答

月关宝盒

排序是一个非常广泛的主题,因为有许多不同的排序方法(快速排序、归并排序等)。但是,冒泡排序是一个非常基本且简单的排序方法。虽然它不是最快的,但使用递归很容易理解和编码。本质上,冒泡排序会迭代成对 2 的元素,如果两个元素的顺序错误,则交换它们。例如,让我们使用冒泡排序对 (3, 2, 5, 4, 1) 进行排序。(&nbsp;2, 3&nbsp;, 5, 4, 1) 首先,它会查看前两个元素是否需要交换它们。由于 3 大于 2,它会交换它们。(2,&nbsp;3, 5&nbsp;, 4, 1) 接下来看 3 和 5。因为 3 小于 5,所以不需要交换(2, 3,&nbsp;4, 5&nbsp;, 1) 现在查看 5 和 4 并交换它们。(2, 3, 4,&nbsp;1, 5&nbsp;) 最后,它查看 5 和 1 并将它们交换。现在从头开始,重复整个过程。如果在迭代期间恰好进行了 0 次交换,则排序结束。如果您仍然有点困惑,请尝试观看有关冒泡排序的教程或访问此链接。

牛魔王的故事

所以从我上面问的为什么你需要递归排序算法这里我将尝试解释递归排序是如何工作的。我花了一些时间才弄明白,因为我相信大多数第一次接触它的人都会这样做。public static void Qsort(int[] array, int start, int end){&nbsp; &nbsp; //find the current center of the whole or parital array part I am working on.&nbsp; &nbsp; int center = (start+end)/2;&nbsp; &nbsp; ///System.out.println("\n This is the center : " + center);&nbsp; &nbsp; int pivot, i, pivotplace;&nbsp; &nbsp; i = 0;&nbsp; &nbsp; pivot = 0;&nbsp; &nbsp; pivotplace = 0;&nbsp; &nbsp; //if start = end then we are at a single element.&nbsp; just return to the previous iterative call.&nbsp; &nbsp; if(start == end)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;// System.out.println("\n Inside base case return :");&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; //find the pivot value we are using.&nbsp; using a 3 prong selection we are assured to at least get some type of median value and avoid the N^2 worst case.&nbsp; &nbsp; pivot = getpivot(array[start], array[center], array[end]); //gets median value of start, center and end values in the array.&nbsp; &nbsp;// System.out.println("\n pivotvalue is&nbsp; : " + pivot);&nbsp; &nbsp; //find where the current pivot is located and swap it with the last element in the current portion of the array.&nbsp; &nbsp; if(array[start] == pivot)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //System.out.print("\n Inside pivot at start");&nbsp; &nbsp; &nbsp; &nbsp; swap(array, start, end);&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(array[center] == pivot)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //System.out.print("\n Inside pivot at center");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swap(array, center, end);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; //due to iteration the pivot place needs to start at the passed in value of 'start' and not 0.&nbsp; &nbsp; pivotplace = start;&nbsp; &nbsp; //due to iteration the loop needs to go from the passed in value of start and not 0 and needs to go&nbsp;&nbsp; &nbsp; //until it reaches the end value passed in.&nbsp; &nbsp; for(i = start; i < end; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //if the current slot of the array is less than then pivot swap it with the current pivotplace holder&nbsp; &nbsp; &nbsp; &nbsp; //since the pivotplace keeps getting iterated up be each swap the final place of pivot place&nbsp; &nbsp; &nbsp; &nbsp; //is where the pivot will actually be swapped back to after the loop cpompletes.&nbsp; &nbsp; &nbsp; &nbsp; if(array[i] < pivot)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //System.out.print("\n Swapping");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swap(array, i, pivotplace);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pivotplace++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; //loop is finished, swap the pivot into the spot it belongs in.&nbsp; &nbsp; swap(array, pivotplace, end);//there are 2 cases for recursive iteration.//The first is from the start to the slot before the pivotif(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){&nbsp; &nbsp; if((a > b)&nbsp; && (a < c))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return a;&nbsp; &nbsp; }&nbsp; &nbsp; if((b > a)&nbsp; && (b < c))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return b;&nbsp; &nbsp; }&nbsp; &nbsp; return c;}public static void swap(int[] array, int posa, int posb){&nbsp; &nbsp; int temp;&nbsp; &nbsp; temp = array[posa];&nbsp; &nbsp; array[posa] = array[posb];&nbsp; &nbsp; array[posb] = temp;}这是我在编程课程中编写的基本快速排序或递归排序。在处理一小组字符串时,您可能不需要使用 getpivot 代码,但是如果您进行一些研究,您会发现使用可能的 3 个样本大大加快了递归速度,因为递归树的工作负载均衡.
随时随地看视频慕课网APP

相关分类

Java
我要回答