Search code examples
c#bubble-sort

Bubble sort reversing entire list after 3 inputs


So I'm trying to sort a list of books that have been added to an array alphabetically however, whenever I input the third book, the list flips and sorts the list in unalphabetical order.

If anyone knows why this is, please comment and let me know, my code is below.

The sort to determine if two indexes need to be swapped

private void bookSort()
{
    for (int y = 0; y < 20; y++)
    {
        for (int x = 0; x < bookPTR - 1; x++)
        {
            if (string.Compare(books[x].GStitle, books[x + 1].GStitle) > 0)
            {
                bookSwapRoutine(books[x]);
            }
        }
    }
}

The swap itself

private void bookSwapRoutine(Book book, int x = 0)
{
    string tempString = books[x].GStitle;
    books[x].GStitle = books[x + 1].GStitle;
    books[x + 1].GStitle = tempString;

    int tempInt = books[x].GSisbn;
    books[x].GSisbn = books[x + 1].GSisbn;
    books[x + 1].GSisbn = tempInt;

    tempString = books[x].GSauthor;
    books[x].GSauthor = books[x + 1].GSauthor;
    books[x + 1].GSauthor = tempString;

    tempString = books[x].GSpublisher;
    books[x].GSpublisher = books[x + 1].GSpublisher;
    books[x + 1].GSpublisher = tempString;

    double tempDouble = books[x].GSprice;
    books[x].GSprice = books[x + 1].GSprice;
    books[x + 1].GSprice = tempDouble;

    tempString = books[x].GSdate;
    books[x].GSdate = books[x + 1].GSdate;
    books[x + 1].GSdate = tempString;
}

Solution

  • Because of this place. That function call always swap books at zero index and first index because of default parameter x = 0.

    bookSwapRoutine(books[x]);
    

    You should call it like.

    bookSwapRoutine(books[x], x);
    

    This will swap books[x] and books[x + 1] for you.

    If you want just sort your books by GStitle in alphabeticall order you can call.

    Array.Sort(books, (x, y) => string.Compare(x.GStitle, y.GStitle, StringComparison.InvariantCulture));
    

    Here all code, with correct bubble sort, if it will help you.

    public static void Main()
    {
        var books = new Book[]
        {
            new Book() {GStitle = "E"},
            new Book() {GStitle = "D"},
            new Book() {GStitle = "C"},
            new Book() {GStitle = "B"},
            new Book() {GStitle = "A"}
        };
    
        Console.WriteLine("Before sort.");
        foreach (var book in books)
        {
            Console.WriteLine(book.GStitle);
        }
    
        Array.Sort(books, (x, y) => string.Compare(x.GStitle, y.GStitle, StringComparison.InvariantCulture));
        //BookSort(books);
    
        Console.WriteLine("After sort.");
        foreach (var book in books)
        {
            Console.WriteLine(book.GStitle);
        }
    }
    
    public class Book
    {
        public string GStitle { get; set; }
    }
    
    public static void BookSort(Book[] books)
    {
        for (int y = 0; y < books.Length; y++)
        {
            for (int x = 0; x < books.Length - 1 - y; x++)
            {
                if (string.Compare(books[x].GStitle, books[x + 1].GStitle, StringComparison.InvariantCulture) > 0)
                {
                    var temp = books[x];
                    books[x] = books[x + 1];
                    books[x + 1] = temp;
                }
            }
        }
    }