1

I am having an issue printing a sorted list after creating a bubble sort. I am able to print the unsorted list, however, I'm not sure how to actually pass in the unsorted list to my Bubble sort method or if I even have to. The list that I am printing and trying to sort is a .csv file that I was supposed to read in from a separate method. This is the code I have so far.

                switch (menuChoice)
                {
                    case 1:
                        ReadFile(fpath);
                        Console.ReadKey();
                        Console.Clear();
                        break;

                    case 2:

                        break;

                    case 3:

                        break;

                    case 4:

                        break;

                    case 5:

                        break;
                }
            }

        }

        private static void BubbleSort(List<string> bList)
        {
            int listLength = bList.Count();

                bool swapped = false;
            do
            {

                for (int i = 0; i <= listLength; i++)
                {
                    int newListLngth = bList[i - 1].CompareTo(bList[i]);

                    if (newListLngth > 0)
                    {
                        Swap(bList, i, i - 1);
                        swapped = true;
                    }
                }
            } while (!swapped);

        }

        static List<string> MergedSort(List<string> mergedList)
        {
            List<string> merger = new List<string>();
            if (mergedList.Count <= 1)
            {
                return mergedList;
            }

            return merger;
        }

        static void Swap(List<string> numbers, int index1, int index2)
        {

            string temp = numbers[index1];
            numbers[index1] = numbers[index2];
            numbers[index2] = temp;

        }

        private static void ReadFile(string filePath)
        {
            using (StreamReader sr = new StreamReader(filePath))
            {
                char delimiter = ',';

                string comicText = File.ReadAllText(filePath);
                string[] data = comicText.Split(delimiter);
                List<string> names = data.ToList();
                for (int i = 0; i < names.Count; i++)
                {
                    Console.WriteLine(names[i]);
                }
            }
        }
        
    } 
  • Please only use the `[visual-studio]` tag for questions about the Visual Studio application. I've removed it for you. – ProgrammingLlama Jan 17 '22 at 03:59
  • also please remove the many parts in code that isn't related to the problem(such as menu, user selection, etc). – Lei Yang Jan 17 '22 at 04:02
  • [Does this answer the question?](https://stackoverflow.com/questions/8708632/passing-objects-by-reference-or-value-in-c-sharp) (Note that `List<>` is a reference type) – ProgrammingLlama Jan 17 '22 at 04:03
  • Sorry, new to posting here, been lurking up until now. I probably should have mentioned that the printed lists are going into a switch case, in this instance case 1 is going to be my bubble sort. – RichieMunoz Jan 17 '22 at 04:12

1 Answers1

0

You read the file and print its contents to the console, but you don't store the read data anywhere or return it, which makes it impossible to use anywhere else in the program

Consider returning it

private static List<string> ReadFile(string filePath){
  ...
  return names;
}

And then you can

case 1:
  var file = ReadFile(...);
  //print...
  BubbleSort(file);
  //print...

Move that for loop in read file, into its own method that prints the list, then you can call it every time you want to print

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • Would I have to declare ``` var file = ReadFile(); ``` outside of the switch statements in order to use it with the print method that I will create using the for loop that was in the ReadFile Method? If not, what parameter would I pass into the Print method call if any parameter should be passed? Also sorry if I don't use the correct terminology at times, this is my second month/class of programming. Also thank you so much for taking the time to reply, as a beginning/aspiring programmer/game dev, it means a lot. – RichieMunoz Jan 17 '22 at 07:19
  • After doing all you suggested, to the best of my knowledge, I got an unhandled exception for my BubbleSort method on the line containing ```int newListLength = bList[i - 1].CompareTo(bList[i]);``` as well as on where I called ```BubbleSort(file)```. Exception states index was out of range. – RichieMunoz Jan 17 '22 at 07:28
  • If you plan to use the var inside the switch so all the statements are inside the case block (read, print, sort, print) then you don't have to declare outside the switch – Caius Jard Jan 17 '22 at 08:17
  • The exception you get is probably not related to the changes; I didn't verify whether the bubblesort method was correct – Caius Jard Jan 17 '22 at 08:17
  • Having reviewed it now, I would point out that you cannot do `I <= listLength` - for a list that contains 10 items, the valid indexes are 0-9. If you use "less than or equals" you end up running the loop when I == 10 – Caius Jard Jan 17 '22 at 08:20
  • Also consider if your loop accessed the n+1 item you need to loop to a max of `< length -1` – Caius Jard Jan 17 '22 at 08:24
  • (And similarly if you write some loop that accesses the `n-1`th item during its work you need to start looping from 1 (or have some other protection against trying to access list index [-1] – Caius Jard Jan 17 '22 at 10:49