-1
To simplify my question, I will show the Bubble Sort in python:
def bubbleSort(self):
for i in range(len(self) - 1):
for j in range(len(self) - 1):
if self[j] > self[j+1]:
self[j], self[j+1] = self[j+1], self[j]
l = [2, 8, 3, 5, 4, 6, 0, 7, 1]
bubbleSort(l)
print(l)
EXIT: [0, 1, 2, 3, 4, 5, 6, 7, 8]
I created a code in C#:
using System;
public class ListFunction : Object
{
public static int len(var l)
{
int len = 0;
foreach (var i in l)
{
len++;
}
return len;
}
public static void bubbleSort(int[] l)
{
for (int i = 0; i < len(l); i++)
{
for (int j = 0; i < len(l); j++)
{
if (l[j] > l[j + 1])
{
int temp = l[j];
l[j] = l[j + 1];
l[j + 1] = temp;
}
}
}
}
}
var l = new List { 2, 8, 3, 5, 4, 6, 0, 7, 1 };
bubbleSort(l);
I did not test because I had error. Error does not let test, right?
You are using
var
at all remember that [tag:c#] is a strongly typed language and which parameters should have their type explicitly stated. Language does not accept data processing outside of class declaration and out-of-method flow control.– Augusto Vasques
Ahh, I get it. Can you show me how to create this kind of function?
– DerickXIGamer
There are many errors in your code I will not miss my Saturday correcting them. Example the function
bubbleSort()
does not return a value, has no return type and in the absence of this does not have a parameter output attribute, the functionlen()
should not exist,......– Augusto Vasques
Okay. But I also wanted to know how you see the size of a list
– DerickXIGamer
List<T>.Count
. Example:var l = new List<int> { 2, 8, 3, 5, 4, 6, 0, 7, 1 }; Console.WriteLine(l.Count);
– Augusto Vasques