C
C#2y ago
ESGO

❔ ✅ how to sort numbers in list without using funcitons

how can i sort numbers in a list without using reverse?
154 Replies
sibber
sibber2y ago
without using functions? why?
ESGO
ESGOOP2y ago
yea thats the requirment i thought of how to do it in python but in c# i just cant
sibber
sibber2y ago
oh its an assignment
ESGO
ESGOOP2y ago
yea
sibber
sibber2y ago
so they want you to implement a sorting algorithm?
ESGO
ESGOOP2y ago
yes i wanted to mb check which number is higher or lower and doing it like that
sibber
sibber2y ago
they didnt say which algorithm to implement?
ESGO
ESGOOP2y ago
no just sort without functions
Angius
Angius2y ago
And what do you have so far?
ESGO
ESGOOP2y ago
creating list by the user and i want to sort this list i searched it online but couldnt find it
Angius
Angius2y ago
Look up "bubble sort", it's one of the more basic sorting algorithms
ESGO
ESGOOP2y ago
like sorting numbers i only found sorting with lengths of words
Angius
Angius2y ago
Lengths of words are... numbers
cap5lut
cap5lut2y ago
just take the "length of words" as ur numbers 😉
ESGO
ESGOOP2y ago
wait
Anton
Anton2y ago
there are dozens of algorithms for sorting. the simplest would be a nested for that goes through every element and swaps them if one is less/greater
sibber
sibber2y ago
Bubble sort
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the input list element by element, comparing the current element with the one after it, swapping their values if needed. These passes through the list are repeated until no swaps had to be performed during a pass, meaning that the list...
ESGO
ESGOOP2y ago
but how can u make it so it checks eachother
cap5lut
cap5lut2y ago
well u have a List<int> i assume, right?
ESGO
ESGOOP2y ago
yea
cap5lut
cap5lut2y ago
so with yourList[index] u can access each element in the list if u provide the corresponding index, like yourList[0] will give you access to the first element
ESGO
ESGOOP2y ago
the list was created with inputs and than adding them so i dont know how many elements are there
cap5lut
cap5lut2y ago
thats why the list has a Count property, its giving u the amount of elements stored in that list so yourList[yourList.Count - 1] would be the last element in ur list (the - 1 is needed because indices are starting at zero)
ESGO
ESGOOP2y ago
but what about the middle part
cap5lut
cap5lut2y ago
by "middle part" u mean the elements between first and last?
ESGO
ESGOOP2y ago
yea
cap5lut
cap5lut2y ago
ah okay: u dont write that down manually. do u know about for loops yet?
ESGO
ESGOOP2y ago
yea this is how i made the list List<int> Numbers = new List<int>(); Console.WriteLine("how many numbers do you want to input:"); int num = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < num; i++) { Console.WriteLine("input number "); int Input_Num = Convert.ToInt32(Console.ReadLine()); Numbers.Add(Input_Num); }
cap5lut
cap5lut2y ago
okay, now think about how u would write each number from the Numbers list to the console in a for loop (just try it, if its wrong thats okay, ill explain what was wrong) also $code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
cap5lut
cap5lut2y ago
$codegif
cap5lut
cap5lut2y ago
(for better readability)
ESGO
ESGOOP2y ago
oh ty create seperate count =0 and create loop printing the elements but adding +1 to count
cap5lut
cap5lut2y ago
show me some code ;p
ESGO
ESGOOP2y ago
wait it didnt workk lemme try oen sec
cap5lut
cap5lut2y ago
try to ur hearts content 😉 when u r stuck ill help out
ESGO
ESGOOP2y ago
wait should i use foreach?
cap5lut
cap5lut2y ago
nope, i want u to use a for loop (like u did for reading the user input numbers) this is essential for the sorting algorithm
ESGO
ESGOOP2y ago
int Count = 0; for (int i = 0; i < Numbers.Count; i++) ; Console.WriteLine(Numbers.Count); Count++; couldnt find that on my keyboard sorry
sibber
sibber2y ago
above tab
ESGO
ESGOOP2y ago
oh
int Count = 0;
for (int i = 0; i < Numbers.Count; i++) ;
Console.WriteLine(Numbers.Count);
Count++;
int Count = 0;
for (int i = 0; i < Numbers.Count; i++) ;
Console.WriteLine(Numbers.Count);
Count++;
Numbers is the list
sibber
sibber2y ago
the semicolon after the for loop is a statement no brackets means only do the following statement
ESGO
ESGOOP2y ago
oh yea
sibber
sibber2y ago
so that for loop will do nothing
ESGO
ESGOOP2y ago
ty will that work for printing that element
sibber
sibber2y ago
btw Count is the same as i so no need for it
ESGO
ESGOOP2y ago
k changed that
sibber
sibber2y ago
btw an easier way print lists/enumerables is
Console.WriteLine(string.Join(", ", list));
Console.WriteLine(string.Join(", ", list));
cap5lut
cap5lut2y ago
imagine u entered 3 numbers, what would this print?
for (int i = 0; i < Numbers.Count; i++)
{
Console.WriteLine(i);
}
for (int i = 0; i < Numbers.Count; i++)
{
Console.WriteLine(i);
}
sibber
sibber2y ago
0 1 2
cap5lut
cap5lut2y ago
but thats not the point, its the first step to implement bubble sort dont spoon feed them D:
sibber
sibber2y ago
oh i thought you were trying to print the list welp you asked oh thats different person lol my bad
cap5lut
cap5lut2y ago
yeah, to help them understand whats going on 😉
ESGO
ESGOOP2y ago
ok ik whats wrong i didnt print out the list just the order or number of the list 0 1 2 not like 59 90 103
cap5lut
cap5lut2y ago
ESGO, try to turn that loop into printing the elements of the list instead of the indices u will just have to adjust the line Console.WriteLine(i);
ESGO
ESGOOP2y ago
for (int i = 0; i < Numbers.Count; i++)
{
Console.WriteLine(Numbers[i]);
}
for (int i = 0; i < Numbers.Count; i++)
{
Console.WriteLine(Numbers[i]);
}
cap5lut
cap5lut2y ago
correct ;p so for now u have learned how to iterate over the elements by accessing them by index now its about sorting that bunch of numbers, we dont look at the big picture yet, but only at the two first elements Number[0] and Number[1]. to sort them, u must compare them first, how would u do that?
ESGO
ESGOOP2y ago
looping comparing first and second elements and than adding i+1?
cap5lut
cap5lut2y ago
dont think about the loop for now, but just Number[0] and Number[1] (we will come back to the loop)
ESGO
ESGOOP2y ago
kk
cap5lut
cap5lut2y ago
basically, if Number[0] is bigger than Number[1] u would have to swap them. write that in code 😉
ESGO
ESGOOP2y ago
if? using if
cap5lut
cap5lut2y ago
yep
ESGO
ESGOOP2y ago
if Number[i]>Number[i+1]
Number = Number.Swap(i, i+1);
if Number[i]>Number[i+1]
Number = Number.Swap(i, i+1);
cap5lut
cap5lut2y ago
oh, i just noticed that i had typos, its Numbers[...] instead of Number[...] about ur code: the condition must be in brackets so its if (Numbers[i] > Numbers[i + 1])
ESGO
ESGOOP2y ago
yea forgot i just wrote it here
cap5lut
cap5lut2y ago
Swap isnt an existing method, u have to manually do it. so basically Numbers[i] = ...
if (Numbers[i] > Numbers[i + 1])
{
// swap the numbers here
}
if (Numbers[i] > Numbers[i + 1])
{
// swap the numbers here
}
ESGO
ESGOOP2y ago
wait its not? dam Numbers [i+1] = Numbers [i] will that work
cap5lut
cap5lut2y ago
thats only a part of the solution. u basically just set the value of the next element (Numbers[i + 1]) to the current value (Numbers[i]) so if u have a list [15, 1] that would result in [15, 15], right?
ESGO
ESGOOP2y ago
oh and do Numbers [i] = Numbers [i+1] but wouldnt it alr be set to 15? like the example
cap5lut
cap5lut2y ago
yep thats why u would need to store the value first in another variable
ESGO
ESGOOP2y ago
Numbers [i+1] = New_num Numbers [i+1] = Numbers [i] Numbers [i] = New_num
cap5lut
cap5lut2y ago
the first line would be incorrect
int temp = Numbers[i + 1];
Numbers[i + 1] = Numbers[i];
Numbers[i] = temp;
int temp = Numbers[i + 1];
Numbers[i + 1] = Numbers[i];
Numbers[i] = temp;
ESGO
ESGOOP2y ago
int i am too used to python ong
cap5lut
cap5lut2y ago
yeah, python is a bad starting language imo
ESGO
ESGOOP2y ago
lemme think of using this in a loop for (int i = 0; i < Numbers.Count;) and do i++ in for loop wont it work? and i need to add another if statement
cap5lut
cap5lut2y ago
u mean
for (int i = 0; i < Numbers.Count; i++)
{
if (Numbers[i] > Numbers[i + 1])
{
int temp = Numbers[i + 1];
Numbers[i + 1] = Numbers[i];
Numbers[i] temp;
}
}
for (int i = 0; i < Numbers.Count; i++)
{
if (Numbers[i] > Numbers[i + 1])
{
int temp = Numbers[i + 1];
Numbers[i + 1] = Numbers[i];
Numbers[i] temp;
}
}
?
ESGO
ESGOOP2y ago
yea and adding another if statement
cap5lut
cap5lut2y ago
keep ur if statement idea in mind, there is a flaw in the current code:
ESGO
ESGOOP2y ago
dam
cap5lut
cap5lut2y ago
for (int i = 0; i < Numbers.Count; i++) will do the code for each index/element of the list right?
ESGO
ESGOOP2y ago
1 2 3 4 5 6 it will go to 1 to 2 than 2 and 3 wont it
cap5lut
cap5lut2y ago
so for the list [15, 1], the i will be 0 and 1 so lets assume we are at the last iteration of the loop, so i will be 1, basically the very last element of the list.
ESGO
ESGOOP2y ago
i wont trust the loop suggestions anymore ig
cap5lut
cap5lut2y ago
but our code looks "forward" by Numbers[i + 1] an element that can not possibly exist thats why we have to adjust the loop condition now we dont want to iterate over all elements anymore, but over all but not the last
ESGO
ESGOOP2y ago
OOOh
cap5lut
cap5lut2y ago
so the i < Numbers.Count must be adjusted
ESGO
ESGOOP2y ago
at the and of the list it will break i am dum for (int i = 0; i < Numbers.Count - 1; i++)
cap5lut
cap5lut2y ago
u aint dumb, im leading ya to some flaws 😉 yes thats correct
ESGO
ESGOOP2y ago
for (int i = 0; i < Numbers.Count - 1; i++)
for (int i = 0; i < Numbers.Count - 1; i++)
les go
cap5lut
cap5lut2y ago
so thats the up to date code which runs smoothly
for (int i = 0; i < Numbers.Count - 1; i++)
{
if (Numbers[i] > Numbers[i + 1])
{
int temp = Numbers[i + 1];
Numbers[i + 1] = Numbers[i];
Numbers[i] temp;
}
}
for (int i = 0; i < Numbers.Count - 1; i++)
{
if (Numbers[i] > Numbers[i + 1])
{
int temp = Numbers[i + 1];
Numbers[i + 1] = Numbers[i];
Numbers[i] temp;
}
}
ESGO
ESGOOP2y ago
now second statement
cap5lut
cap5lut2y ago
now imagine u have a list [15, 2, 1] and think about what happens if u run that code whats the end result of the list?
ESGO
ESGOOP2y ago
one sec 2 1 15
cap5lut
cap5lut2y ago
correct
ESGO
ESGOOP2y ago
what if we make it so it checks it 2 times
cap5lut
cap5lut2y ago
i would phrase it differently: we sorted the first element at index 0 into its appropriate place but that also means sorted only one element, now we have to do that for each element. that means, we need a for loop, that executes the current code for each element
for (???; ???; ???)
{
for (int i = 0; i < Numbers.Count - 1; i++)
{
if (Numbers[i] > Numbers[i + 1])
{
int temp = Numbers[i + 1];
Numbers[i + 1] = Numbers[i];
Numbers[i] temp;
}
}
}
for (???; ???; ???)
{
for (int i = 0; i < Numbers.Count - 1; i++)
{
if (Numbers[i] > Numbers[i + 1])
{
int temp = Numbers[i + 1];
Numbers[i + 1] = Numbers[i];
Numbers[i] temp;
}
}
}
ESGO
ESGOOP2y ago
wait what if we do i = 1 and in if statments we do 0 index as i-1
cap5lut
cap5lut2y ago
then we start always at the second element
ESGO
ESGOOP2y ago
i-1?
cap5lut
cap5lut2y ago
the first for loop (the one with the ???) should loop over all elements so at each iteration u are looking at one element. the second loop then moves that element to its correct position
ESGO
ESGOOP2y ago
oh i am dum one sec
cap5lut
cap5lut2y ago
but currently, the inner loop always checks the first element, ignoring the outer loop
ESGO
ESGOOP2y ago
waht if we do the same loop after that loop finishes it should work
cap5lut
cap5lut2y ago
can u show what code u have in mind?
ESGO
ESGOOP2y ago
got erros its incorrect like if we run the same loop twice on the list doing the same checks
cap5lut
cap5lut2y ago
show me the code ;p and the errors
ESGO
ESGOOP2y ago
for (int i = 0; i < Numbers.Count - 1; i++) { for (int i = 0; i < Numbers.Count - 1; i++) { if (Numbers[i] > Numbers[i + 1]) { int temp = Numbers[i + 1]; Numbers[i + 1] = Numbers[i]; Numbers[i] temp; } } } didnt work same thing
ESGO
ESGOOP2y ago
cap5lut
cap5lut2y ago
in the outer for loop, rename the variable i to something else (eg k )
ESGO
ESGOOP2y ago
cap5lut
cap5lut2y ago
Numbers[i] temp; needs to be Numbers[i] = temp; (sorry thats my fault, my keyboard is a bit broken)
ESGO
ESGOOP2y ago
i swear i did that it was the same error dam ty lemme try doesnt print the last output like the l;ist
ESGO
ESGOOP2y ago
ESGO
ESGOOP2y ago
oh i am so dumb i forgot to write to print out the list
cap5lut
cap5lut2y ago
;p
MODiX
MODiX2y ago
cap5lut#0057
REPL Result: Success
List<int> Numbers = new List<int> { 2, 1, 15, 2 };

