2017-04-17 25 views
-1

我有一個作業使用鏈排序和我必須從初始陣列取數的生長順序,並將它們表示我們的結果(C#)鋼絞線排序在c#使用數組只

喜歡這個陣列中合併在一起一個http://imgur.com/nQFzJw7

到目前爲止,我沒有類似的東西

public static int[] Str(int[] a) 
    { 
     int i, j, x ,temp,k=0,count=1; 
     int size = a.Length; 
     int len = a.Length; 
     Strand = new int[size]; 
     Merged = new int[size]; 




     for (i = k; i < size; i++) 
     { 
      x = a[i]; 
      Strand[0] = x; 

      for (i = k; i < size; i++)  //checking if there's a bigger int than the first one 
      { 
       if (a[i] > x) 
       { 
        x = a[i]; 

       } 

      } 

      for (i = k; i < len; i++)  
      { 
       if (a[i] == x)  // checking if the max appears more than 1 time 
       { 

        temp = a[i]; 
        a[i] = a[len]; 
        a[len] = temp; 
        len--;   //swaps the max numbers to the last position 

        Strand[count] = x; 
        count++; 





       } 

      } 
      for (i = 0; i < count; i++) // cant find a way to put in the final merged and sorted array 
      { 

      } 
      count = 1; 
      k++; 
     } 

有什麼建議?

回答

0

你總是需要在第一元件提取到strand

strand[0] = a[0] 
count = 1 

然後,你需要合適的元件提取到strand,移陣列的其餘部分

for i = 1 to size - 1 
    if a[i] >= strand[count - 1] 
     strand[count++] = a[i] 
    else 
     a[i - count] = a[i] 
size = size - count 

然後,你需要合併電流strandmerged - 從MergeSort尋找合併程序

重複這些步驟直到size變爲0