Data Structures and Algorithms In C#: Array + Array Insertions (Part 1)

preview_player
Показать описание
Data Structures and Algorithms In C#: Array + Array Insertions (Part 1)

Рекомендации по теме
Комментарии
Автор

ngl... you are very talented at teaching. This playlist made so many things click for me. Im a self taught dev that never learned data structures at the core. I just know how to practically build applications and been working professionallly for 6 years. I am trying to get into my dream company (Microsoft) one day and I know this is stuff I need to learn to get the job, but I always found this stuff extremely difficult to learn and I end up giving up.

THANK YOU FOR THESE GODLY VIDEOS

nyhustler
Автор

I still use some arrays, even after many years experience. I will mainly use it for some kind of read-only, fixed storage or just a lower-level collection that's compact and lightweight. You have to remember that List<T> is generally going to consume _2n_ × sizeof(T), i.e., double the number of items multiplied by their size in memory. They are also fast and very direct, there's not a bunch of extra call overhead or anything. They're great in situations where I have several instance members of the same type that you want to be able to iterate over or do something with collectively, too, I will make a private array that just initializes at the end of the constructor logic and like:
this._arr = new[] { a, b, c };
... then I can use that privately inside the class, like if those are 3 void* or IntPtr style pointers to unmanaged resources I can foreach/iterate over them when IDisposable.Dispose is called and release the pointer and resources to cleanup, or if it's some sort of time accumulator mechanism for game logic then I may do that when some sort of timer "Reset" method/event is run.

Another important thing to be aware of is to avoid temporary array or collection initialization inside of methods ... people do it frequently if they don't know any better: they're writing a method and decide they need a small array or List temporarily so they declare it like:
var temp = new List<int>();
And what they don't realize is that you may not notice a problem in simple programs that don't run very long or do much, but if you're doing this sort of stuff in a real application at scale it's going to cause GC (garbage collector) pressure and can significantly hurt performance. If you're doing this in a video game in the main loop, like a Unity project, you're going to cause GC spikes that lag the game and make the framerate randomly drop and users will be mad/frustrated and hate it. So, how do you get around this? It's actually quite simple ... just declare _one_ *static* array, list or whatever outside your method body, of whatever kind of collection your methods required inside of them. And then just _recycle_ and _reuse_ that same collection when the method is called! It's an easy change to make, and it stops the CLR from allocating more and more heap space each time the method is called and recycles the same memory. Just call list.Clear() or an equivalent method and reuse the same thing!

GameDevNerd
Автор

When you used i = 4 in the for loop to insert anywhere in the array, the number 6 got overridden, I got back all the numbers when I used 7 which is the number of indexes that was populated or 8, but that ends up doing an unnecessary loop because it is just shifting a 0. Otherwise, great stuff, learned a lot.

daviad
Автор

As a university student, Your tutorial is perfect for me, You are the best teacher, thank u !

harryspinach
Автор

Man, I'm on the 3rd video and I can't get why this does't get more vues, that's just gold, thank you so much !

KDAN
Автор

Now hold on here... you didn't "insert" anything into the "end" of that Array ???
All you did was loop through and assign values to existing indexes ?

chadgregory
Автор

For adding at the last index, why don't we do it like this instead of introducing a loop which will slow down the algorithm.
int[] array1 = new int[5];
int length = array1.Length;
int lastIndex = length - 1;

array1[lastIndex] = 9;


This will be 0(1) instead of o(n)

linclinc
Автор

In the Algorithm where you inserted value 8 at any place, you started shifting the values not from the last element but rather from element at index 4, due to which the number 6 gets replaced by 5 permanently. I think you should've started shifting the element all the way from the last element of the array.

mohammadtalha
Автор

Thanks for the Video, I see when you make an insertion in the middle of the array number 6 gets disappeared.

apoorvtiwari
Автор

This playlist is Insanely good! Thank you !

thedarkestwhite
Автор

When u say add to end of array, u mean that to assign a value to the next index which as not been assigned to yet? How do u know that a value has not been assigned to an index yet? In the example it’s hardcoded to insert values up to 2 index so we know the length(in this case the indexes that have been assigned a value)

whosgotrythm
Автор

I tried to make the insertion at the end by myself, this is what I got:
int k = 0;
for (int i = 0; i < 5; i++)
{
arr[k] = k - 1;
k--;
}
arr[0] = 10;
It worked for me, but i'm wondering if it's going to be slower than the way you did it?

salahuddin
Автор

Hi Teddy, at 23:24 shouldn't it be int i = length ? This way you don't lose any data in the array.

fatboysticks
Автор

Thank you sir, like your way of teaching 🙏

bonkeselemani
Автор

I take a huge huge huge issue with these being called "insertions" ....
They're assignments

chadgregory
Автор

How is insertion at the end of an array O(1) if the algorithm contains a for loop with those typically representative of linear complexity O(n)?
Thank you for making these videos!

chrisp
Автор

I tried the last part of video my self and this is what i got is it correct?

private static void InsertionAnyWhere()
{
int[] intArray = { 1, 2, 3, 4, 5, 6, 7, 0 };

int numberToInsert = 20;
int whereToInsert = 4;

for(int i = intArray.Length - 1; i >= whereToInsert; i-- )
{
intArray[i] = intArray[i-1];
}

intArray[whereToInsert] = numberToInsert;

int joker = 0;
}

StressedProgrammer
Автор

quick question, how are u able to write an array without it being inside a class, and how are you able to access the methods/classes of the array without it being inside the main method?

mahersabit
Автор

What about you don't know the array size. How would you do it .e.g. like array.lenth/2 then implement the shift? Thanks.

patyue
Автор

It will maybe sound stupid but where are methods ex. namespace, class, static void Main etc. Please explain a little bit Teddy 🙏

KeskilChnProgram