Java中打亂一個數組的2種公平演算法分享_java

來源:互聯網
上載者:User

公平演算法,打亂數組

這是前幾天面試的時候遇見的一道題目,看到這個題首先想到了洗牌程式:

方法一:洗牌程式原理

在java.util包中的Collections類中的 shuffle方法,現在手工實現以下代碼如下:

package test.ms;import java.util.Random;public class Redistribute2 {public static void main(String[] args) {//define the array int[] s = {1,5,4,3,6,9,8,7,0,8,5,6,7,2};// before redistribute outputSystem.out.println("before redistribute:");for(int i = 0 ; i<s.length; i++){System.out.print(s[i]+" ");}// invoke the method shuffle(s,new Random());System.out.println();// after redistribute outputSystem.out.println("after redistribute:");for(int i = 0 ; i<s.length; i++){System.out.print(s[i]+" ");}}  // using the random get the random numberpublic static  void  shuffle(int[] array, Random random){for(int i = array.length; i >= 1; i--){swap(array,i-1,random.nextInt(i));}}// the two number swap  in the arraypublic static void swap(int[] array, int i , int j){ int temp = array[i];array[i] = array[j];array[j] = temp; }}


swap方法用於交換數組中的兩個數,  shuffle方法 用於 根據隨機源 產生的隨機數進行交換。
輸出結果如下:

before redistribute:1 5 4 3 6 9 8 7 0 8 5 6 7 2 after redistribute:9 8 7 8 0 6 1 6 5 5 2 3 7 4 


方法二:產生隨機索引交換

該方法利用Set集合的特性:Set集合中的資料不重複,產生數組的索引,根據產生的索引進行交換資料。

實現方式如下:

package test.ms;import java.util.Iterator;import java.util.LinkedHashSet;import java.util.Random;import java.util.Set;public class Redistribute {public static void main(String[] args) {int[] s = {1,5,4,3,6,9,8,7,0,8,5,6,7,2};redistribute(s);}public static void  redistribute(int[] s){Random  random = new Random();Set<Integer> set = new LinkedHashSet<Integer>();// redistribute the  indexwhile(true){int t =random.nextInt(s.length);set.add(t);if(set.size()== s.length)break;}System.out.println("before redistribute:");for(int i = 0 ; i<s.length; i++){System.out.print(s[i]+" ");}System.out.println();System.out.println("redistribute the index ");System.out.println(set);int [] out = new int[s.length];int count = 0;for(Iterator<Integer> iterator = set.iterator(); iterator.hasNext();){out[count] = s[iterator.next()];count++;}// out the result;System.out.println("after redistribute:");for(int i = 0 ; i<s.length; i++){System.out.print(out[i]+" ");}}}

這個方法首先產生索引,然後根據新索引進行資料交換,代碼都寫在main方法裡了,不是太好。

產生結果如下:

before redistribute:1 5 4 3 6 9 8 7 0 8 5 6 7 2 redistribute the index [6, 2, 9, 1, 10, 5, 11, 4, 12, 3, 7, 8, 0, 13]after redistribute:8 4 8 5 5 9 6 6 7 3 7 0 1 2 


關於隨機數的產生,用了java類中的隨機數的產生的工具類,這個隨機類需要單獨研究一下。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.