.net版 字串陣列桶排序演算法
現要將字串陣列按首字母做排序,如:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->string[] sortStr = new string[] {"Jack","John","Ja","Bob","Bay","Candy","Enk","Fuk","Amy","Day","Mum","Num","Ju","Fee" };
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->string[] sortStr = new string[] {"Jack","John","Ja","Bob","Bay","Candy","Enk","Fuk","Amy","Day","Mum","Num","Ju","Fee" };
於是想到了利用桶排序的方法
首先要定義一個陣列包含所有的首字母,如:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->string[] strArray = new string[] {"A","B","C","D","E","F","G","H","I","J","K","L","M","N" };
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->string[] strArray = new string[] {"A","B","C","D","E","F","G","H","I","J","K","L","M","N" };
因為有重複的首字母存在,所以要定義一個二維陣列來存放相同首字母的陣列元素,其中行數就是strArray.Length,而列數就是sortStr.Length,如:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->string[,] finaArr = new string[strArray.Length, sortStr.Length];
這裡還需要一個準備工作,就是取每個元素的首字母,如:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->string[,] finaArr = new string[strArray.Length, sortStr.Length];
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->private string GetFirstLetter(string str){
if (string.IsNullOrEmpty(str)) return "";
return str.Substring(0, 1);
}
好了,現在可以開始排序演算法了,思路就是將陣列sortStr中的每一個元素中的首字母與陣列strArray進行比較,然後存入陣列finaArr相應的行中,如:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->private string GetFirstLetter(string str){
if (string.IsNullOrEmpty(str)) return "";
return str.Substring(0, 1);
}
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->foreach (string s in sortStr) {
InputElement(s, strArray, finaArr);
}
private void InputElement(string sortStrElement, string[] strArray, string[,] finaArr) {
string temp = GetFirstLetter(sortStrElement);
for (int i = 0; i < strArray.Length; i++) {
if (temp == strArray[i]) {
if (string.IsNullOrEmpty(finaArr[i, 0]))
finaArr[i, 0] = sortStrElement;
else {
int j = 0;
while (!string.IsNullOrEmpty(finaArr[i, j]))
j++;
finaArr[i,j] = sortStrElement;
}
break;
}
}
}
做到這裡,排序就完成了,只需輸出就可以了,如:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->foreach (string s in sortStr) {
InputElement(s, strArray, finaArr);
}
private void InputElement(string sortStrElement, string[] strArray, string[,] finaArr) {
string temp = GetFirstLetter(sortStrElement);
for (int i = 0; i < strArray.Length; i++) {
if (temp == strArray[i]) {
if (string.IsNullOrEmpty(finaArr[i, 0]))
finaArr[i, 0] = sortStrElement;
else {
int j = 0;
while (!string.IsNullOrEmpty(finaArr[i, j]))
j++;
finaArr[i,j] = sortStrElement;
}
break;
}
}
}
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->StringBuilder bui = new StringBuilder();
for (int i = 0; i < finaArr.GetLength(0); i++){
if (string.IsNullOrEmpty(finaArr[i, 0]))
continue;
int j = 0;
while (!string.IsNullOrEmpty(finaArr[i, j])) {
bui.Append(finaArr[i, j]).Append("
");
j++;
}
}
return bui.ToString();
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->StringBuilder bui = new StringBuilder();
for (int i = 0; i < finaArr.GetLength(0); i++){
if (string.IsNullOrEmpty(finaArr[i, 0]))
continue;
int j = 0;
while (!string.IsNullOrEmpty(finaArr[i, j])) {
bui.Append(finaArr[i, j]).Append("
");
j++;
}
}
return bui.ToString();
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-545466/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- iOS開發·必會的演算法操作:字串陣列排序+模型物件陣列排序iOS演算法字串陣列排序模型物件
- 基數排序--陣列模擬桶結構排序陣列
- PHP 陣列排序(複雜字串)PHP陣列排序字串
- 排序演算法__桶排序排序演算法
- java中排序一個字串陣列Java排序字串陣列
- 排序演算法之——桶排序排序演算法
- 演算法合併排序陣列演算法排序陣列
- 常用排序演算法之桶排序排序演算法
- PHP 字串陣列按照拼音排序的問題PHP字串陣列排序
- 陣列排序陣列排序
- 【演算法-初級-陣列】刪除排序陣列中的重複項(多語言版實現)演算法陣列排序
- 演算法--陣列氣泡排序和選擇排序原理分析演算法陣列排序
- 陣列的排序陣列排序
- 物件陣列排序物件陣列排序
- JavaScript 陣列排序JavaScript陣列排序
- js陣列排序JS陣列排序
- 陣列和字串陣列字串
- 快看!這是一道送分演算法題,基礎陣列字串排序問題演算法陣列字串排序
- js將陣列中的字串執行字母表排序JS陣列字串排序
- 資料結構與演算法——歸併排序: 陣列&連結串列&遞迴&非遞迴解法全家桶資料結構演算法排序陣列遞迴
- 桶排序排序
- 為什麼處理排序陣列比未排序陣列快排序陣列
- 演算法-兩個排序陣列的中位數演算法排序陣列
- 多維陣列排序陣列排序
- 陣列氣泡排序陣列排序
- 陣列選擇排序陣列排序
- js陣列排序整理JS陣列排序
- javascript 陣列快速排序JavaScript陣列排序
- 陣列多重排序陣列排序
- c# 陣列排序C#陣列排序
- 二維陣列排序陣列排序
- 陣列二:使用陣列可變函式為陣列排序陣列函式排序
- 複習資料結構:排序演算法(七)——桶排序資料結構排序演算法
- ***PHP陣列排序+php二維陣列排序方法(PHP比較器)PHP陣列排序
- String字串,陣列字串陣列
- 字元陣列與字串字元陣列字串
- C++陣列 字串C++陣列字串
- 資料結構與演算法——桶排序資料結構演算法排序