2016-02-26 57 views
0
for (int i = 1; i < data.Count; i++) 
{ 
    int j = i; 
    while (j > 0) 
    { 
     if (numarray[j - 1] > numarray[j]) 
     { 
      int temp = numarray[j - 1]; 
      numarray[j - 1] = numarray[j]; 
      numarray[j] = temp; 
      j--; 

     } 

     else 
      break; 
    } 
} 

有人可以幫助我確定什麼是上述代碼的排序算法?我知道冒泡排序不是很有效率。如果我要使用插入排序算法,我該如何改進上述代碼。謝謝!我想要一個有效的排序算法來排序數組

+1

它是冒泡排序,因爲最大數量像氣泡一樣上升。 – Valentin

+1

你的目標不太清楚。如果你只需要對數組進行排序 - 那麼'Array.Sort'方法就足夠了。如果出於某些教育原因需要實施插入排序 - 那麼,嘗試實施插入排序,並在具體問題遇到困難時回來。 –

+1

你想知道這種算法的哪種算法,或者你需要更高效的算法嗎?本例中的 – HimBromBeere

回答

4

只是這樣做:

Array.Sort(data); 
+3

你爲什麼使用'.ToList()'? '列表'執行'.Sort()' – xanatos

+2

以及Eshqna提到了它的一個數組並且沒有列表 –

+1

爲什麼還要用List來干擾?爲什麼不只是'Array.Sort'? –

1

.NET有數組默認的排序方案。 Array.Sort。默認情況下,.NET實現了一個在O(n)和O(n log n)之間排序的Quicksort algorithm。 (這比bubble sort快得多)。

你可以通過做Array.Sort(your_array)來使用它,或者如果你需要更復雜的東西(比如排序對象或者反向排序),那麼就會有一個過載,它會帶上一個IComparer對象。

3

最有效的方式應該是Array.Sort(data);其使用快速排序

一些其他排序算法:

冒泡

public static void BubbleSort<T>(this List<T> source) where T : IComparable<T> 
{ 
    IComparer<T> comparer = Comparer<T>.Default; 
    for (int i = (source.Count - 1); i >= 0; i--) 
    { 
     for (int j = 1; j <= i; j++) 
     { 
      if (comparer.Compare(source[j - 1], source[j]) > 0) 
      { 
       var temp = source[j - 1]; 
       source[j - 1] = source[j]; 
       source[j] = temp; 
      } 
     } 
    } 
} 

插入排序

public static void InsertionSort<T>(this List<T> source) where T : IComparable<T> 
{ 
    IComparer<T> comparer = Comparer<T>.Default; 
    int j; 
    for (int i = 1; i < source.Count; i++) 
    { 
     var index = source[i]; 
     j = i; 
     while ((j > 0) && comparer.Compare(source[j - 1], index) > 0) 
     { 
      source[j] = source[j - 1]; 
      j = j - 1; 
     } 
     source[j] = index; 
    } 
} 

堆排序

public static void HeapSort<T>(this List<T> source) where T : IComparable<T> 
{ 
    int heapSize = source.Count; 
    for (int p = (heapSize - 1)/2; p >= 0; p--) 
    { 
     HeapSort_sub(source, heapSize, p); 
    } 
    for (int i = source.Count - 1; i > 0; i--) 
    { 
     var temp = source[i]; 
     source[i] = source[0]; 
     source[0] = temp; 
     heapSize--; 
     HeapSort_sub(source, heapSize, 0); 
    } 
} 

private static void HeapSort_sub<T>(List<T> source, int heapSize, int index) 
{ 
    IComparer<T> comparer = Comparer<T>.Default; 
    int left = (index + 1) * 2 - 1; 
    int right = (index + 1) * 2; 
    int largest = 0; 
    if (left < heapSize && comparer.Compare(source[left], source[index]) > 0) 
    { 
     largest = left; 
    } 
    else 
    { 
     largest = index; 
    } 
    if (right < heapSize && comparer.Compare(source[right], source[largest]) > 0) 
    { 
     largest = right; 
    } 
    if (largest != index) 
    { 
     var temp = source[index]; 
     source[index] = source[largest]; 
     source[largest] = temp; 
     HeapSort_sub(source, heapSize, largest); 
    } 
} 

快速排序

public static void QuickSort<T>(this List<T> source) where T : IComparable<T> 
{ 
    QuickSort_sub(source, 0, source.Count - 1); 
} 

