C 陣列排序方法

2022-08-28 03:15:14 字數 2363 閱讀 8590

在c#中常用的陣列排序的方法有:選擇排序法、氣泡排序法、插入排序法和希爾排序法等。

一、選擇排序法

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace test

;console.write("陣列排序前的結果為:");

foreach (int n in array)

", n + " ");

}console.writeline();

int min;

for (int i = 0; i < array.length - 1; i++)

int t = array[min];

array[min] = array[i];

array[i] = t;

}console.write("陣列排序後的結果為:");

foreach (int n in array)

", n + " ");

}console.readline();}}

}執行結果:

陣列排序前的結果為:2 18 9 26 3 7 5 10

陣列排序後的結果為:2 3 5 7 9 10 18 26

二、氣泡排序法

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace test

;console.write("陣列排序前的結果為:");

foreach (int n in array)

", n + " ");

}console.writeline();

for (int i = 0; i < array.length; i++)}}

console.write("陣列排序後的結果為:");

foreach (int n in array)

", n + " ");

}console.readline();}}

}執行結果:

陣列排序前的結果為:2 18 9 26 3 7 5 10

陣列排序後的結果為:26 18 10 9 7 5 3 2

三、插入排序法

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace test

;console.write("陣列排序前的結果為:");

foreach (int n in array)

", n + " ");

}console.writeline();

for (int i = 1; i < array.length; i++)

array[j] = t;

}console.write("陣列排序後的結果為:");

foreach (int n in array)

", n + " ");

}console.readline();}}

}執行結果:

陣列排序前的結果為:2 18 9 26 3 7 5 10

陣列排序後的結果為:2 3 5 7 9 10 18 26四、希爾排序法

希爾排序是將組分段,然後進行插入排序。

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace test

;console.write("陣列排序前的結果為:");

foreach (int n in array)

", n + " ");

}console.writeline();

int inc;

for (inc = 1; inc <= array.length / 9; inc = 3 * inc + 1) ;

for (; inc > 0; inc /= 3)

array[j - 1] = t;}}

console.write("陣列排序後的結果為:");

foreach (int n in array)

", n + " ");

}console.readline();}}

}執行結果:

陣列排序前的結果為:2 18 9 26 3 7 5 10

陣列排序後的結果為:2 3 5 7 9 10 18 26

C 陣列排序方法

using system using system.collections.generic using system.linq using system.text using system.threading.tasks namespace 1209 陣列排序方法 console.write 陣列排...

C 物件陣列排序方法

實現陣列的排序 乙個排序的類,用了幾種方式實現的。1using system 23namespace datastruct 423 24 25 2627 28 插入排序法 29 30 31 public static void insertionsort int list 32 42 list j ...

C 陣列排序的方法

using system namespace datastruct 插入排序法 public static void insertionsort int list list j t 選擇排序法 public static void selectionsort int list intt list m...