Lecture 4- Bubble Sort -Competitive Programming Problem 2

Krish Naik · Intermediate ·📐 ML Fundamentals ·6y ago

Key Takeaways

Explains the Bubble Sort algorithm, a simple sorting technique used in competitive programming

Full Transcript

hello my name is Krishna and welcome to my youtube channel so guys today we'll be discussing our lecture 4 that is regarding bubble sort algorithm first of all we try to understand this and after the after sometime I'll be actually giving you a competitive programming which you have to solve by using sorting algorithms now there are many sorting algorithms like selection chart bubble chart merge sort so we'll be discussing one by one and based on that I'll also be giving you a problem statement at the end and the solution will actually be discussed in the next video so first of all let us go and go ahead and try to understand what how exactly bubble sort works which is a pretty much algorithm all together most of the sorting algorithms where you most of the people commonly use it but there's still a lot of things that we need to note down in bubble sort like dieting constant how much time it is actually taking so first of all let us go ahead and try to understand how does bubble sort actually works and suppose I have an array which I am going to denote it as a which has elements like 7 5 1 2 3 now we need to sort this particular numbers that are actually present in this particular array now how does bubble sort actually work so first of all bubble sort actually considers the first element okay then what it does is that tries to call it goes and compares with the next element so this element is suppose it is present in a and this element is present in B ok so first of all what will happen is that I'll not take a and D instead I can take it as a of I is equal to 7 a of I plus 1 is equal to 5 then also I'll try to write a pseudo code so that it will be very very easy for you to understand now this particular elements will actually be compared suppose if 7 is greater than 5 okay now in this particular case 7 is greater than 5 then what we do is that we basically swap these elements now when we swap this elements the first element actually becomes 5 and the next element actually becomes 7 after that now after one sort what will happen is that this 5 will be here 7 will be here 1 will be here and do the 3 will be here then again the loop will get continued now we have the current pointer over here itself this 7 and 1 is actually getting compared now again we can find out over here is that 7 is greater than 1 so again or swap will happen now in this particular case one will again come to this particular side then it will become seven it will become two then three then again the same step is actually you know repeated where seven will I get actually compared to 2 again 2 is actually smaller than 7 will exchange we have to swap it so we have something like this finally again it is getting compared to 3 and finally my order will become like this ok so 3 & 7 now in my next loop again again the first element 5 is taken it is going to get compared to the next element like how we have compared over here in my second loop a of I will get now initialized to 5 okay then a of I plus 1 will get initialized to 1 okay then this 2 elements will be compared now what will happen is that as the comparison is happening 5 is greater than 1 so we will be actually swapping it now in this particular case after this first repetition of first iteration is done in the second iteration I will be having 1 7 1 5 2 3 7 right then again the pointer will get increased in the next element is this and a plus 1 I plus 1 element is this this too will get compared again this will be 1 2 5 3 7 okay then after this this 2 element will get compared after it is getting compared again 5 is greater than 3 so this will be swapped and finally will be getting like this now when this two elements are getting compared what will happen now it is basically going to see that ok 5 is less than 7 it is not going to do the swap okay so only the swap is basically happening when the previous element is greater than the next element now again in the third iteration you can see all the numbers are actually ordered so nothing will happen no so app will actually happen okay so this is the basic scenario of bubble sort and after this we will try to understand the pseudo code of bubble sort okay and will try to see that how we can actually write a simple pseudo code so let me just wrap this quickly but I hope you have understood this now in the pseudo code when I am writing actually the pseudo code right the first thing is that I will be running a loop suppose I say from K is equal to 1 to n minus 1 okay so this is the loop the whole loop I'm actually taking it up my next loop what I'll do is that I'll say for I is equal to 0 to n minus 1 okay that basically means now my eye is over here okay I is over here inside this what I'm going to do first of all I'm going to understand I've a of I is equal to I and sorry I should not write like this instead what I can do is that I can say that a is equal to a of I comma B is equal to a of I plus 1 that basically means the first element is going to a the second element is going to be now what will happen is that I'll just compare if is less than B or if a is greater than B okay what I'm going to do I'm going to basically swap a of I with a of I plus 1 okay I'm going to swap it okay all right I don't have to take this a and B also it is basically just creating in more memory so I can actually swap it after swapping it what will happen again this will be in another loop okay then it will go again back it will repeat this particular loop the next element will be pointing here and here again the same condition will be taking us again it will be swapping okay but when we reach over here in this particular situation when I have five seven when this two elements will be actually compared this condition is not going to be true so it is going to come out and it is getting repeated with respect to the outer loop so after every one month cycle what is going to happen is that first of all one element will be compared with every other elements okay then in the second loop the next element the next element which is in the first that is going to get compared with every other numbers then in the third condition third loop third eye' iteration what will happen which is the element present in the first that will be compared to all the numbers like this after some n number n minus 1 number of steps it is going to basically get sorted now the main thing that comes up is about the time constraint about the time constraint and guys please do not focus on this you know syntax this is just a pseudo code right time constraint if I want to find out D of n over here you can see that n minus 1 loop is basically taking place so I am going to write it as n minus 1 multiplied I'm n minus one the inner loop that many number of times it is going to run then this I can consider it as C because this is just one step process right one step so we basically consider this as a constant okay now okay I'll multiply with C okay now if I try to multiply this what will happen is that I will be getting CN square plus C n minus C right so this is what I'm going to get over here you can see that n square is the maximum value that I'm actually getting so my T of n is actually oo of n square so this is my worst case till this particular execution will take place now the best case basically means that already if the number is sorted then how many number of iteration it is going to take place so for that I can actually give Omega of n okay so Omega is basically the lower bound o is basically the worst case this is basically the best case now why am i am taking because just imagine all the number is sorted at that time I do not have to I trait again and again you know and it will be just getting completed in n number of steps now this is a brief process about bubbles are pretty much easier organism now I will come to a competitive programming that is problem number two which you have to solve it initially and then if you are getting the answer please do comment down in the comment section I'll be having a look and if you are right I will also be giving the solution in the next I'll also be telling your name who has actually given that particular code so you all can actually give it over there I'll try to mention as much as possible all your names now the problem statement is suppose I have an array a and suppose I have numbers like suppose I have numbers like 1 2 6 3 suppose I say 1 4 okay now I need to find out how many number of elements are not in ascending order ok how many number of elements are not in ascending order over here you can see one element is not in ascending order this element is also not in ascending order this element is also not in ascending order and this element is also not in ascending order whereas these two elements are in ascending order right so the total number of elements that are not in ascending order is 4 so you have to try to write some sorting algorithms and to see some logic whether you are able to get home and what is the count of the numbers that are not in ascending order pretty much simple guys because see now you may be knowing the sorting algorithm this is the problem that I was actually talking about you may be knowing the sorting algorithm but when you are trying to apply that particular logic in this particular problem statement it becomes very very difficult yes I'll say that it is not possible it is definitely possible definitely you'll be able to write it down but definitely you will take some amount of time so it is good that you try to see you try to research you try to write a small sample of code and try to get this particular answer this particular solution will be shared in my next video which will be we will be discussing about selection shot so yes this was all about this particular video I hope you like this particular video please do subscribe the channel if you have not I discourage you all in the next video have a great day thank you wonder dog bye bye

