簡體   English   中英

使用遞歸方法對數組進行排序

[英]sort array using recursive method

我正在嘗試編寫一個程序,其中用戶將輸入 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 對遍歷元素,如果它們的順序錯誤,則交換兩個元素。

例如,讓我們使用冒泡排序對 (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 次交換,則排序結束。

如果您仍然有點困惑,請嘗試觀看有關冒泡排序的教程或訪問此鏈接

因此,根據我上面的問題,為什么需要遞歸排序算法在這里,我將嘗試解釋遞歸排序的工作原理。 我花了一些時間才弄清楚,因為我相信大多數第一次接觸它的人都會這樣做。

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 樣本由於遞歸樹的平衡工作負載而大大加快了遞歸.

在 kotlin 中使用遞歸對數組進行排序

    fun main() {
        print(sortArray(arrayListOf(1,3,2,6,8,3)))
    }
    
    fun sortArray(arr: MutableList<Int>): MutableList<Int>{
        if(arr.size==1) {
            return arr
        }
        val lastValue = arr.last()
        arr.removeLast()
        sortArray(arr)
        insert(arr, lastValue)
        return arr
    }
    
    fun insert (arr: MutableList<Int>, value: Int): MutableList<Int> {
        if(arr.size == 0 || arr.last() < value) {
            arr.add(value)
            return arr
        }
        val lastValue = arr.last()
        arr.removeLast()
        insert(arr, value)
        arr.add(lastValue)
        return arr
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM