-2

So iam new to c# and trying to making a bubble sort algorithm and was wondering is there a way to swap 2 values in a list in the simplest way?

static void BubbleSort(List<int> list)
        {
            for (int i = 0; i == list.Count-1; i++)
            {
                for (int j = 0; i == list.Count - i; j++)
                {
                    if (list[j] > list[j+1])
                    {
                      //Need a way to swap values in position j and j+1
                    }
                }
            }
Mj _
  • 13
  • 5

2 Answers2

0

Keep it simple, use a temporary variable to momentanly store one of two values:

if (list[j] > list[j+1])
{
    int temp = list[j];
    list[j] = list[j+1];
    list[j+1] = temp;
}
maxroma91
  • 105
  • 1
  • 4
  • 2
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Feb 09 '21 at 19:59
-2
if (list[j] > list[j + 1])
    (list[j], list[j + 1]) = (list[j + 1], list[j]);
Blindy
  • 65,249
  • 10
  • 91
  • 131
  • 6
    I don't want to be "that guy", but could you explain what this is doing? – Andy Feb 09 '21 at 19:45
  • 5
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Feb 09 '21 at 19:58
  • 1
    @Blindy hey i tried your error and now it shows me an out of index error – Mj _ Feb 09 '21 at 20:15
  • 1
    @Blindy ``` List list = new List { 3, 1, 7, 5, 2 }; BubbleSort(list); } static void BubbleSort(List list) { for (int i = 0; i <= list.Count-1; i++) { for (int j = 0; j <= list.Count - i; j++) { if (list[j] > list[j+1]) { // can just swap values otherwise causes issues int temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } Console.WriteLine(String.Join(", ", list)); } – Mj _ Feb 09 '21 at 20:16
  • 1
    If you are trying to read this code and your eyes are getting crossed, it uses _Tuples_. If you have N values surrounded by parens, it represents a Tuple in modern C#. The right-hand side (RHS, as opposed to LHS) creates a tuple with two value (what at the `j+1` position in the list and what's at the `j` position). That's assigned to the tuple on the LHS, effectively assigning the values to the variables that make up the LHS tuple. In the end, this is the same as `list[j] = list[j+1]` and `list[j+1] = list[j]` happening simultaneously (without the need for the temporary you'd normally need) – Flydog57 Feb 09 '21 at 20:26
  • Im team parentesis magic need explanation. Im also in the team. if this is any good this should find home on a upvoted good question. – Drag and Drop Feb 09 '21 at 20:29
  • @Flydog57: You're on the right track, but to be nitpicky the LHS isn't a Tuple: it's a pair of assignments wrapped in deconstruction syntax. – StriplingWarrior Feb 09 '21 at 21:03
  • 2
    @StriplingWarrior I didn't have enough characters to get into deconstructors. It's tuple-ish! – Flydog57 Feb 09 '21 at 21:38