为什么我不能选择排序这个库?

时间:2013-03-13 15:36:28

标签: c# arrays sorting selection

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

选择排序(库);不起作用,但SelectionSort(标题);有没有想法?谢谢:))

以下是完整代码:

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]);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

因为SelectionSortstring类型的数组作为参数,并且您传递的是类型为Book的数组。 SelectionSort(titles)有效,因为titles是string类型的数组。

您需要编写一个采用Book

类型数组的方法
static public void SelectionSort(Book[] books)

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

答案 1 :(得分:3)

您的方法需要string[],但您需要Book[]

您需要更改SelectionSort的实施以支持可比较收藏集,并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