for (int k = 0; k < Numbers.Count - 1; k++)
{
for (int i = 0; i < Numbers.Count - 1; i++)
{
if (Numbers[i] > Numbers[i + 1])
{
int temp = Numbers[i + 1];
Numbers[i + 1] = Numbers[i];
Numbers[i] = temp;
}
}
}

foreach (var num in Numbers) Console.WriteLine(num);
List<int> Numbers = new List<int> { 2, 1, 15, 2 };

for (int k = 0; k < Numbers.Count - 1; k++)
{
for (int i = 0; i < Numbers.Count - 1; i++)
{
if (Numbers[i] > Numbers[i + 1])
{
int temp = Numbers[i + 1];
Numbers[i + 1] = Numbers[i];
Numbers[i] = temp;
}
}
}

foreach (var num in Numbers) Console.WriteLine(num);
Console Output
1
2
2
15
1
2
2
15
Compile: 539.171ms | Execution: 60.722ms | React with ❌ to remove this embed.
ESGO
ESGOOP2y ago
i forgot LESE GOOOO
ESGO
ESGOOP2y ago
ESGO
ESGOOP2y ago
fuk i should have just printed it out myself
cap5lut
cap5lut2y ago
show me ur code
ESGO
ESGOOP2y ago
cap5lut
cap5lut2y ago
List<int> Numbers = new List<int> { 2, 1, 15, 2 }; <-- that line was just to fake user input. that part is already done by u (and above the code u have shown)
ESGO
ESGOOP2y ago
i didnt even look at it i am retar
MODiX
MODiX2y ago
Sorry @cap5lut your message contained blocked content and has been removed!
ESGO
ESGOOP2y ago
cap5lut
cap5lut2y ago
can u show the whole code? its simply about variable names i guess
ESGO
ESGOOP2y ago
\
cap5lut
cap5lut2y ago
and u are neither dumb or retar..., u r learning something new, so its okay to make some errors 😉
ESGO
ESGOOP2y ago
bro i swear ty ily like i wasted so much of your time
cap5lut
cap5lut2y ago
that List<int> Numbers = ... between the reading user input and sorting for-loops should be removed
ESGO
ESGOOP2y ago
i am blind ty
cap5lut
cap5lut2y ago
as said, that was to fake the user input which u have done already
ESGO
ESGOOP2y ago
cap5lut
cap5lut2y ago
in the last foreach loop, the num contradicts with the num of ur user input (how many numbers should be entered) so u can just rename it for the foreach loop
ESGO
ESGOOP2y ago
oh yea i am blind
cap5lut
cap5lut2y ago
its basically complaining that a num variable already exists
ESGO
ESGOOP2y ago
yea saw it i like checked num and didnt saw the other num change colours ty like u spent so much time on me ily sorry for your time and thank u
cap5lut
cap5lut2y ago
i love myself too ;p
ESGO
ESGOOP2y ago
U SHOULD
cap5lut
cap5lut2y ago
so, before we stop here now: foreach (var num in Numbers) Console.WriteLine(num); <- dont do it like that, i was just lazy typing. u should always use curly braces spot the huge difference on what it is doing:
foreach (var num in Numbers)
Console.WriteLine(num);
Console.WriteLine("some text");
foreach (var num in Numbers)
Console.WriteLine(num);
Console.WriteLine("some text");
foreach (var num in Numbers)
{
Console.WriteLine(num);
Console.WriteLine("some text");
}
foreach (var num in Numbers)
{
Console.WriteLine(num);
Console.WriteLine("some text");
}
assuming there are 10 numbers in Numbers, one will print "some text" 10 times and the other only once
ESGO
ESGOOP2y ago
i always forget those last task i was stuck 30 mins bc of it it wouldnt run normally f python
cap5lut
cap5lut2y ago
well, u come from python, so its sorta understandable ;p
ESGO
ESGOOP2y ago
yea shi to easy there ty again i learned alot
cap5lut
cap5lut2y ago
dont get me wrong, python has its benefits too, but for a beginner its simply the wrong language so do u have any left over questions regarding this topic? if not: $close
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
cap5lut
cap5lut2y ago
(and im glad i could help :3)
ESGO
ESGOOP2y ago
$close
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
ESGO
ESGOOP2y ago
ty btw
sibber
sibber2y ago
theres no wrong language
cap5lut
cap5lut2y ago
yeah i should have added "in my opinion". imo having duck typing languages like python, js, etc shouldnt be the first language to learn, a statically typed language is imo the better choice
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server