Home > Software engineering >  sorting a Salesteam using bubblesort
sorting a Salesteam using bubblesort

Time:11-22

I need help with the following program:

What I want to do is sorting a sales team after how many sales they have made (AntalSåldartiklar). The first one in the list is the one who has sold the least and in the end the one who has sold the most. The rest is in between. And yes I need to use Bubblesort. Had a few problems along the way but I think I fixed all of those. My problem now is that the program don't sort anything att all. Please explain how I can fix it like I am a little child. I am a complete newbie when it comes to programming

using System;
using System.IO;


namespace Inlämingsuppgift2ConsoleApplication
{
    class Program
    {
      struct Säljare : IComparable<Säljare>
        {
            public String Namn;
            public String Personnummer;
            public String Distrikt;
            public int AntalSåldaArtiklar;
            public int CompareTo(Säljare other)

            {
                  return AntalSåldaArtiklar - other.AntalSåldaArtiklar;
            }
          }


            [STAThread]

            static void Main(string[] args)
            {
                System.Console.Write("Hur många säljare vill du registrera?:");
                string antalSäljare = System.Console.ReadLine();
                System.Console.WriteLine("Registrera säljare:");
                int Antalsäljare = int.Parse(antalSäljare);

                Säljare[] säljare = new Säljare[Antalsäljare];

                int nivå1 = 0;
                int nivå2 = 0;
                int nivå3 = 0;
                int nivå4 = 0;

           void BubbleSort(Säljare[] list) where Säljare : IComparable<Säljare>

            {

                for (int i = 0; i < list.Length - 1; i  )
                {

                    for (int j = 1; j < list.Length - 1 - i; j  )
                    {
                        if (list[j].CompareTo(list[j - 1]) < 0)
                        {
                            Säljare tmp = list[j   1];
                            list[j   1] = list[j];
                            list[j] = tmp; 
                        }
                    }

                }
            }

            for (int i = 0; i < Antalsäljare; i  )

                {

                    System.Console.Write(" Ange Namn: ");
                    säljare[i].Namn = System.Console.ReadLine();
                    System.Console.Write(" Ange Personnummer: ");
                    säljare[i].Personnummer = System.Console.ReadLine();
                    System.Console.Write(" Ange Distrikt: ");
                    säljare[i].Distrikt = System.Console.ReadLine();
                    System.Console.Write(" Ange antal Artiklar: ");
                    säljare[i].AntalSåldaArtiklar = int.Parse(System.Console.ReadLine());
                    Console.WriteLine("\n");

                }

                for (int j = 0; j < Antalsäljare; j  )
                {

                    if (säljare[j].AntalSåldaArtiklar < 50)
                    {

                        nivå1  ;
                    }

                    else if (säljare[j].AntalSåldaArtiklar >= 50 && säljare[j].AntalSåldaArtiklar < 100)
                    {

                        nivå2  ;
                    }

                    else if (säljare[j].AntalSåldaArtiklar >= 100 && säljare[j].AntalSåldaArtiklar < 200)

                    {

                        nivå3  ;
                    }

                    else if (säljare[j].AntalSåldaArtiklar > 200)
                    {

                        nivå4  ;
                    }

                }

                BubbleSort(säljare);
                try
                {
                    StreamWriter sw = new StreamWriter("F:\\Test.txt");

                    for (int i = 0; i < säljare.Length; i  )
                    {

                        Console.WriteLine("\n");
                        Console.WriteLine("Namn: "   säljare[i].Namn);
                        Console.WriteLine("Personnummer: "   säljare[i].Personnummer);
                        Console.WriteLine("Distrikt: "   säljare[i].Distrikt);
                        Console.WriteLine("Antal sälj: "   säljare[i].AntalSåldaArtiklar);

                        sw.WriteLine("\n");
                        sw.WriteLine("Namn: "   säljare[i].Namn);
                        sw.WriteLine("Personnummer: "   säljare[i].Personnummer);
                        sw.WriteLine("Distrikt: "   säljare[i].Distrikt);
                        sw.WriteLine("Antal sälj: "   säljare[i].AntalSåldaArtiklar);

                        sw.WriteLine("\n");
                        sw.WriteLine("-----------------------------");
                        sw.WriteLine("\n Antal Säljare på nivå 1: "   nivå1);
                        sw.WriteLine("Antal Säljare på nivå 2: "   nivå2);
                        sw.WriteLine("Antal Säljare på nivå 3: "   nivå3);
                        sw.WriteLine("Antal Säljare på nivå 4: "   nivå4);
                        sw.WriteLine("\n -----------------------------");

                        Console.WriteLine("\n");
                        Console.WriteLine("-----------------------------");
                        Console.WriteLine("\n Antal Säljare på nivå 1: "   nivå1);
                        Console.WriteLine("Antal Säljare på nivå 2: "   nivå2);
                        Console.WriteLine("Antal Säljare på nivå 3: "   nivå3);
                        Console.WriteLine("Antal Säljare på nivå 4: "   nivå4);
                        Console.WriteLine("\n -----------------------------");
                        sw.Flush();


                    }

                }

                catch (Exception e)
                {
                    Console.WriteLine("Exception: "   e.Message);
                }

                finally
                {
                    Console.WriteLine("Executing finally block.");
                }
            }
        }
    }

CodePudding user response:

Your sort algo compares "current vs previous" and then swaps "current vs next" - this is inconsistent. If you had an array of { 3, 1, 2 } and "current" was 1 (the middle one), you cannot compare 1 with 3 (prev), and then use it as a basis to decide 1 and 2 (next) should be swapped

if (list[j].CompareTo(list[j - 1]) < 0) //if "current" less than "previous"
{
    Säljare tmp = list[j   1];  //remember next
    list[j   1] = list[j];      //swap current into next
    list[j] = tmp;              //set current from memory
}

Really, you'd want to do "current vs next" and maybe "swap current and next". Because we are comparing the current and the next, and we want a list that is sorted with the smallest first, we want to perform a swap if current is greater than next.

for (int i = 0; i < list.Length - 1; i  )
{
    for (int j = 0; j < list.Length - 1 - i; j  ) 
    {
        if (list[j].CompareTo(list[j   1]) > 0)
        {
            Säljare tmp = list[j   1];
            list[j   1] = list[j];
            list[j] = tmp; 
        }
    }
}

Note we've changed the start point of the j loop. j starts at 0, current, and proceeds to "less than (length - 1)" which in practice means that in an array of 10 elements j will run from 0 to 8, because it runs to "less than one-less-than-the-length". This means it's safe to do j 1 because it cannot fall out of the array

Keeping with the 10 size array for a moment, the i value increases all the time. When i is 1, j only runs from 0 to 7. We don't need to run all the way to 8 because we don't need to check it. The bubble sort carried the smallest item all the way and dumped it in slot 9 (the end of the array) the first time, so we know that 9 is already the smallest item; we dont need to swap it any more. It's an optimization to forget about it now.. so we make the loop run from 0 to 7, which means that the almost-smallest item will be carried all the way to slot 8 and dumped there. Repeat until the array is sorted

If you wanted to sort the other way, greatest first, you'd "swap elements if the current item is less than the next"

Have a play with it using just integers: https://dotnetfiddle.net/ldwOMV

  • Related