The document on DSA (Data Structures and Algorithms) is a comprehensive guide that covers the fundamental concepts, techniques, and implementations of data structures and algorithms. It provides an overview of various data structures such as arrays, linked lists, stacks, queues, trees, and graphs, ...
Bubble Sort Program in C
CodeWithHarry
The code in Bubble Sort in C language is so easy that after
this video, you 'll say it was super easy and you'll have a lot
of fun. I use C language because most people understand it.
Those who know either C or C++ understand. They are
similar languages so you wo n't face an issue. We 'll add
two for loops here. It 'll work from i = 0 to n. In every pass,
we compared 2 elements like 0 and 1 in each pass. We
should 've replaced them if they were not in place but we
did n't because they were in place. We had to see 11 and 9 ,
we had to replace them. 9 is smaller than 11 , so it 'll come
first. If I compare 11 and 2 , I 'll have to replace it. Then 2
and 3 in the next new array and keep swapping. So till
where should passes run ? It 's a mystery. The comparisons
fit into a math formula, n-1-i. This for loop is for number of
passes and this is for comparison in each pass. The
comparisons in each. pass will be in this for loop. The value
of j will be 1, it 'll be between 1 and 2. Is 11 greater than
2 ? Yes. Interchange 11 and 11 and swap them.
If in our comparison , the first element is greater than the
next element, we 'll have to swap them. We only have to do
this and our array will be sorted. Everything else will be
handled automatically because what are we doing ? We are
running n-1 passes and we 're doing this for every pass.
Bubble Sort is just this easy. After every pass, I 'll make a
variable, isSorted, which means whether or not it 's sorted.
If it fails every time, that means the array is sorted. Even if
this happens in pass 2 or 3, we 'll want it to stop as soon as
if does n't apply to any pass , we want the sorting process to
stop. So how many passes will work in this here ? A single
pass will do. If the array is already sorted, it takes only one
,pass to sort it. If it 's not sorted, if I comment out and
uncomment, then you can see that it takes 5 passes to sort.
If there 's no swapping in the first pass itself, that means the
array 's already sorted. If I do n't have to swap anything,
what does that mean that the array's elements are sorted. I
'm making isSorted value 1 after every pass. I only care
about sorting my array within minimum time , I should be
happy.
,Insertion Sort Algorithm in Hindi
CodeWithHarry
I will tell you Insertion sort in intuitive way in this video that it
will be difficult for you to not understand Insertion Sort. I will
challenge you that. Now listen carefully to what I am saying
Here are some people in a line like and assembly line of our
school. And many other lines. Lalita found her place in this
array. Lalita kept this array sorted. The array was sorted
before too but Lalita captured position such a way that now
the array sorted too. Lalitan said that if he had more money
than Lalita then he too would be behind Lalita. Insertion sort
algorithm uses Insertion Sort algorithm to sort an array of
elements. The elements in array are 7, 2, 91, 77 and 91. We
need to insert an arbitrary element in a sorted array. So 91
will go to 7 and ask Are you bigger than me ? 7 will say I 'm
smaller. 91 will ask 77 whether you are greater or smaller ? It
is bigger so 77 will ask it to shift one step.
Insertion Sort in C Language (With
Explanation)
CodeWithHarry
After watching this video, I guarantee you to teach coding of
insertion sort. And your responsiblity is to watch this video to
understand insertion sort properly. In this video we 'll be
doing only insertion sort and nothing else. In insertion sort
we assume that the 1st element is sorted. And that is a
sorted array, and other side array is not sorted. Now when we
will insert 2 in this array, what will happen ? 2 will come
, here , but how ? Let me show you how. We have not made
insertion sort function. So I 'll write it as , printArray And I
have written , A It will print the array A. So here , there is
some problem , that is I have to also write n here. Now when
I'll run it it will print all the elements. No surprises but so
ever, 12 , 54 , 65 , 7 , 23 , 9 and 9. Loop for passes means 1st
pass, 2nd pass, 3rd pass, and 1th pass. If I am counting from 1
to 1 it will go till n - 1 I can write < = n 1 or < n < n. How many
time I 'll run it , how many passes are there , its n-1 So , if I'm
counting from index 1 I am going till last index. And
completing all the passes. Loop will work here.
Key will be inserted one by one in left array. First 54 then 65,
7, 23 , 9 and 65 are inserted. If A [ j ] means 65 is < key then
okay I will stop. If this condition will be false like in this case it
is true so it will go inside while loop. If false, then it will stay
here. Then if I do n't add one more condition here Then A will
start comparing 7 to -1 And A [ -1 ] is nothing. I will revise it
once more in the video. Sorting algorithm can be easily
understood in the whole DSA. In fact it is something which
you can explain to a layman. to anyone in this world. The
code is very small but Understanding is simple to just like
code. I wo n't say it is difficult to understand. I do n't know
why people make sorting algorithms so complicated. So , I
will run this code step by step and after that I will show you
sorting here. So , you can see here I have written elements
like this. and I am writing nine as 09 so that my index will be
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller abdullahkhan7. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $7.99. You're not tied to anything after your purchase.