我正在尝试将选择排序用于图书库,以便按字母顺序对它们进行排序,但是我无法让它工作。
选择排序(库);不起作用,但 SelectionSort(titles); 有,有什么想法吗?谢谢 :)
这是完整的代码:
using System;
using System.Collections.Generic;
using System.Text;
namespace BookSortingEx
{
class Program
{
static void swap<T>(ref T x, ref T y)
{
//swapcount++;
T temp = x;
x = y;
y = temp;
}
static void printArray(string[] a)
{
for (int i = 0; i < a.Length; i++)
{
Console.Write(a[i] + ",");
}
Console.WriteLine();
}
static bool IsInOrder<T>(T[] a) where T : IComparable
{
for (int i = 0; i < a.Length - 1; i++)
{
if (a[i].CompareTo(a[i + 1]) > 0)
return false;
}
return true;
}
static void Main(string[] args)
{
string[] array1 = { "Fred", "Zoe", "Angela", "Umbrella", "Ben" };
string[] titles = {"Writing Solid Code",
"Objects First","Programming Gems",
"Head First Java","The C Programming Language",
"Mythical Man Month","The Art of Programming",
"Coding Complete","Design Patterns",
"Problem Solving in Java"};
string[] authors = { "Maguire", "Kolling", "Bentley", "Sierra", "Richie", "Brooks", "Knuth", "McConnal", "Gamma", "Weiss" };
string[] isbns = { "948343", "849328493", "38948932", "394834342", "983492389", "84928334", "4839455", "21331322", "348923948", "43893284", "9483294", "9823943" };
Book[] library = new Book[10];
for (int i = 0; i < library.Length; i++)
{
library[i] = new Book(isbns[i], titles[i], authors[i]);
}
**DOESNT WORK - // SelectionSort(library);**
SelectionSort(titles);
printArray(titles);
foreach (Book book in library)
{
Console.WriteLine(" {0} ", book);
}
Console.WriteLine();
Console.ReadKey();
}
static public void SelectionSort(string[] a)
{
for (int i = 0; i < a.Length - 1; i++)
{
int smallest = i;
for (int j = i + 1; j < a.Length; j++)
{
if (a[j].CompareTo(a[smallest]) < 0)
smallest = j;
}
swap(ref a[i], ref a[smallest]);
}
}
}
}