3

我正在尝试将选择排序用于图书库,以便按字母顺序对它们进行排序,但是我无法让它工作。

选择排序(库);不起作用,但 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]);
            }
        }
    }
}
4

2 回答 2

3

您的方法需要 a string[],但您要给它 aBook[]

您需要更改实现SelectionSort以支持 Comparable Collections,并Book实现IComparable接口。

public class Book : IComparable
{
    // Implementation

    public int CompareTo(Book otherBook)
    {
        // Implementation
    }
}

static public void SelectionSort<T>(IList<T> a) where T : IComparable
{
    // Implementation
}

这种方法的优点是,您不必为SelectionSort要排序的每种对象集合创建不同的版本。

于 2013-03-13T15:44:24.670 回答
3

因为SelectionSort将 type 数组作为参数,string而您将 type 数组传递给它BookSelectionSort(titles)有效,因为titles 是一个类型的数组string

您需要编写一个采用类型数组的方法Book

static public void SelectionSort(Book[] books)

如果您还没有这样做,您可能需要CompareTo在您的类中定义一个方法Book,以便您的排序算法可以弄清楚如何对书籍进行排序。

于 2013-03-13T15:39:15.810 回答