C
C#•4w ago
Rex_rawr

Does this count as an InsertionSort

So i need to program an Insertionsort for my school and i just wanted to ask if this would count as one. I does sort the numbers atleast.
No description
22 Replies
canton7
canton7•4w ago
Come on man, actually paste the code here, please Don't make us squint at a quite bad picture of some code on a screen
Rex_rawr
Rex_rawrOP•4w ago
Sorry im on it rn booting up my laptop ill send a screenshot any second
Rex_rawr
Rex_rawrOP•4w ago
i hope this is better
No description
canton7
canton7•4w ago
$code
MODiX
MODiX•4w ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
canton7
canton7•4w ago
I don't see how that can be an insertion sort, no I don't see how it could actually sort correctly either
Rex_rawr
Rex_rawrOP•4w ago
The teacher said that we should always check the number before it and then swap them out so like if we have [2,5,7,4] it should firstly look is the numer bigger then if yes then leave it if it is smalle rthen change it and that till it is right and he called it an insertionsort do idk tbh
canton7
canton7•4w ago
Can you post your actual code, rather than a screenshot, so we can run it If that code actually works you'll win a prize or something -- there's no sorting algorithm which works using a single pass through the array
Rex_rawr
Rex_rawrOP•4w ago
Yea sure do i need to use some kind of command or just paste it in here here is the code:
canton7
canton7•4w ago
$code
MODiX
MODiX•4w ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
canton7
canton7•4w ago
$codegif
Rex_rawr
Rex_rawrOP•4w ago
Ohh sorry ill do that
canton7
canton7•4w ago
Oh sorry, I misread your loop Let's read it again...
Rex_rawr
Rex_rawrOP•4w ago
cs
internal class Insertionsort {

static void Main() {
int[] unsortetArray = { 56, 8, 9, 85, 24, 23, 22, 33, 81, 44, 79, 41, 36, 46, 34, 52, 26,
72, 99, 31, 68, 95, 96, 17, 63, 55, 37, 14, 87, 23, 2, 93, 96, 16,
19, 34, 24, 79, 23, 3, 20, 40, 34, 79, 14, 5, 34, 86, 12, 5}; // generated int array from chat gpt to get a random number array

unsortetArray = InsertionSorting(unsortetArray);

foreach (int i in unsortetArray) {
Console.WriteLine(i);
}
}


static int[] InsertionSorting(int[] unsortetArray) {

int index = 0, compare, remember;

while (index < unsortetArray.Length - 1) {

index++;
compare = index - 1;

if (compare < 0) { // checks that if compare would be smaller than zero so an error wont occure
compare = 0;
}

if (unsortetArray[compare] > unsortetArray[compare + 1]) {

remember = unsortetArray[compare];
unsortetArray[compare] = unsortetArray[compare + 1];
unsortetArray[compare + 1] = remember;

index -= 2; // -2 so it can sort the numbers before that if they are not already sorted

}
}

return unsortetArray;
}
}
cs
internal class Insertionsort {

static void Main() {
int[] unsortetArray = { 56, 8, 9, 85, 24, 23, 22, 33, 81, 44, 79, 41, 36, 46, 34, 52, 26,
72, 99, 31, 68, 95, 96, 17, 63, 55, 37, 14, 87, 23, 2, 93, 96, 16,
19, 34, 24, 79, 23, 3, 20, 40, 34, 79, 14, 5, 34, 86, 12, 5}; // generated int array from chat gpt to get a random number array

unsortetArray = InsertionSorting(unsortetArray);

foreach (int i in unsortetArray) {
Console.WriteLine(i);
}
}


static int[] InsertionSorting(int[] unsortetArray) {

int index = 0, compare, remember;

while (index < unsortetArray.Length - 1) {

index++;
compare = index - 1;

if (compare < 0) { // checks that if compare would be smaller than zero so an error wont occure
compare = 0;
}

if (unsortetArray[compare] > unsortetArray[compare + 1]) {

remember = unsortetArray[compare];
unsortetArray[compare] = unsortetArray[compare + 1];
unsortetArray[compare + 1] = remember;

index -= 2; // -2 so it can sort the numbers before that if they are not already sorted

}
}

return unsortetArray;
}
}
canton7
canton7•4w ago
I think that's doing like a reverse bubble sort
Rex_rawr
Rex_rawrOP•4w ago
Im sorry a what?😭
Rex_rawr
Rex_rawrOP•4w ago
Okay ill read it but ty for the help
canton7
canton7•4w ago
Ah no you're right, that is an in-place insertion sort I take that all back 😛 It's late and I'm tired, that's my excuse
Rex_rawr
Rex_rawrOP•4w ago
Ohh okay okay ty for the help! I was kinda scared because the teacher just said yh make an insertionsort and i tried to understand what he said but he just rushed over it and stopped with the lesson 🥲 and i need it till tomorrow so yhhh😭 ty again
Want results from more Discord servers?
Add your server