private static void QuickSort_sub<T>(List<T> source, int left, int right) 
{ 
    IComparer<T> comparer = Comparer<T>.Default; 
    int i = left, j = right; 
    var pivot = source[(left + right)/2]; 
    while (i <= j) 
    { 
     while (comparer.Compare(source[i], pivot) < 0) 
     { 
      i++; 
     } 
     while (comparer.Compare(source[j], pivot) > 0) 
     { 
      j--; 
     } 
     if (i <= j) 
     { 
      var tmp = source[i]; 
      source[i] = source[j]; 
      source[j] = tmp; 
      i++; 
      j--; 
     } 
    } 
    if (left < j) 
    { 
     QuickSort_sub(source, left, j); 
    } 
    if (i < right) 
    { 
     QuickSort_sub(source, i, right); 
    } 
} 

臭皮匠排序

public static void StoogeSort<T>(this List<T> L) where T : IComparable 
{ 
    StoogeSortSub(L, 0, L.Count - 1); 
} 

private static void StoogeSortSub<T>(List<T> L, int i, int j) where T : IComparable 
{ 
    if (L[j].CompareTo(L[i]) < 0) 
    { 
     T tmp = L[i]; 
     L[i] = L[j]; 
     L[j] = tmp; 
    } 
    if (j - i > 1) 
    { 
     int t = (j - i + 1)/3; 
     StoogeSortSub(L, i, j - t); 
     StoogeSortSub(L, i + t, j); 
     StoogeSortSub(L, i, j - t); 
    } 
} 

選擇排序

public static void SelectionSort<T>(this List<T> source) where T : IComparable<T> 
{ 
    IComparer<T> comparer = Comparer<T>.Default; 
    int min; 
    for (int i = 0; i < source.Count - 1; i++) 
    { 
     min = i; 
     for (int j = i + 1; j < source.Count; j++) 
     { 
      if (comparer.Compare(source[j], source[min]) < 0) 
      { 
       min = j; 
      } 
     } 
     var temp = source[i]; 
     source[i] = source[min]; 
     source[min] = temp; 
    } 
} 

雞尾酒排序

public static void CocktailSort<T>(this List<T> A) where T : IComparable<T> 
{ 
    IComparer<T> comparer = Comparer<T>.Default; 
    bool swapped; 
    do 
    { 
     swapped = false; 
     for (int i = 0; i <= A.Count - 2; i++) 
     { 
      if (comparer.Compare(A[i] , A[i + 1])> -1) 
      { 
       T temp = A[i]; 
       A[i] = A[i + 1]; 
       A[i + 1] = temp; 
       swapped = true; 
      } 
     } 
     if (!swapped) 
     { 
      break; 
     } 
     swapped = false; 
     for (int i = A.Count - 2; i >= 0; i--) 
     { 
      if (comparer.Compare(A[i], A[i + 1]) > -1) 
      { 
       T temp = A[i]; 
       A[i] = A[i + 1]; 
       A[i + 1] = temp; 
       swapped = true; 
      } 
     } 
    } while (swapped); 
} 

GnomeSort

public static void GnomeSort<T>(this List<T> a) where T : IComparable<T> 
{ 
    IComparer<T> comparer = Comparer<T>.Default; 
    int i = 1, j = 2; 

    while (i < a.Count) 
    { 
     if (comparer.Compare(a[i - 1] ,a[i])<1) 
     { 
      i = j; 
      j++; 
     } 
     else 
     { 
      T tmp = a[i - 1]; 
      a[i - 1] = a[i]; 
      a[i] = tmp; 
      i -= 1; 
      if (i == 0) 
      { 
       i = 1; 
       j = 2; 
      } 
     } 
    } 
} 

希爾排序

public static void ShellSort<T>(this List<T> source) where T : IComparable<T> 
{ 
    IComparer<T> comparer = Comparer<T>.Default; 
    int j, increment; 
    increment = 3; 
    while (increment > 0) 
    { 
     for (int i = 0; i < source.Count; i++) 
     { 
      j = i; 
      var temp = source[i]; 
      while ((j >= increment) && comparer.Compare(source[j - increment], temp) > 0) 
      { 
       source[j] = source[j - increment]; 
       j = j - increment; 
      } 
      source[j] = temp; 
     } 
     if (increment/2 != 0) 
     { 
      increment = increment/2; 
     } 
     else if (increment == 1) 
     { 
      increment = 0; 
     } 
     else 
     { 
      increment = 1; 
     } 
    } 
}