Original Description

Please join as a member in my channel to get additional benefits like materials in Data Science, live streaming for Members and many more https://www.youtube.com/channel/UCNU_lfiiWBdtULKOw6X0Dig/join Please do subscribe my other channel too https://www.youtube.com/channel/UCjWY5hREA6FFYrthD0rZNIw Connect with me here: Twitter: https://twitter.com/Krishnaik06 Facebook: https://www.facebook.com/krishnaik06 instagram: https://www.instagram.com/krishnaik06
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Krish Naik · Krish Naik · 0 of 60

← Previous Next →
1 Natural Language Processing|Stemming
Natural Language Processing|Stemming
Krish Naik
2 Natural Language Processing|BagofWords
Natural Language Processing|BagofWords
Krish Naik
3 Gaussian distribution or Normal Distribution in statisctics
Gaussian distribution or Normal Distribution in statisctics
Krish Naik
4 Natural Language Processing|TF-IDF for Machine Learning| Text Prerocessing
Natural Language Processing|TF-IDF for Machine Learning| Text Prerocessing
Krish Naik
5 Log Normal Distribution in Statistics
Log Normal Distribution in Statistics
Krish Naik
6 Covariance in Statistics
Covariance in Statistics
Krish Naik
7 Confusion matrix, Precision, Recall| Data Science Interview questions
Confusion matrix, Precision, Recall| Data Science Interview questions
Krish Naik
8 Tutorial 44-Balanced vs Imbalanced Dataset and how to handle Imbalanced Dataset
Tutorial 44-Balanced vs Imbalanced Dataset and how to handle Imbalanced Dataset
Krish Naik
9 Implementing a Spam classifier in python| Natural Language Processing
Implementing a Spam classifier in python| Natural Language Processing
Krish Naik
10 Tutorial 11-Exploratory Data Analysis(EDA) of Titanic dataset
Tutorial 11-Exploratory Data Analysis(EDA) of Titanic dataset
Krish Naik
11 Face Recognition using open CV and VGG 16 Transfer Learning
Face Recognition using open CV and VGG 16 Transfer Learning
Krish Naik
12 Pedestrian Detection using OpenCV from Videos
Pedestrian Detection using OpenCV from Videos
Krish Naik
13 Face and Eye Detection from Videos using HAAR Cascade Classifier
Face and Eye Detection from Videos using HAAR Cascade Classifier
Krish Naik
14 Reading, Writing and Displaying images with Opencv| OpenCV Tutorial
Reading, Writing and Displaying images with Opencv| OpenCV Tutorial
Krish Naik
15 OpenCV Installation | OpenCV tutorial
OpenCV Installation | OpenCV tutorial
Krish Naik
16 Face and Eye Detection from Images using HAAR Cascade Classifier
Face and Eye Detection from Images using HAAR Cascade Classifier
Krish Naik
17 Car Detection using HAAR Cascade and Opencv from Videos.
Car Detection using HAAR Cascade and Opencv from Videos.
Krish Naik
18 Using OpenFace for Face recognition in Keras
Using OpenFace for Face recognition in Keras
Krish Naik
19 OpenPose Tutorial with Tensorflow
OpenPose Tutorial with Tensorflow
Krish Naik
20 Multiple Linear Regression using python and sklearn
Multiple Linear Regression using python and sklearn
Krish Naik
21 Dimensional Reduction| Principal Component Analysis
Dimensional Reduction| Principal Component Analysis
Krish Naik
22 Movie Recommender System using Python
Movie Recommender System using Python
Krish Naik
23 TPR,FPR,FNR,TNR, Confusion Matrix
TPR,FPR,FNR,TNR, Confusion Matrix
Krish Naik
24 Precision, Recall and F1-Score
Precision, Recall and F1-Score
Krish Naik
25 Artificial Neural Network for Customer's Exit Prediction from Bank
Artificial Neural Network for Customer's Exit Prediction from Bank
Krish Naik
26 GridSearchCV- Select the best hyperparameter for any Classification Model
GridSearchCV- Select the best hyperparameter for any Classification Model
Krish Naik
27 RandomizedSearchCV- Select the best hyperparameter for any Classification Model
RandomizedSearchCV- Select the best hyperparameter for any Classification Model
Krish Naik
28 K Nearest Neighbor classification with Intuition and practical solution
K Nearest Neighbor classification with Intuition and practical solution
Krish Naik
29 K Means Clustering Intuition
K Means Clustering Intuition
Krish Naik
30 Create custom Alexa Skill- Lambda function- Part2
Create custom Alexa Skill- Lambda function- Part2
Krish Naik
31 Hierarchical Clustering intuition
Hierarchical Clustering intuition
Krish Naik
32 Implement Transfer Learning with a generic Code Template
Implement Transfer Learning with a generic Code Template
Krish Naik
33 Gender Classifier and Age Estimator using Resnet Convolution Neural Network
Gender Classifier and Age Estimator using Resnet Convolution Neural Network
Krish Naik
34 Unlock Your Application With Your Face using OpenCV
Unlock Your Application With Your Face using OpenCV
Krish Naik
35 Draw rectangle from webcam and sketch process it on a live feed
Draw rectangle from webcam and sketch process it on a live feed
Krish Naik
36 Complete Life Cycle of a Data Science Project
Complete Life Cycle of a Data Science Project
Krish Naik
37 How we can apply Machine Learning in Finance
How we can apply Machine Learning in Finance
Krish Naik
38 Deep Learning in Medical Science
Deep Learning in Medical Science
Krish Naik
39 How to switch your career to Data Science.
How to switch your career to Data Science.
Krish Naik
40 Linear Regression Mathematical Intuition
Linear Regression Mathematical Intuition
Krish Naik
41 Handle Categorical features using Python
Handle Categorical features using Python
Krish Naik
42 Machine Learning Algorithm- Which one to choose for your Problem?
Machine Learning Algorithm- Which one to choose for your Problem?
Krish Naik
43 DBSCAN Clustering Easily Explained with Implementation
DBSCAN Clustering Easily Explained with Implementation
Krish Naik
44 Curse of Dimensionality Easily explained| Machine Learning
Curse of Dimensionality Easily explained| Machine Learning
Krish Naik
45 Feature Selection Techniques Easily Explained | Machine Learning
Feature Selection Techniques Easily Explained | Machine Learning
Krish Naik
46 Tutorial 29-R square and Adjusted R square Clearly Explained| Machine Learning
Tutorial 29-R square and Adjusted R square Clearly Explained| Machine Learning
Krish Naik
47 Cross Validation using sklearn and python | Machine Learning
Cross Validation using sklearn and python | Machine Learning
Krish Naik
48 Handling Missing Data Easily Explained| Machine Learning
Handling Missing Data Easily Explained| Machine Learning
Krish Naik
49 Deploy Machine Learning Model using Flask
Deploy Machine Learning Model using Flask
Krish Naik
50 Deployment of Deep Learning Model using Flask
Deployment of Deep Learning Model using Flask
Krish Naik
51 How to Visualize Multiple Linear Regression in python
How to Visualize Multiple Linear Regression in python
Krish Naik
52 K Nearest Neighbour Easily Explained with Implementation
K Nearest Neighbour Easily Explained with Implementation
Krish Naik
53 Predicting Heart Disease using Machine Learning
Predicting Heart Disease using Machine Learning
Krish Naik
54 Predicting Lungs Disease using Deep Learning
Predicting Lungs Disease using Deep Learning
Krish Naik
55 Stock Sentiment Analysis using News Headlines
Stock Sentiment Analysis using News Headlines
Krish Naik
56 Random Forest(Bootstrap Aggregation) Easily Explained
Random Forest(Bootstrap Aggregation) Easily Explained
Krish Naik
57 Voting Classifier(Hard Voting and Soft Voting Classifier)
Voting Classifier(Hard Voting and Soft Voting Classifier)
Krish Naik
58 Credit Card Fraud Detection using Machine Learning from Kaggle
Credit Card Fraud Detection using Machine Learning from Kaggle
Krish Naik
59 Hyperparameter Optimization for Xgboost
Hyperparameter Optimization for Xgboost
Krish Naik
60 Tutorial 45-Handling imbalanced Dataset  using python- Part 1
Tutorial 45-Handling imbalanced Dataset using python- Part 1
Krish Naik

Related AI Lessons

Up next
Learn Deep Learning by Hand (Beginner's Guide - Part 1)
Thu Vu
Watch →