diff --git a/.gitattributes b/.gitattributes index c5153b9a0810c1777d6e6f85718603df0796cd3a..792bacb47cdef59be8689d4c1a55cd5aebeb6e93 100644 --- a/.gitattributes +++ b/.gitattributes @@ -157,3 +157,52 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text 05[[:space:]]-[[:space:]]JDBC[[:space:]](Java[[:space:]]Database[[:space:]]Connectivity)/004[[:space:]]Postgres[[:space:]]Library[[:space:]]jar.mp4 filter=lfs diff=lfs merge=lfs -text 05[[:space:]]-[[:space:]]JDBC[[:space:]](Java[[:space:]]Database[[:space:]]Connectivity)/007[[:space:]]Fetching[[:space:]]all[[:space:]]Records.mp4 filter=lfs diff=lfs merge=lfs -text 05[[:space:]]-[[:space:]]JDBC[[:space:]](Java[[:space:]]Database[[:space:]]Connectivity)/005[[:space:]]Connecting[[:space:]]Java[[:space:]]and[[:space:]]DB.mp4 filter=lfs diff=lfs merge=lfs -text +05[[:space:]]-[[:space:]]JDBC[[:space:]](Java[[:space:]]Database[[:space:]]Connectivity)/006[[:space:]]Execute[[:space:]]and[[:space:]]Process.mp4 filter=lfs diff=lfs merge=lfs -text +05[[:space:]]-[[:space:]]JDBC[[:space:]](Java[[:space:]]Database[[:space:]]Connectivity)/009[[:space:]]Problems[[:space:]]with[[:space:]]Statement.mp4 filter=lfs diff=lfs merge=lfs -text +05[[:space:]]-[[:space:]]JDBC[[:space:]](Java[[:space:]]Database[[:space:]]Connectivity)/008[[:space:]]Crud[[:space:]]operations.mp4 filter=lfs diff=lfs merge=lfs -text +05[[:space:]]-[[:space:]]JDBC[[:space:]](Java[[:space:]]Database[[:space:]]Connectivity)/010[[:space:]]PreparedStatement.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/001[[:space:]]Hibernate[[:space:]]Section[[:space:]]Introduction.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/003[[:space:]]Project[[:space:]]setup.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/002[[:space:]]What[[:space:]]is[[:space:]]Hibernate.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/004[[:space:]]Failed[[:space:]]attempt[[:space:]]to[[:space:]]save[[:space:]]data.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/007[[:space:]]Refactoring.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/005[[:space:]]Successful[[:space:]]attempt[[:space:]]to[[:space:]]save[[:space:]]data.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/008[[:space:]]Fetching[[:space:]]the[[:space:]]data.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/009[[:space:]]Update[[:space:]]and[[:space:]]Delete[[:space:]]Objects.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/006[[:space:]]Show[[:space:]]SQL[[:space:]]config.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/010[[:space:]]Changing[[:space:]]Table[[:space:]]and[[:space:]]Column[[:space:]]names.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/013[[:space:]]OneToOne[[:space:]]Mapping.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/014[[:space:]]OneToMany[[:space:]]and[[:space:]]ManyToOne.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/012[[:space:]]Mapping[[:space:]]Relationship[[:space:]]Theory.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/015[[:space:]]Manytomany[[:space:]]Mapping[[:space:]]2.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/016[[:space:]]Hibernate[[:space:]]Eager[[:space:]]And[[:space:]]Lazy[[:space:]]Fetch[[:space:]]2.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/011[[:space:]]Embeddable.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/017[[:space:]]Hiberntae[[:space:]]Caching.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/020[[:space:]]Fetching[[:space:]]with[[:space:]]filter[[:space:]]and[[:space:]]specific[[:space:]]properties.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/019[[:space:]]Fetching[[:space:]]Data[[:space:]]using[[:space:]]HQL.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/021[[:space:]]Get[[:space:]]vs[[:space:]]Load.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/018[[:space:]]HQL[[:space:]]Introduction.mp4 filter=lfs diff=lfs merge=lfs -text +06[[:space:]]-[[:space:]]Hibernate/022[[:space:]]Level[[:space:]]2[[:space:]]cache[[:space:]]using[[:space:]]Ehcache.mp4 filter=lfs diff=lfs merge=lfs -text +07[[:space:]]-[[:space:]]Spring[[:space:]]Getting[[:space:]]Started/001[[:space:]]Introduction[[:space:]]To[[:space:]]Spring.mp4 filter=lfs diff=lfs merge=lfs -text +07[[:space:]]-[[:space:]]Spring[[:space:]]Getting[[:space:]]Started/002[[:space:]]Spring[[:space:]]Docs.mp4 filter=lfs diff=lfs merge=lfs -text +07[[:space:]]-[[:space:]]Spring[[:space:]]Getting[[:space:]]Started/003[[:space:]]Prerequisites.mp4 filter=lfs diff=lfs merge=lfs -text +07[[:space:]]-[[:space:]]Spring[[:space:]]Getting[[:space:]]Started/004[[:space:]]IDE[[:space:]]For[[:space:]]Spring.mp4 filter=lfs diff=lfs merge=lfs -text +07[[:space:]]-[[:space:]]Spring[[:space:]]Getting[[:space:]]Started/005[[:space:]]IoC[[:space:]]And[[:space:]]DI.mp4 filter=lfs diff=lfs merge=lfs -text +07[[:space:]]-[[:space:]]Spring[[:space:]]Getting[[:space:]]Started/006[[:space:]]Spring[[:space:]]Vs[[:space:]]Spring[[:space:]]Boot.mp4 filter=lfs diff=lfs merge=lfs -text +07[[:space:]]-[[:space:]]Spring[[:space:]]Getting[[:space:]]Started/009[[:space:]]Autowiring[[:space:]]In[[:space:]]Spring[[:space:]]Boot.mp4 filter=lfs diff=lfs merge=lfs -text +07[[:space:]]-[[:space:]]Spring[[:space:]]Getting[[:space:]]Started/008[[:space:]]DI[[:space:]]Using[[:space:]]Spring[[:space:]]Boot.mp4 filter=lfs diff=lfs merge=lfs -text +07[[:space:]]-[[:space:]]Spring[[:space:]]Getting[[:space:]]Started/007[[:space:]]First[[:space:]]Spring[[:space:]]Boot[[:space:]]App.mp4 filter=lfs diff=lfs merge=lfs -text +08[[:space:]]-[[:space:]]Exploring[[:space:]]Spring[[:space:]]Framework/003[[:space:]]Object[[:space:]]Creation.mp4 filter=lfs diff=lfs merge=lfs -text +08[[:space:]]-[[:space:]]Exploring[[:space:]]Spring[[:space:]]Framework/002[[:space:]]Spring[[:space:]]Bean[[:space:]]Xml[[:space:]]Config.mp4 filter=lfs diff=lfs merge=lfs -text +08[[:space:]]-[[:space:]]Exploring[[:space:]]Spring[[:space:]]Framework/004[[:space:]]Scopes.mp4 filter=lfs diff=lfs merge=lfs -text +08[[:space:]]-[[:space:]]Exploring[[:space:]]Spring[[:space:]]Framework/001[[:space:]]Spring[[:space:]]1st[[:space:]]Project.mp4 filter=lfs diff=lfs merge=lfs -text +08[[:space:]]-[[:space:]]Exploring[[:space:]]Spring[[:space:]]Framework/005[[:space:]]Setter[[:space:]]Injection.mp4 filter=lfs diff=lfs merge=lfs -text +08[[:space:]]-[[:space:]]Exploring[[:space:]]Spring[[:space:]]Framework/006[[:space:]]Ref[[:space:]]Attribute.mp4 filter=lfs diff=lfs merge=lfs -text +08[[:space:]]-[[:space:]]Exploring[[:space:]]Spring[[:space:]]Framework/008[[:space:]]Creating[[:space:]]Interface.mp4 filter=lfs diff=lfs merge=lfs -text +08[[:space:]]-[[:space:]]Exploring[[:space:]]Spring[[:space:]]Framework/009[[:space:]]Autowiring.mp4 filter=lfs diff=lfs merge=lfs -text +08[[:space:]]-[[:space:]]Exploring[[:space:]]Spring[[:space:]]Framework/007[[:space:]]Constructor[[:space:]]Injection.mp4 filter=lfs diff=lfs merge=lfs -text +08[[:space:]]-[[:space:]]Exploring[[:space:]]Spring[[:space:]]Framework/010[[:space:]]Primary[[:space:]]Bean.mp4 filter=lfs diff=lfs merge=lfs -text +08[[:space:]]-[[:space:]]Exploring[[:space:]]Spring[[:space:]]Framework/011[[:space:]]Lazy[[:space:]]Init[[:space:]]Bean.mp4 filter=lfs diff=lfs merge=lfs -text +08[[:space:]]-[[:space:]]Exploring[[:space:]]Spring[[:space:]]Framework/012[[:space:]]Getbean[[:space:]]By[[:space:]]Type.mp4 filter=lfs diff=lfs merge=lfs -text +08[[:space:]]-[[:space:]]Exploring[[:space:]]Spring[[:space:]]Framework/013[[:space:]]Inner[[:space:]]Bean.mp4 filter=lfs diff=lfs merge=lfs -text +09[[:space:]]-[[:space:]]Working[[:space:]]with[[:space:]]Java[[:space:]]Based[[:space:]]Configuration/002[[:space:]]Bean[[:space:]]Name.mp4 filter=lfs diff=lfs merge=lfs -text diff --git a/05 - JDBC (Java Database Connectivity)/006 Execute and Process.mp4 b/05 - JDBC (Java Database Connectivity)/006 Execute and Process.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..6b75dd49003d8af1bc7ddbff9ad4fecdbd872973 --- /dev/null +++ b/05 - JDBC (Java Database Connectivity)/006 Execute and Process.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c21ada6ea76bf50bf5758b0699d29127fc896cf79b2c9b8088b410e88b6adced +size 122373144 diff --git a/05 - JDBC (Java Database Connectivity)/008 Crud operations.mp4 b/05 - JDBC (Java Database Connectivity)/008 Crud operations.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..793fde6293df10762a37aad97d59c2134fc3bccf --- /dev/null +++ b/05 - JDBC (Java Database Connectivity)/008 Crud operations.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1991b722e9638aecb4b21705fd490f9d143949bd26ab7a3b444df7f7975a5a7b +size 133255801 diff --git a/05 - JDBC (Java Database Connectivity)/009 Problems with Statement.mp4 b/05 - JDBC (Java Database Connectivity)/009 Problems with Statement.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..ceb35a885da29f306428312423041fd8754dcc25 --- /dev/null +++ b/05 - JDBC (Java Database Connectivity)/009 Problems with Statement.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3087b929a948f6748d1ff39fee3c34e2f44ab4c8e056eccd97846d354a0af116 +size 43917351 diff --git a/05 - JDBC (Java Database Connectivity)/010 PreparedStatement.mp4 b/05 - JDBC (Java Database Connectivity)/010 PreparedStatement.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..5c8f197cea4020836886e9ae9549df59e2320d4a --- /dev/null +++ b/05 - JDBC (Java Database Connectivity)/010 PreparedStatement.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:eccebe2a30378440f8fa95ba7bbdafee4bbc7d136415c3af746744ce694a0f25 +size 45192208 diff --git a/06 - Hibernate/001 Hibernate Section Introduction.mp4 b/06 - Hibernate/001 Hibernate Section Introduction.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..b978ce907392758bc7a9294792f71d2798296ed6 --- /dev/null +++ b/06 - Hibernate/001 Hibernate Section Introduction.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a35e4045c9f997086093de4c64063bd66791f602c879334c8847f51f23789446 +size 55675636 diff --git a/06 - Hibernate/002 What is Hibernate.mp4 b/06 - Hibernate/002 What is Hibernate.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..853e65d3a35773db81c6825cdac7dfa418bfca66 --- /dev/null +++ b/06 - Hibernate/002 What is Hibernate.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f516b2a746e5f63bfe6152f20f4de10752181b20a28793bc80049860ff10d70c +size 170799701 diff --git a/06 - Hibernate/003 Project setup.mp4 b/06 - Hibernate/003 Project setup.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..4cac5c0d75b425bf8a28ad984ab8480e6c233d69 --- /dev/null +++ b/06 - Hibernate/003 Project setup.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:63a8c1fd927c08aa1f5c6ad55b1dbde300a6486843fff67f5ec92d8d3452b951 +size 92995523 diff --git a/06 - Hibernate/004 Failed attempt to save data.mp4 b/06 - Hibernate/004 Failed attempt to save data.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..c7a3d36c6abea2481756f3d31b4de50792dd077b --- /dev/null +++ b/06 - Hibernate/004 Failed attempt to save data.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:972f2023705ac6494bc9c4103bc0df41d51afc5a014b0b5fbd97b8bd7483a521 +size 147327698 diff --git a/06 - Hibernate/005 Successful attempt to save data.mp4 b/06 - Hibernate/005 Successful attempt to save data.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..a1dd077c8b842d197e3308eb213450ed601dafb8 --- /dev/null +++ b/06 - Hibernate/005 Successful attempt to save data.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7196947b14f1612bd6bb6dd9a0245c01cb8fc0ee16e8d3b518b675aca0758719 +size 181497343 diff --git a/06 - Hibernate/006 Show SQL config.mp4 b/06 - Hibernate/006 Show SQL config.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..f3236ed08bf774c1f83645a250b02c92cba0cebd --- /dev/null +++ b/06 - Hibernate/006 Show SQL config.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:12860438e93dac9647e1d5e7bd26244ad1d52a285b0f9de425d165ce8fc2671b +size 97836933 diff --git a/06 - Hibernate/007 Refactoring.mp4 b/06 - Hibernate/007 Refactoring.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..3c0f30216904852933a5b81dc507a6c195c7dd36 --- /dev/null +++ b/06 - Hibernate/007 Refactoring.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0b5c1bab1dc2dea55708d932a407e44e4aa0401ee561d658413c03486e3f6923 +size 36324495 diff --git a/06 - Hibernate/008 Fetching the data.mp4 b/06 - Hibernate/008 Fetching the data.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..31051365ea8f5290adcd666caed38f2e0fee727c --- /dev/null +++ b/06 - Hibernate/008 Fetching the data.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ec4a2886cfd8986e19d5e94437d4d740e0464bb664ff9778d78ef34e6b5ebe54 +size 54436244 diff --git a/06 - Hibernate/009 Update and Delete Objects.mp4 b/06 - Hibernate/009 Update and Delete Objects.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..cfa7134a4d3a8cd5d3ab935b0981287468694fc0 --- /dev/null +++ b/06 - Hibernate/009 Update and Delete Objects.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c4bb347e84c53985599a8a3d98fbda438081763921c0433c2232f0012e66bc96 +size 52861481 diff --git a/06 - Hibernate/010 Changing Table and Column names.mp4 b/06 - Hibernate/010 Changing Table and Column names.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..bda7180170859b04c2043854ac09c29b516eba39 --- /dev/null +++ b/06 - Hibernate/010 Changing Table and Column names.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4029cb9a6c1fd7ee71affbf41d2744f4951e09f0e5eb3f8eeb3ef96c573de5bc +size 60808574 diff --git a/06 - Hibernate/011 Embeddable.mp4 b/06 - Hibernate/011 Embeddable.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..20c94c30c914aa87107f6571a11f954495e4a570 --- /dev/null +++ b/06 - Hibernate/011 Embeddable.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0ba70434b6493dd4ff992341a2e2af648eacaa04e876223a7c428b7621ba105a +size 228779704 diff --git a/06 - Hibernate/012 Mapping Relationship Theory.mp4 b/06 - Hibernate/012 Mapping Relationship Theory.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..8b6f9fb0f7ceb077966023003b02584618c22a3d --- /dev/null +++ b/06 - Hibernate/012 Mapping Relationship Theory.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ddb0b878887aaa0cf684999d413b078e86f7f89edda5146f16b0e7b6cca814d2 +size 244740202 diff --git a/06 - Hibernate/013 OneToOne Mapping.mp4 b/06 - Hibernate/013 OneToOne Mapping.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..5599340598f52b848ab263a6f995d3b05297d7f0 --- /dev/null +++ b/06 - Hibernate/013 OneToOne Mapping.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7fb881fccd915cbbb067b6ec1387170f2ef9f5e5de7dd63dd473ddddd692502a +size 76990391 diff --git a/06 - Hibernate/014 OneToMany and ManyToOne.mp4 b/06 - Hibernate/014 OneToMany and ManyToOne.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..9428e47a99c4ac55ae55ce6409ddcc673f371c06 --- /dev/null +++ b/06 - Hibernate/014 OneToMany and ManyToOne.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4c30058345b5ed45546932ae4ba9e12a024510830087a6109156d54681374362 +size 86686203 diff --git a/06 - Hibernate/015 Manytomany Mapping 2.mp4 b/06 - Hibernate/015 Manytomany Mapping 2.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..3109f48f2a66cee1f9e02893fc6e6097ba5ff433 --- /dev/null +++ b/06 - Hibernate/015 Manytomany Mapping 2.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:420b3d506fcf186ee8a3b0816f8ed9da1fda43bda28c828de9c6ad5b4e90b648 +size 135779041 diff --git a/06 - Hibernate/016 Hibernate Eager And Lazy Fetch 2.mp4 b/06 - Hibernate/016 Hibernate Eager And Lazy Fetch 2.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..02c46869da451dc45df224083632afb0cda849e4 --- /dev/null +++ b/06 - Hibernate/016 Hibernate Eager And Lazy Fetch 2.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:768a5a800ce252d4167cf91bbb13eadaf5eb415be2f0d6f02ac43fe43b9d9751 +size 140172378 diff --git a/06 - Hibernate/017 Hiberntae Caching.mp4 b/06 - Hibernate/017 Hiberntae Caching.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..676f2a1aca2eb8af82d628181dbeb1b956f871b4 --- /dev/null +++ b/06 - Hibernate/017 Hiberntae Caching.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:09d01f4e34af4181326ff8541e554ce95721bc9ca8e9feb6f3443b99b2398960 +size 88304204 diff --git a/06 - Hibernate/018 HQL Introduction.mp4 b/06 - Hibernate/018 HQL Introduction.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..d21cafcf5c42b7d0aa89a7a88a2f7a78e7059597 --- /dev/null +++ b/06 - Hibernate/018 HQL Introduction.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:955b14d4a7d6605c108f60fc9adeccdb53b03a1daac5ecd2b2a9f8b64c86c35f +size 162578000 diff --git a/06 - Hibernate/019 Fetching Data using HQL.mp4 b/06 - Hibernate/019 Fetching Data using HQL.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..b9eed5c3a6cd84017c4364075b6a6dcbdfa6eee7 --- /dev/null +++ b/06 - Hibernate/019 Fetching Data using HQL.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f51884c4cfec99c49b2248d39793cfd807456c8bb4ba9e53d8068446df417275 +size 56447360 diff --git a/06 - Hibernate/020 Fetching with filter and specific properties.mp4 b/06 - Hibernate/020 Fetching with filter and specific properties.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..82b0bbabd269533e406df7156d3a78487fa7f533 --- /dev/null +++ b/06 - Hibernate/020 Fetching with filter and specific properties.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9a8e08dbc4d250b8057b81ccd85ca887848638bed740dd2784a5fdde423ce01c +size 57621753 diff --git a/06 - Hibernate/021 Get vs Load.mp4 b/06 - Hibernate/021 Get vs Load.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..b65935962400f86f2ceb29dc5c38fa4aa2ced37f --- /dev/null +++ b/06 - Hibernate/021 Get vs Load.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:83bd3138ba5364cbaae98b5c0de0466340d5cdce8cd3f95038be90c25d1daf56 +size 85885024 diff --git a/06 - Hibernate/022 Level 2 cache using Ehcache.mp4 b/06 - Hibernate/022 Level 2 cache using Ehcache.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..1222a88e13cb177b1f3811ce3a9169d876a99f13 --- /dev/null +++ b/06 - Hibernate/022 Level 2 cache using Ehcache.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8196b151f6c4ce8bed788dde53ff4f84607d41f282fab99d879ccfb60a1960bb +size 108139474 diff --git a/07 - Spring Getting Started/001 Introduction To Spring.mp4 b/07 - Spring Getting Started/001 Introduction To Spring.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..330008e185f1479dcf762a9ca2d3f617db87c79f --- /dev/null +++ b/07 - Spring Getting Started/001 Introduction To Spring.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:555d3081ce120a246643b14d8f3bc12d84ffd6157ca6a330b6a670175daf1f41 +size 88436892 diff --git a/07 - Spring Getting Started/002 Spring Docs.mp4 b/07 - Spring Getting Started/002 Spring Docs.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..13b22d9d05f4a64023bf4e24cfd39f003326bbfa --- /dev/null +++ b/07 - Spring Getting Started/002 Spring Docs.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6ae8aa7b63c94506219e7a16d9f0bf4f4d5610fe93329488d4ad9385784ec203 +size 30197581 diff --git a/07 - Spring Getting Started/003 Prerequisites.mp4 b/07 - Spring Getting Started/003 Prerequisites.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..9345b9d1f810219b6910ab5162de0d54945c54eb --- /dev/null +++ b/07 - Spring Getting Started/003 Prerequisites.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:99a8fa294c7238db863faccec9cd9c76305769fb73800147409d29e67682b66d +size 46933378 diff --git a/07 - Spring Getting Started/004 IDE For Spring.mp4 b/07 - Spring Getting Started/004 IDE For Spring.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..0dc90fcae18085ff192af8220dd02d62070d3574 --- /dev/null +++ b/07 - Spring Getting Started/004 IDE For Spring.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c58a8a87da3c64236ef13050ef673128930420f60b49d10d6bd7609d0106d9fb +size 127422636 diff --git a/07 - Spring Getting Started/005 IoC And DI.mp4 b/07 - Spring Getting Started/005 IoC And DI.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..3c3d40207021ca055a521c990ed8e998b3b505d3 --- /dev/null +++ b/07 - Spring Getting Started/005 IoC And DI.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a2e724b4eef334c259831ee390e0bb5be652e2a4cea7d63b6c6d1b238c5ba042 +size 143451743 diff --git a/07 - Spring Getting Started/006 Spring Vs Spring Boot.mp4 b/07 - Spring Getting Started/006 Spring Vs Spring Boot.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..1aa70d706de24592adc8e64a72042b187d45fe7c --- /dev/null +++ b/07 - Spring Getting Started/006 Spring Vs Spring Boot.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a5bbdcab1b80c5a3f5ad1cf84cca0a72ef9b66862e6a90b14a4c1cc996c72be5 +size 77406878 diff --git a/07 - Spring Getting Started/007 First Spring Boot App.mp4 b/07 - Spring Getting Started/007 First Spring Boot App.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..e103ed993396ca03d6a1083519d6606b573d6c97 --- /dev/null +++ b/07 - Spring Getting Started/007 First Spring Boot App.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2f4c6fc9e0a240daf8c0d04d30afb176bbec28ed2c30cf180452ad8b806618fc +size 137810829 diff --git a/07 - Spring Getting Started/008 DI Using Spring Boot.mp4 b/07 - Spring Getting Started/008 DI Using Spring Boot.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..eabf9a6d6b63449b85e02066798877dd1b15464c --- /dev/null +++ b/07 - Spring Getting Started/008 DI Using Spring Boot.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dcf08fd203eaa8ac38a0aede377cda2c4189ee82f21d7990aa522ba223e3fec5 +size 131461087 diff --git a/07 - Spring Getting Started/009 Autowiring In Spring Boot.mp4 b/07 - Spring Getting Started/009 Autowiring In Spring Boot.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..d3bd50068500cb2fcb984b3c5e889ddd867c4ca2 --- /dev/null +++ b/07 - Spring Getting Started/009 Autowiring In Spring Boot.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:15481ab835ec6513c31f5d368df98774e4c0c39f32858869819017ce84e052d3 +size 50909583 diff --git a/08 - Exploring Spring Framework/001 Spring 1st Project.mp4 b/08 - Exploring Spring Framework/001 Spring 1st Project.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..30e854827c45058d0885ac79fc8ef28dda52bf98 --- /dev/null +++ b/08 - Exploring Spring Framework/001 Spring 1st Project.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ac99e469392ed2ae35afae7d6cc7c62817536103ee8031dfcaede53b429b9c59 +size 283886392 diff --git a/08 - Exploring Spring Framework/002 Spring Bean Xml Config.mp4 b/08 - Exploring Spring Framework/002 Spring Bean Xml Config.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..8264d52fb58038add7979bf7853692c63ceebb2c --- /dev/null +++ b/08 - Exploring Spring Framework/002 Spring Bean Xml Config.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d6b6fda3fc0edc52d79481462fdb853e7db1f8304a486e016cbf47ffce015422 +size 183253335 diff --git a/08 - Exploring Spring Framework/003 Object Creation.mp4 b/08 - Exploring Spring Framework/003 Object Creation.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..a886b9ff9328fb4b6d3123a4125bb0981a10ce9d --- /dev/null +++ b/08 - Exploring Spring Framework/003 Object Creation.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4f55b075b6c464bdc8112e18c7c257daa67051aa7ec6115ea77c27c16e9b102d +size 48459922 diff --git a/08 - Exploring Spring Framework/004 Scopes.mp4 b/08 - Exploring Spring Framework/004 Scopes.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..76c51fc6f892845ecd486c8c0a6aca277c4aebd9 --- /dev/null +++ b/08 - Exploring Spring Framework/004 Scopes.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:86073a87f629b69b2bda104f526b32ea9d1d1235e14ce107d5a07cf52e4abdc9 +size 48381761 diff --git a/08 - Exploring Spring Framework/005 Setter Injection.mp4 b/08 - Exploring Spring Framework/005 Setter Injection.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..d1c128190be24ee3a5e224eb59ce66508119694a --- /dev/null +++ b/08 - Exploring Spring Framework/005 Setter Injection.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6d21dd67980770608ec02041757ef541f005e7fa7636ac1facd78176aee4e7d2 +size 35782899 diff --git a/08 - Exploring Spring Framework/006 Ref Attribute.mp4 b/08 - Exploring Spring Framework/006 Ref Attribute.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..37fb96d5d064bd36c3d35a40f53eb8fcd1abafd1 --- /dev/null +++ b/08 - Exploring Spring Framework/006 Ref Attribute.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d5b6649497f4fe05b02ce9c6de2122a1dcedcdd803492de30c9bed441c7f3f6c +size 46700502 diff --git a/08 - Exploring Spring Framework/007 Constructor Injection.mp4 b/08 - Exploring Spring Framework/007 Constructor Injection.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..d1d7fb934fd45f46066ff96383d327a8a30cb682 --- /dev/null +++ b/08 - Exploring Spring Framework/007 Constructor Injection.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:611bfa870cf3ffd6a21b229acad09e915ea8d5b64f684b390a29cfd465472d6c +size 201564567 diff --git a/08 - Exploring Spring Framework/008 Creating Interface.mp4 b/08 - Exploring Spring Framework/008 Creating Interface.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..219edc1376cf14a009ab34efc367eb1ae6ce135e --- /dev/null +++ b/08 - Exploring Spring Framework/008 Creating Interface.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6f8c4c1309b3349887890b2922fe2dab28e97d21eed3fad35e623fea0be27f23 +size 29941335 diff --git a/08 - Exploring Spring Framework/009 Autowiring.mp4 b/08 - Exploring Spring Framework/009 Autowiring.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..8a0aeb642672788dd3e5a83d7e86563242470554 --- /dev/null +++ b/08 - Exploring Spring Framework/009 Autowiring.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5c214fcc6e4a3bb120fe8f85cda5c95addeff38e3a1757d9393c1f404118eb4b +size 111340061 diff --git a/08 - Exploring Spring Framework/010 Primary Bean.mp4 b/08 - Exploring Spring Framework/010 Primary Bean.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..8766d2195e21f94245d5396d33d35b128ee77d72 --- /dev/null +++ b/08 - Exploring Spring Framework/010 Primary Bean.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:efc93c281506ad9093355d66f0d8769e78737c73463bfaa81f20fbdf5658505f +size 13852811 diff --git a/08 - Exploring Spring Framework/011 Lazy Init Bean.mp4 b/08 - Exploring Spring Framework/011 Lazy Init Bean.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..6dc9832ba684d30a3a0ba6a639428f679f2b3b92 --- /dev/null +++ b/08 - Exploring Spring Framework/011 Lazy Init Bean.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:024ec92491448555be43a193a80c85708c15f9b302df59aef1b4a780fe998672 +size 64730609 diff --git a/08 - Exploring Spring Framework/012 Getbean By Type.mp4 b/08 - Exploring Spring Framework/012 Getbean By Type.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..e2cf65789f4718f5e68b21f84d647d174821bff4 --- /dev/null +++ b/08 - Exploring Spring Framework/012 Getbean By Type.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bda32160baf437a1d9c18ce9aac3c23edd0196efa7bda228adb649f19fc861ca +size 45999367 diff --git a/08 - Exploring Spring Framework/013 Inner Bean.mp4 b/08 - Exploring Spring Framework/013 Inner Bean.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..71a9400b16ace4965d293974cc39c1f8b82d1744 --- /dev/null +++ b/08 - Exploring Spring Framework/013 Inner Bean.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:63ac8750658128b4d725d6011a9b356d46230d50ad9f93f936d94bef6fdc776a +size 23854327 diff --git a/09 - Working with Java Based Configuration/002 Bean Name.mp4 b/09 - Working with Java Based Configuration/002 Bean Name.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..919061366c1dccf1dbb48f451acd363760772722 --- /dev/null +++ b/09 - Working with Java Based Configuration/002 Bean Name.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a31d4f5ef3d1f7fed7619e7590d71e7de5b4d15589a7a4404da8327665c2dadf +size 25348094 diff --git a/27 - Git/021 Git stash_en.srt b/27 - Git/021 Git stash_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..2f9619b7ca4cf826c71038238391f3c89cc90071 --- /dev/null +++ b/27 - Git/021 Git stash_en.srt @@ -0,0 +1,548 @@ +1 +00:00:00,000 --> 00:00:03,000 +-: In this video, we'll talk about Git Stash. + +2 +00:00:03,000 --> 00:00:05,000 +Now there might be some situation + +3 +00:00:05,000 --> 00:00:07,000 +where you are going to use this command. + +4 +00:00:07,000 --> 00:00:09,000 +First of all, what is Git Stash? + +5 +00:00:09,000 --> 00:00:11,000 +Let's say if you're working on one of the branch, okay, + +6 +00:00:11,000 --> 00:00:13,000 +so let's say I'm working on a new branch. + +7 +00:00:13,000 --> 00:00:14,000 +Let's say feature1. + +8 +00:00:14,000 --> 00:00:15,000 +So I'm in our main branch now. + +9 +00:00:15,000 --> 00:00:18,000 +I'm going to switch to feature1. + +10 +00:00:18,000 --> 00:00:19,000 +I'm going to feature1 branch. + +11 +00:00:19,000 --> 00:00:21,000 +And you can see we have all these changes here + +12 +00:00:21,000 --> 00:00:23,000 +in the feature1 branch. + +13 +00:00:23,000 --> 00:00:25,000 +And you are doing some experiment, let's say. + +14 +00:00:25,000 --> 00:00:28,000 +And what you're doing is, of course you have a main branch + +15 +00:00:28,000 --> 00:00:30,000 +where you have all the code which is working, + +16 +00:00:30,000 --> 00:00:31,000 +which is production ready. + +17 +00:00:31,000 --> 00:00:33,000 +And let's say, + +18 +00:00:33,000 --> 00:00:35,000 +when I'm working on this particular new feature, + +19 +00:00:35,000 --> 00:00:37,000 +it might take time to complete it, right? + +20 +00:00:37,000 --> 00:00:41,000 +So let's say I want to implement some AI feature here. + +21 +00:00:41,000 --> 00:00:43,000 +So I will say AI feature + +22 +00:00:43,000 --> 00:00:45,000 +and then of course this will take a lot of time, right? + +23 +00:00:45,000 --> 00:00:47,000 +To build this feature. + +24 +00:00:47,000 --> 00:00:48,000 +Maybe I want to connect with some server. + +25 +00:00:48,000 --> 00:00:50,000 +I want to build some model. + +26 +00:00:50,000 --> 00:00:53,000 +So that was, it was a lot of time to build this feature. + +27 +00:00:53,000 --> 00:00:55,000 +And let's say I'm taking two weeks for it, okay? + +28 +00:00:55,000 --> 00:00:58,000 +And while doing it, of course I don't want + +29 +00:00:58,000 --> 00:01:00,000 +to make any commits. + +30 +00:01:00,000 --> 00:01:02,000 +So I'm writing a lot of things here + +31 +00:01:02,000 --> 00:01:04,000 +and then since it is not ready, + +32 +00:01:04,000 --> 00:01:05,000 +I don't want to do any commit. + +33 +00:01:05,000 --> 00:01:08,000 +But at this point someone says, + +34 +00:01:08,000 --> 00:01:11,000 +Hey, you know, or your boss comes to you by saying, okay, + +35 +00:01:11,000 --> 00:01:13,000 +there's something wrong with the main branch. + +36 +00:01:13,000 --> 00:01:15,000 +The code which we have written has some bugs, + +37 +00:01:15,000 --> 00:01:16,000 +please solve it. + +38 +00:01:16,000 --> 00:01:18,000 +Now the first thing you will do is, okay, + +39 +00:01:18,000 --> 00:01:20,000 +I'm working on this. + +40 +00:01:20,000 --> 00:01:22,000 +If I directly go back to the main branch + +41 +00:01:22,000 --> 00:01:25,000 +to work on the bugs, let's try. + +42 +00:01:25,000 --> 00:01:27,000 +The moment I click on switch main. + +43 +00:01:27,000 --> 00:01:29,000 +You can see it's giving you error. + +44 +00:01:29,000 --> 00:01:32,000 +It's because you are already working on one of the branch + +45 +00:01:32,000 --> 00:01:36,000 +and then you have not committed that changes. + +46 +00:01:36,000 --> 00:01:37,000 +And the moment you switch now, + +47 +00:01:37,000 --> 00:01:39,000 +what will happen to those changes? + +48 +00:01:39,000 --> 00:01:40,000 +And that's what it says, you know. + +49 +00:01:40,000 --> 00:01:41,000 +Your local changes + +50 +00:01:41,000 --> 00:01:43,000 +to the following files would be overwritten + +51 +00:01:43,000 --> 00:01:47,000 +by checkout: userservice.txt, that's file you are working. + +52 +00:01:47,000 --> 00:01:49,000 +So please commit to changes, which we don't wanna do + +53 +00:01:49,000 --> 00:01:52,000 +because if you commit is not complete, right? + +54 +00:01:52,000 --> 00:01:55,000 +So this product is not complete so you cannot commit it. + +55 +00:01:55,000 --> 00:01:56,000 +Otherwise you can do Stash. + +56 +00:01:56,000 --> 00:01:59,000 +Now what Stash will do is stash will take your changes + +57 +00:01:59,000 --> 00:02:03,000 +and save it so that next time when you're coming back + +58 +00:02:03,000 --> 00:02:06,000 +to this particular branch, you can use this stash. + +59 +00:02:06,000 --> 00:02:08,000 +So what is stash basically? + +60 +00:02:08,000 --> 00:02:12,000 +if you want to save your code without committing + +61 +00:02:12,000 --> 00:02:13,000 +and when you come back here, of course one + +62 +00:02:13,000 --> 00:02:15,000 +of the way you can delete the code, okay? + +63 +00:02:15,000 --> 00:02:17,000 +So you can delete this and you are all good, right? + +64 +00:02:17,000 --> 00:02:20,000 +But I don't wanna do that, I don't wanna delete this. + +65 +00:02:20,000 --> 00:02:22,000 +What if you can save this? + +66 +00:02:22,000 --> 00:02:23,000 +And the way you can do that + +67 +00:02:23,000 --> 00:02:26,000 +is by using something called a Git Stash. + +68 +00:02:26,000 --> 00:02:27,000 +And when you say Git Stash, + +69 +00:02:27,000 --> 00:02:30,000 +you can see it says saved working directory + +70 +00:02:30,000 --> 00:02:34,000 +and index state work in progress on feature1 + +71 +00:02:34,000 --> 00:02:36,000 +and this is your stash ID. + +72 +00:02:36,000 --> 00:02:38,000 +Okay, so next time when you are coming back here, this is + +73 +00:02:38,000 --> 00:02:41,000 +what you need to work on, okay? + +74 +00:02:41,000 --> 00:02:42,000 +Cool. + +75 +00:02:42,000 --> 00:02:44,000 +So I have done the Git Stash now. + +76 +00:02:44,000 --> 00:02:46,000 +Now once you have done that, you can see it's deleted. + +77 +00:02:46,000 --> 00:02:47,000 +It looks like it is deleted, but it's not. + +78 +00:02:47,000 --> 00:02:48,000 +It is saved. + +79 +00:02:48,000 --> 00:02:50,000 +So you can come back here and use it. + +80 +00:02:50,000 --> 00:02:51,000 +Example, if you want to get it back, + +81 +00:02:51,000 --> 00:02:54,000 +you can say Git Stash list. + +82 +00:02:54,000 --> 00:02:56,000 +It will give you all these stash which you have done. + +83 +00:02:56,000 --> 00:02:58,000 +So you can say this is your first stash, + +84 +00:02:58,000 --> 00:03:00,000 +you can create multiple + +85 +00:03:00,000 --> 00:03:04,000 +and maybe you can say git stash apply. + +86 +00:03:04,000 --> 00:03:07,000 +And now the saved things will be applied again here + +87 +00:03:07,000 --> 00:03:09,000 +and that's done, right? + +88 +00:03:09,000 --> 00:03:11,000 +But again, I want to go back + +89 +00:03:11,000 --> 00:03:14,000 +to the main branch which is not committed yet. + +90 +00:03:14,000 --> 00:03:16,000 +So what I will do is I will try to switch now. + +91 +00:03:16,000 --> 00:03:18,000 +Again, it will give you the problem. + +92 +00:03:18,000 --> 00:03:21,000 +So what you will do is you will say Git Stash + +93 +00:03:21,000 --> 00:03:24,000 +and now you can go to the main branch. + +94 +00:03:24,000 --> 00:03:28,000 +So you can say git switch main + +95 +00:03:28,000 --> 00:03:29,000 +and now we're in the main branch + +96 +00:03:29,000 --> 00:03:31,000 +and these are the changes you're making there right? + +97 +00:03:31,000 --> 00:03:33,000 +Now, let's say there were some bugs + +98 +00:03:33,000 --> 00:03:34,000 +and then you have solved the bugs. + +99 +00:03:34,000 --> 00:03:37,000 +We don't want two, three dots, we want only two dots. + +100 +00:03:37,000 --> 00:03:39,000 +Let's say if you're working on a project, + +101 +00:03:39,000 --> 00:03:41,000 +you have solved some coding bugs. + +102 +00:03:41,000 --> 00:03:44,000 +But yeah, so you have done the changes. + +103 +00:03:44,000 --> 00:03:46,000 +So once you have done the changes in the main, + +104 +00:03:46,000 --> 00:03:47,000 +and if you say git status, + +105 +00:03:47,000 --> 00:03:49,000 +of course you have to commit this. + +106 +00:03:49,000 --> 00:03:52,000 +So we'll say git add, of course you can specify the user + +107 +00:03:52,000 --> 00:03:54,000 +of the file name or dot + +108 +00:03:54,000 --> 00:03:56,000 +and we'll say git commit -m. + +109 +00:03:56,000 --> 00:04:01,000 +And the commit is issue number 234. + +110 +00:04:01,000 --> 00:04:04,000 +So let's say that's the issue number we have is resolved. + +111 +00:04:06,000 --> 00:04:08,000 +So bug is fixed and we are done. + +112 +00:04:08,000 --> 00:04:10,000 +Now we are done with the main branch, okay? + +113 +00:04:10,000 --> 00:04:13,000 +And now you are happy because there's no bugs anymore. + +114 +00:04:13,000 --> 00:04:15,000 +Now you can go back to your feature1 branch. + +115 +00:04:15,000 --> 00:04:19,000 +So when you say git switch feature1. + +116 +00:04:19,000 --> 00:04:21,000 +So again, now you can work on your AI feature, + +117 +00:04:21,000 --> 00:04:23,000 +but you can see the code is not here. + +118 +00:04:23,000 --> 00:04:25,000 +It's because we have done something, right? + +119 +00:04:25,000 --> 00:04:26,000 +We have saved it. + +120 +00:04:26,000 --> 00:04:28,000 +So we say git stash list. + +121 +00:04:28,000 --> 00:04:30,000 +So these are the stash we have. + +122 +00:04:30,000 --> 00:04:33,000 +Anyone will do because both are same. + +123 +00:04:33,000 --> 00:04:38,000 +So you can simply say git stash apply + +124 +00:04:39,000 --> 00:04:40,000 +and you can say it is applied now. + +125 +00:04:40,000 --> 00:04:44,000 +So when you apply this, basically you can make more changes + +126 +00:04:44,000 --> 00:04:48,000 +and let's say at this point the code is complete. + +127 +00:04:48,000 --> 00:04:49,000 +So once you think the code is complete, + +128 +00:04:49,000 --> 00:04:52,000 +you can say git status. + +129 +00:04:52,000 --> 00:04:53,000 +Okay, I have to add this. + +130 +00:04:53,000 --> 00:04:55,000 +I would say git add . + +131 +00:04:56,000 --> 00:05:01,000 +and git commit -m ai enabled. + +132 +00:05:02,000 --> 00:05:04,000 +I know that sounds weird, but okay then. + +133 +00:05:04,000 --> 00:05:05,000 +So we have committed this. + +134 +00:05:05,000 --> 00:05:08,000 +Now everything looks cool, you can switch + +135 +00:05:08,000 --> 00:05:10,000 +between the branches without using stash now. + +136 +00:05:10,000 --> 00:05:12,000 +So stash is important if you want to save your work, okay? + +137 +00:05:12,000 --> 00:05:15,000 +So we have talked about it and that is stash. + diff --git a/27 - Git/022 Git fork_en.srt b/27 - Git/022 Git fork_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..88aeaf82ad57bbb01f7856bfce926c034a310e56 --- /dev/null +++ b/27 - Git/022 Git fork_en.srt @@ -0,0 +1,408 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: In this video, we'll talk about Git Fork. + +2 +00:00:02,000 --> 00:00:03,000 +So basically we went + +3 +00:00:03,000 --> 00:00:06,000 +to this particular page multiple times, right? + +4 +00:00:06,000 --> 00:00:08,000 +So this is your repository, the Git course, + +5 +00:00:08,000 --> 00:00:10,000 +and we were basically playing + +6 +00:00:10,000 --> 00:00:12,000 +between the console level, which is Git, + +7 +00:00:12,000 --> 00:00:14,000 +and then the GitHub. + +8 +00:00:14,000 --> 00:00:15,000 +We were able to push it, + +9 +00:00:15,000 --> 00:00:18,000 +and then on this page, we have multiple things, + +10 +00:00:18,000 --> 00:00:21,000 +but there's one important thing here, which is called Fork. + +11 +00:00:21,000 --> 00:00:23,000 +Now, this project has not been forked yet. + +12 +00:00:23,000 --> 00:00:25,000 +I hope it will be happening soon, + +13 +00:00:25,000 --> 00:00:28,000 +but we have this particular project here, right? + +14 +00:00:28,000 --> 00:00:30,000 +Now we'll talk about this thing here. + +15 +00:00:30,000 --> 00:00:31,000 +Now, why do we need this thing? + +16 +00:00:31,000 --> 00:00:33,000 +So let's say I'm exploring a project. + +17 +00:00:33,000 --> 00:00:35,000 +See, whatever we have done 'til now + +18 +00:00:35,000 --> 00:00:37,000 +is we own both of the things, right? + +19 +00:00:37,000 --> 00:00:40,000 +So whatever change I was making in my local, I own it. + +20 +00:00:40,000 --> 00:00:41,000 +This particular repository, + +21 +00:00:41,000 --> 00:00:44,000 +I own this repository because I have created it, okay? + +22 +00:00:44,000 --> 00:00:46,000 +Of course, I can add contributors here, + +23 +00:00:46,000 --> 00:00:49,000 +but at this point, we have not done it, okay? + +24 +00:00:49,000 --> 00:00:51,000 +And now let's say you are searching + +25 +00:00:51,000 --> 00:00:52,000 +for the particular repository, + +26 +00:00:52,000 --> 00:00:54,000 +and whatever the repository is is not yours, + +27 +00:00:54,000 --> 00:00:55,000 +but someone else's. + +28 +00:00:55,000 --> 00:00:56,000 +I'm searching for VS Code, + +29 +00:00:56,000 --> 00:01:00,000 +and this is basically a VS Code repository, + +30 +00:01:00,000 --> 00:01:02,000 +so they have the entire source code here. + +31 +00:01:02,000 --> 00:01:05,000 +Now what if I want to make some changes here? + +32 +00:01:05,000 --> 00:01:07,000 +And you can see I have this repository. + +33 +00:01:07,000 --> 00:01:09,000 +In fact, we download this repository, + +34 +00:01:09,000 --> 00:01:11,000 +we tried to do something. + +35 +00:01:11,000 --> 00:01:13,000 +Of course, you can download the entire repository + +36 +00:01:13,000 --> 00:01:14,000 +in your own machine, + +37 +00:01:14,000 --> 00:01:16,000 +but what if you want to make some changes? + +38 +00:01:16,000 --> 00:01:17,000 +So let's say I don't like + +39 +00:01:17,000 --> 00:01:20,000 +the way VS Code compiles the files. + +40 +00:01:20,000 --> 00:01:22,000 +For example, if you go back to your VS Code, + +41 +00:01:22,000 --> 00:01:24,000 +let's say there's one thing which I don't like, + +42 +00:01:24,000 --> 00:01:27,000 +is this windows, I want this windows to be on right inside. + +43 +00:01:27,000 --> 00:01:28,000 +Is it really possible? + +44 +00:01:28,000 --> 00:01:30,000 +Yes, you can change the source code of it + +45 +00:01:30,000 --> 00:01:33,000 +and then you cannot commercialize it, + +46 +00:01:33,000 --> 00:01:35,000 +depending upon the license. + +47 +00:01:35,000 --> 00:01:36,000 +There are multiple license for the projects, right? + +48 +00:01:36,000 --> 00:01:38,000 +So this is MIT license. + +49 +00:01:38,000 --> 00:01:40,000 +So different licenses have different meanings, + +50 +00:01:40,000 --> 00:01:43,000 +but let's say I want to make some changes for my own use. + +51 +00:01:43,000 --> 00:01:45,000 +I don't want to sell this. + +52 +00:01:45,000 --> 00:01:46,000 +What I can do is I can clone the project + +53 +00:01:46,000 --> 00:01:50,000 +and I can make the changes and I can use it the way I want. + +54 +00:01:50,000 --> 00:01:54,000 +But what if I want these changes to be used by everyone? + +55 +00:01:54,000 --> 00:01:59,000 +I want to make real changes in this particular repository. + +56 +00:01:59,000 --> 00:02:00,000 +Is it really possible? + +57 +00:02:00,000 --> 00:02:03,000 +Actually not, and even if you clone this project, + +58 +00:02:03,000 --> 00:02:05,000 +if you have a local copy of it and if you make changes, + +59 +00:02:05,000 --> 00:02:07,000 +if you try to push the changes, + +60 +00:02:07,000 --> 00:02:10,000 +it will not work because you are not allowed. + +61 +00:02:10,000 --> 00:02:11,000 +You are not even a contributor here. + +62 +00:02:11,000 --> 00:02:13,000 +So in that case, what you will do + +63 +00:02:13,000 --> 00:02:15,000 +is you will use something called a fork here. + +64 +00:02:15,000 --> 00:02:17,000 +So when you fork the project, let me just try that. + +65 +00:02:17,000 --> 00:02:18,000 +So when you click on Fork, + +66 +00:02:18,000 --> 00:02:21,000 +you can see now this VS Code will be available for me. + +67 +00:02:21,000 --> 00:02:23,000 +I mean, I will be having the source code of it + +68 +00:02:23,000 --> 00:02:24,000 +in my account. + +69 +00:02:24,000 --> 00:02:27,000 +I can have a description, and looks cool. + +70 +00:02:27,000 --> 00:02:29,000 +I will click on create fork. + +71 +00:02:29,000 --> 00:02:33,000 +Now the entire project will be available in my machine. + +72 +00:02:33,000 --> 00:02:34,000 +So if I... + +73 +00:02:34,000 --> 00:02:37,000 +Or not in my machine, but in my repository. + +74 +00:02:37,000 --> 00:02:40,000 +So if you can see, I'm into my own account, + +75 +00:02:40,000 --> 00:02:43,000 +which is Navin Reddy, and this is where if I expand, + +76 +00:02:43,000 --> 00:02:45,000 +if I look at all the repositories, + +77 +00:02:45,000 --> 00:02:48,000 +you can see we have VS Code, okay? + +78 +00:02:48,000 --> 00:02:50,000 +Now this is the official Microsoft VS Code, + +79 +00:02:50,000 --> 00:02:52,000 +which I got in my repository. + +80 +00:02:52,000 --> 00:02:53,000 +I can make the changes, + +81 +00:02:53,000 --> 00:02:56,000 +but it'll only available in my repository, okay? + +82 +00:02:56,000 --> 00:02:58,000 +So if you want to push, yes, you can now. + +83 +00:02:58,000 --> 00:03:01,000 +So you can just clone this project, make some changes. + +84 +00:03:01,000 --> 00:03:02,000 +So you can just clone this project, + +85 +00:03:02,000 --> 00:03:04,000 +make the changes and push it, + +86 +00:03:04,000 --> 00:03:07,000 +but you're pushing in your own repository, + +87 +00:03:07,000 --> 00:03:09,000 +not on the Microsoft repository. + +88 +00:03:09,000 --> 00:03:12,000 +That you can do, okay? + +89 +00:03:12,000 --> 00:03:16,000 +But one thing you can do, once you make the changes, + +90 +00:03:16,000 --> 00:03:18,000 +you can create something called a pull request. + +91 +00:03:18,000 --> 00:03:20,000 +Of course, we'll see pull request later, + +92 +00:03:20,000 --> 00:03:23,000 +but at this point, this is how you basically fork + +93 +00:03:23,000 --> 00:03:25,000 +and the repositories will be available + +94 +00:03:25,000 --> 00:03:28,000 +on your machine, okay? + +95 +00:03:28,000 --> 00:03:32,000 +So that is basically forking, and it's all about forking. + +96 +00:03:32,000 --> 00:03:32,000 +Now, in the next video, + +97 +00:03:32,000 --> 00:03:35,000 +let's try to understand how do we create a pull request? + +98 +00:03:35,000 --> 00:03:37,000 +What if you make some changes in your own in the project + +99 +00:03:37,000 --> 00:03:39,000 +and you want the entire world to use it? + +100 +00:03:39,000 --> 00:03:42,000 +So of course, you have to ask Microsoft people + +101 +00:03:42,000 --> 00:03:46,000 +or the community to accept your changes, + +102 +00:03:46,000 --> 00:03:47,000 +and we'll see that in the next video. + diff --git a/27 - Git/023 Git Pull Request_en.srt b/27 - Git/023 Git Pull Request_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..76c37dfc009f69fe6a1645232390f1fac040734f --- /dev/null +++ b/27 - Git/023 Git Pull Request_en.srt @@ -0,0 +1,832 @@ +1 +00:00:00,000 --> 00:00:03,000 +-: In this video, let's try to create a pull request. + +2 +00:00:03,000 --> 00:00:04,000 +But then why do we need a pull request? + +3 +00:00:04,000 --> 00:00:07,000 +As we mentioned before, whenever you make some changes. + +4 +00:00:07,000 --> 00:00:10,000 +In fact, it's not just for the forking, + +5 +00:00:10,000 --> 00:00:12,000 +but if you are doing some changes in your branches, + +6 +00:00:12,000 --> 00:00:14,000 +you can create a pull request. + +7 +00:00:14,000 --> 00:00:17,000 +But let's make it more interesting by forking + +8 +00:00:17,000 --> 00:00:19,000 +and then doing the pull request. + +9 +00:00:19,000 --> 00:00:21,000 +I don't want to make any changes in the VS Code + +10 +00:00:21,000 --> 00:00:25,000 +because I don't even know how entire project is built. + +11 +00:00:25,000 --> 00:00:27,000 +It will take a lot of time for me to understand the project, + +12 +00:00:27,000 --> 00:00:28,000 +make the changes. + +13 +00:00:28,000 --> 00:00:31,000 +And then even if I create a pull request, + +14 +00:00:31,000 --> 00:00:34,000 +the Microsoft People should accept my request, okay? + +15 +00:00:34,000 --> 00:00:37,000 +And I don't want to put this particular video in their hand. + +16 +00:00:37,000 --> 00:00:39,000 +So, let's do in our own project. + +17 +00:00:39,000 --> 00:00:43,000 +So, when I'm saying that, is let's go back to navinreddy20, + +18 +00:00:43,000 --> 00:00:44,000 +that's my personal account. + +19 +00:00:44,000 --> 00:00:47,000 +And in this, I have multiple project, if you can see. + +20 +00:00:47,000 --> 00:00:49,000 +So, if I click on the repositories, + +21 +00:00:49,000 --> 00:00:52,000 +I have lot of projects here. + +22 +00:00:52,000 --> 00:00:54,000 +So, let me just make the changes in one project here. + +23 +00:00:54,000 --> 00:00:56,000 +So, let's say we have the Quiz app, okay? + +24 +00:00:56,000 --> 00:01:00,000 +Now, this is something which I'm building for videos, + +25 +00:01:01,000 --> 00:01:02,000 +the monolithic application. + +26 +00:01:03,000 --> 00:01:04,000 +So, we are basically trying + +27 +00:01:04,000 --> 00:01:07,000 +to make a microservices project from monolithic, + +28 +00:01:07,000 --> 00:01:08,000 +so this is the repository. + +29 +00:01:08,000 --> 00:01:09,000 +Okay, that's an important for you. + +30 +00:01:09,000 --> 00:01:12,000 +The important thing is, let's say I have this project, okay? + +31 +00:01:12,000 --> 00:01:14,000 +And someone else, let's say you, + +32 +00:01:14,000 --> 00:01:16,000 +want to contribute here for this project. + +33 +00:01:16,000 --> 00:01:18,000 +For this particular video, I have two machines here. + +34 +00:01:18,000 --> 00:01:20,000 +One, where I have my account. + +35 +00:01:20,000 --> 00:01:22,000 +On the other hand, on the right-hand side, + +36 +00:01:22,000 --> 00:01:25,000 +I have another account just for the example of, + +37 +00:01:25,000 --> 00:01:27,000 +I don't use this account much, + +38 +00:01:27,000 --> 00:01:29,000 +this is only for the experiment purpose. + +39 +00:01:29,000 --> 00:01:31,000 +But I have GitHub account here. + +40 +00:01:31,000 --> 00:01:34,000 +And if you can see, in this particular repository, + +41 +00:01:34,000 --> 00:01:36,000 +I have lot of different repositories created. + +42 +00:01:36,000 --> 00:01:37,000 +Again, just for experiment. + +43 +00:01:37,000 --> 00:01:42,000 +And in this particular repository, I want to fork it. + +44 +00:01:42,000 --> 00:01:45,000 +So, basically, this project, which on the left-hand side, + +45 +00:01:45,000 --> 00:01:47,000 +the actual, my account, I have this project, + +46 +00:01:47,000 --> 00:01:49,000 +I want to make some changes here. + +47 +00:01:49,000 --> 00:01:53,000 +But from the other account, let's say this is your account. + +48 +00:01:53,000 --> 00:01:54,000 +So, the way you can do that, + +49 +00:01:54,000 --> 00:01:56,000 +is by, first of all, going to my particular page, + +50 +00:01:56,000 --> 00:01:58,000 +you can search for navinreddy20, + +51 +00:01:58,000 --> 00:02:01,000 +and you can see we have Users. + +52 +00:02:01,000 --> 00:02:03,000 +Okay, there's only one navinreddy20. + +53 +00:02:03,000 --> 00:02:05,000 +Now, from navinreddy20, + +54 +00:02:05,000 --> 00:02:07,000 +you can go to different repositories. + +55 +00:02:07,000 --> 00:02:09,000 +So, again, I'm on different account now, + +56 +00:02:09,000 --> 00:02:12,000 +I'm accessing my account from different account. + +57 +00:02:13,000 --> 00:02:14,000 +So, basically, you can say + +58 +00:02:14,000 --> 00:02:15,000 +this is the project we want to fork. + +59 +00:02:15,000 --> 00:02:17,000 +So, I will go here, + +60 +00:02:17,000 --> 00:02:20,000 +and then there will be an option of forking it. + +61 +00:02:20,000 --> 00:02:22,000 +So, what I will do, is I will click on Fork. + +62 +00:02:22,000 --> 00:02:24,000 +Now, this project belongs to Navin Reddy, + +63 +00:02:24,000 --> 00:02:28,000 +but I'm TELUSKO now, and I'm forking this project. + +64 +00:02:28,000 --> 00:02:30,000 +So, I don't want to make any description. + +65 +00:02:30,000 --> 00:02:32,000 +Click on Fork, and done. + +66 +00:02:32,000 --> 00:02:34,000 +This project, which was belonging to Navin Reddy, + +67 +00:02:34,000 --> 00:02:37,000 +now we also have that in our depository, + +68 +00:02:37,000 --> 00:02:38,000 +in TELUSKO repository, basically. + +69 +00:02:38,000 --> 00:02:40,000 +So, when I say our, it's very confusing, right? + +70 +00:02:40,000 --> 00:02:43,000 +So, Navin Reddy and then TELUSKO, okay? + +71 +00:02:43,000 --> 00:02:45,000 +That (chuckles) should be simple. + +72 +00:02:45,000 --> 00:02:45,000 +Now, from TELUSKO, + +73 +00:02:45,000 --> 00:02:48,000 +so let's say TELUSKO wants to make some changes now. + +74 +00:02:48,000 --> 00:02:49,000 +Now, what changes you want to make, + +75 +00:02:49,000 --> 00:02:51,000 +of course, you can do it from your local machine as well. + +76 +00:02:51,000 --> 00:02:56,000 +So, what I can do, is I can just simply copy this URL. + +77 +00:02:58,000 --> 00:03:00,000 +So, make sure that you have into SSH + +78 +00:03:00,000 --> 00:03:02,000 +and you have your key setup done. + +79 +00:03:02,000 --> 00:03:04,000 +So, on this machine, I have the key setup ready. + +80 +00:03:04,000 --> 00:03:07,000 +I'll close this, I'll open my terminal. + +81 +00:03:07,000 --> 00:03:09,000 +And here, basically in the Projects folder, + +82 +00:03:09,000 --> 00:03:10,000 +I want to clone it. + +83 +00:03:10,000 --> 00:03:13,000 +So, I will say git clone, paste, + +84 +00:03:13,000 --> 00:03:17,000 +and the entire project will be coming in this machine, but, + +85 +00:03:17,000 --> 00:03:18,000 +yeah, so we got it. + +86 +00:03:18,000 --> 00:03:21,000 +And then I want to open this from my VS Code. + +87 +00:03:21,000 --> 00:03:23,000 +So, let me open that quickly, open. + +88 +00:03:24,000 --> 00:03:28,000 +This goes into the Projects folder. + +89 +00:03:28,000 --> 00:03:32,000 +And you can see we have a project, which is quiz-app-spring. + +90 +00:03:32,000 --> 00:03:33,000 +I will open this. + +91 +00:03:33,000 --> 00:03:35,000 +Yeah, okay, so this is the project we have, right? + +92 +00:03:35,000 --> 00:03:38,000 +So, this was originally owned by Navin Reddy. + +93 +00:03:38,000 --> 00:03:42,000 +And then from my TELUSKO account, I was able to get this. + +94 +00:03:42,000 --> 00:03:43,000 +Okay. + +95 +00:03:43,000 --> 00:03:44,000 +So, let's say I want to make some changes. + +96 +00:03:44,000 --> 00:03:47,000 +I don't know what is there in the project, + +97 +00:03:47,000 --> 00:03:49,000 +but let's say if I go back to any of this code, + +98 +00:03:49,000 --> 00:03:51,000 +let's say Controller. + +99 +00:03:51,000 --> 00:03:54,000 +And, okay, so you can see we have some imports here. + +100 +00:03:54,000 --> 00:03:58,000 +And I think we are not using question anywhere, are we? + +101 +00:03:58,000 --> 00:03:59,000 +No, again, I'm changing Java code. + +102 +00:03:59,000 --> 00:04:01,000 +If you're not sure about Java, just ignore. + +103 +00:04:01,000 --> 00:04:03,000 +Just imagine I'm making some changes, okay? + +104 +00:04:03,000 --> 00:04:06,000 +So, I've deleted one line of code, not a big. + +105 +00:04:06,000 --> 00:04:08,000 +Or maybe deleting is not a good option, right? + +106 +00:04:08,000 --> 00:04:10,000 +What if I want to use that line somewhere? + +107 +00:04:10,000 --> 00:04:12,000 +So, I will add some comment here. + +108 +00:04:12,000 --> 00:04:16,000 +So, I will say controller for quiz, okay? + +109 +00:04:16,000 --> 00:04:17,000 +And I made the changes now. + +110 +00:04:17,000 --> 00:04:19,000 +And let's say I've made a lot of changes. + +111 +00:04:19,000 --> 00:04:21,000 +Just for example, I've only made one here, + +112 +00:04:21,000 --> 00:04:23,000 +but you imagine I've made a lot of changes. + +113 +00:04:23,000 --> 00:04:25,000 +Now, after making those changes, + +114 +00:04:25,000 --> 00:04:28,000 +what I can do, is I can just go back here to the project, + +115 +00:04:28,000 --> 00:04:30,000 +which is quiz service, + +116 +00:04:30,000 --> 00:04:33,000 +and I will check git status. + +117 +00:04:33,000 --> 00:04:36,000 +There's one file which I've made the changes. + +118 +00:04:36,000 --> 00:04:40,000 +So, I'll say git add, add that file, + +119 +00:04:40,000 --> 00:04:44,000 +git commit hyphen M. + +120 +00:04:44,000 --> 00:04:49,000 +And I have to type some message, I will say, comment added. + +121 +00:04:50,000 --> 00:04:51,000 +Okay. + +122 +00:04:51,000 --> 00:04:53,000 +And now, the changes are there in my machine, right? + +123 +00:04:53,000 --> 00:04:57,000 +I want to push it, so I will say git push origin main, + +124 +00:04:57,000 --> 00:05:01,000 +because it's a main branch, and it should be done now. + +125 +00:05:01,000 --> 00:05:02,000 +Pushing is happening. + +126 +00:05:02,000 --> 00:05:05,000 +Okay, pushing done, you can see it is pushed now. + +127 +00:05:05,000 --> 00:05:07,000 +Let me verify that from my GitHub account. + +128 +00:05:07,000 --> 00:05:08,000 +So, if I refresh. + +129 +00:05:08,000 --> 00:05:10,000 +Okay, so you can see there is one change happened + +130 +00:05:10,000 --> 00:05:12,000 +in this particular folder, which is SRC, + +131 +00:05:12,000 --> 00:05:13,000 +and that's what we wanted, right? + +132 +00:05:13,000 --> 00:05:17,000 +But if you see on my real machine, on my real account, + +133 +00:05:17,000 --> 00:05:18,000 +there's no changes. + +134 +00:05:18,000 --> 00:05:22,000 +The last change happened three weeks back. + +135 +00:05:22,000 --> 00:05:23,000 +So, what we need to do is, + +136 +00:05:23,000 --> 00:05:26,000 +of course, the changes are there in the TELUSKO account. + +137 +00:05:26,000 --> 00:05:27,000 +Now, if you want this changes + +138 +00:05:27,000 --> 00:05:31,000 +to be applied to the main account, or for everyone to use, + +139 +00:05:31,000 --> 00:05:32,000 +what we can do, + +140 +00:05:32,000 --> 00:05:34,000 +is we can create something called a pull request. + +141 +00:05:34,000 --> 00:05:36,000 +Now, the way you can do that, is by going to this tab, + +142 +00:05:36,000 --> 00:05:38,000 +this Pull Request, + +143 +00:05:38,000 --> 00:05:40,000 +and you can create a new pull request, okay? + +144 +00:05:40,000 --> 00:05:42,000 +There's option of pull request here as well. + +145 +00:05:42,000 --> 00:05:44,000 +So, create a new pull request. + +146 +00:05:44,000 --> 00:05:45,000 +And the moment you do that, + +147 +00:05:45,000 --> 00:05:49,000 +you can see there's something here which says, + +148 +00:05:49,000 --> 00:05:51,000 +the base repository, which is navinreddy20, + +149 +00:05:51,000 --> 00:05:53,000 +this is a project name. + +150 +00:05:53,000 --> 00:05:55,000 +And your project where you have work working on + +151 +00:05:55,000 --> 00:05:58,000 +is teluskotraining, the project name is same. + +152 +00:05:58,000 --> 00:06:00,000 +And we will be able to merge it + +153 +00:06:00,000 --> 00:06:02,000 +because I don't think there's any conflict there. + +154 +00:06:02,000 --> 00:06:04,000 +So, we can create a pull request. + +155 +00:06:04,000 --> 00:06:07,000 +And the changes we have made is huge change. + +156 +00:06:07,000 --> 00:06:10,000 +We just added a comment in Java file, okay. + +157 +00:06:10,000 --> 00:06:11,000 +That's not important, right? + +158 +00:06:11,000 --> 00:06:11,000 +Java's not important here, + +159 +00:06:11,000 --> 00:06:14,000 +important is you're making some changes. + +160 +00:06:14,000 --> 00:06:15,000 +And once you do that, + +161 +00:06:15,000 --> 00:06:17,000 +you can click on the Create Pull Request, + +162 +00:06:17,000 --> 00:06:19,000 +you can add your comment here. + +163 +00:06:19,000 --> 00:06:24,000 +So, you can say, comments are important for this code. + +164 +00:06:24,000 --> 00:06:25,000 +And that's it. + +165 +00:06:25,000 --> 00:06:29,000 +You can create something called a pull request. + +166 +00:06:29,000 --> 00:06:33,000 +Then once you click this, the pull request has been sent. + +167 +00:06:33,000 --> 00:06:35,000 +You can see it is open now. + +168 +00:06:35,000 --> 00:06:37,000 +And this branch has no conflict with the base branch. + +169 +00:06:37,000 --> 00:06:39,000 +That's good. + +170 +00:06:39,000 --> 00:06:41,000 +So, pull request is created, but how would you check that? + +171 +00:06:41,000 --> 00:06:44,000 +So, you have to go back to your main account. + +172 +00:06:44,000 --> 00:06:46,000 +And if you click on Pull Request here, + +173 +00:06:47,000 --> 00:06:48,000 +so you can see we got a pull request. + +174 +00:06:48,000 --> 00:06:50,000 +Now, who has sent this pull request? + +175 +00:06:50,000 --> 00:06:52,000 +Teluskotraining has sent this pull request. + +176 +00:06:52,000 --> 00:06:55,000 +So, it is my job now to see this pull request. + +177 +00:06:55,000 --> 00:06:57,000 +You can't simply accept any pull request, right? + +178 +00:06:57,000 --> 00:06:59,000 +Anyone can send any type of code. + +179 +00:06:59,000 --> 00:07:00,000 +We have to review the code. + +180 +00:07:00,000 --> 00:07:02,000 +And that's why the code review is very important. + +181 +00:07:02,000 --> 00:07:04,000 +So, when you open this, check the changes. + +182 +00:07:04,000 --> 00:07:08,000 +So, you can just go down, and you can see the changes here. + +183 +00:07:08,000 --> 00:07:11,000 +So, comments are important, what changes we have made. + +184 +00:07:11,000 --> 00:07:14,000 +There's only one change, the comment added. + +185 +00:07:14,000 --> 00:07:15,000 +What are the changes? + +186 +00:07:15,000 --> 00:07:17,000 +It's the controller for quiz that this comment is added, + +187 +00:07:17,000 --> 00:07:20,000 +and I feel that's important. + +188 +00:07:20,000 --> 00:07:23,000 +I will again go back to my pull request, click on this. + +189 +00:07:23,000 --> 00:07:25,000 +And now, I can accept it. + +190 +00:07:25,000 --> 00:07:26,000 +The way you can accept this, + +191 +00:07:26,000 --> 00:07:29,000 +is with the help of Merge Pull Request. + +192 +00:07:29,000 --> 00:07:32,000 +Now, when you click this, the same comment, + +193 +00:07:32,000 --> 00:07:35,000 +commit merge, changes, done. + +194 +00:07:35,000 --> 00:07:36,000 +Is it happening? + +195 +00:07:36,000 --> 00:07:37,000 +Yeah, changes done. + +196 +00:07:37,000 --> 00:07:41,000 +And if you see here on my TELUSKO account, + +197 +00:07:41,000 --> 00:07:43,000 +it says, pull request is closed. + +198 +00:07:43,000 --> 00:07:44,000 +It was open, now it's closed, + +199 +00:07:44,000 --> 00:07:47,000 +because I have done the merging on the main account. + +200 +00:07:48,000 --> 00:07:52,000 +So, that's done, pull request is complete, merge happened. + +201 +00:07:52,000 --> 00:07:56,000 +And if I go back to Code now, look at the changes. + +202 +00:07:56,000 --> 00:07:59,000 +So, the SRC is being changed three minutes ago. + +203 +00:08:00,000 --> 00:08:02,000 +So, that's how you can basically contribute + +204 +00:08:02,000 --> 00:08:03,000 +to the open-source project, okay? + +205 +00:08:03,000 --> 00:08:05,000 +And this is very important. + +206 +00:08:05,000 --> 00:08:06,000 +So, yeah, that's it from this video + +207 +00:08:06,000 --> 00:08:09,000 +where we forked the project, we created a pull request, + +208 +00:08:09,000 --> 00:08:12,000 +and we accepted it as well. + diff --git a/28 - DSA (Optional)/001 What Are Data Structures_en.srt b/28 - DSA (Optional)/001 What Are Data Structures_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..06fd3800a11338a6d0219a24ed92eac257c13d16 --- /dev/null +++ b/28 - DSA (Optional)/001 What Are Data Structures_en.srt @@ -0,0 +1,784 @@ +1 +00:00:00,000 --> 00:00:03,000 +-: What are data structures? + +2 +00:00:03,000 --> 00:00:04,000 +The reason we are talking about this + +3 +00:00:04,000 --> 00:00:06,000 +is because if you look at the internet, + +4 +00:00:06,000 --> 00:00:09,000 +everyone says if you want to get into this company + +5 +00:00:09,000 --> 00:00:11,000 +or that company, they will ask you a question + +6 +00:00:11,000 --> 00:00:14,000 +based on data structures and algorithms. + +7 +00:00:14,000 --> 00:00:17,000 +So first of all, why such a hype + +8 +00:00:17,000 --> 00:00:20,000 +and what exactly data structures are. + +9 +00:00:20,000 --> 00:00:21,000 +So before we get into data structures, + +10 +00:00:21,000 --> 00:00:22,000 +let's talk about data. + +11 +00:00:22,000 --> 00:00:27,000 +See, the entire software industry is based on data. + +12 +00:00:27,000 --> 00:00:28,000 +When you do a course on IT, + +13 +00:00:28,000 --> 00:00:31,000 +which we call information technology, + +14 +00:00:31,000 --> 00:00:34,000 +it's not about technology, it's about information. + +15 +00:00:34,000 --> 00:00:37,000 +Because we work with information. + +16 +00:00:37,000 --> 00:00:39,000 +So data is everything. + +17 +00:00:39,000 --> 00:00:42,000 +So it doesn't matter what technology you learn, + +18 +00:00:42,000 --> 00:00:44,000 +you basically learn or you work on it + +19 +00:00:44,000 --> 00:00:46,000 +so that you can work with data. + +20 +00:00:46,000 --> 00:00:47,000 +Example, think about programming languages. + +21 +00:00:47,000 --> 00:00:51,000 +Why do we use programming languages to process the data? + +22 +00:00:51,000 --> 00:00:53,000 +Why do we use database to store the data? + +23 +00:00:53,000 --> 00:00:56,000 +Why do we use AI to generate data + +24 +00:00:56,000 --> 00:00:58,000 +or to understand data, right? + +25 +00:00:58,000 --> 00:01:02,000 +I mean, just to bring it down, everything is data. + +26 +00:01:02,000 --> 00:01:05,000 +Now then question arise, what is data structures? + +27 +00:01:05,000 --> 00:01:07,000 +Now, if we talk about any programming language, + +28 +00:01:07,000 --> 00:01:09,000 +we have something called primitive data types. + +29 +00:01:09,000 --> 00:01:12,000 +If you want to store a number, we store that in integer. + +30 +00:01:12,000 --> 00:01:15,000 +If you want to store a a text, you store that in a string. + +31 +00:01:15,000 --> 00:01:17,000 +If you want to store a character, + +32 +00:01:17,000 --> 00:01:18,000 +you store that in a character. + +33 +00:01:18,000 --> 00:01:20,000 +Of course, depend upon different languages. + +34 +00:01:20,000 --> 00:01:23,000 +The term or the way you store it changes. + +35 +00:01:23,000 --> 00:01:26,000 +But ultimately you have some types + +36 +00:01:26,000 --> 00:01:27,000 +to it where you store the data. + +37 +00:01:27,000 --> 00:01:29,000 +But what if you have bunch of data + +38 +00:01:29,000 --> 00:01:30,000 +and if you want to store them? + +39 +00:01:30,000 --> 00:01:32,000 +It's not just about storing data. + +40 +00:01:32,000 --> 00:01:33,000 +Anyone can do that. + +41 +00:01:33,000 --> 00:01:35,000 +It's about how do you store data efficiently + +42 +00:01:35,000 --> 00:01:37,000 +and you can also save some memory. + +43 +00:01:37,000 --> 00:01:39,000 +The thing is, if you simply dump the data, + +44 +00:01:39,000 --> 00:01:42,000 +you are expanding the memory. + +45 +00:01:42,000 --> 00:01:43,000 +And if you want to search something + +46 +00:01:43,000 --> 00:01:45,000 +from the data, will be difficult. + +47 +00:01:45,000 --> 00:01:48,000 +And that's why storing that data efficiently + +48 +00:01:48,000 --> 00:01:48,000 +is very important. + +49 +00:01:48,000 --> 00:01:51,000 +And that's where data structures comes into picture. + +50 +00:01:51,000 --> 00:01:54,000 +So data structure is a way to organize + +51 +00:01:54,000 --> 00:01:57,000 +and store data efficiently. + +52 +00:01:57,000 --> 00:01:59,000 +Okay, so when you say efficiently, it means two things. + +53 +00:01:59,000 --> 00:02:01,000 +First, in terms of performance + +54 +00:02:01,000 --> 00:02:03,000 +and also in terms of the memory. + +55 +00:02:03,000 --> 00:02:05,000 +Now what about performance? Now think about this. + +56 +00:02:05,000 --> 00:02:07,000 +If you talk about any software application, + +57 +00:02:07,000 --> 00:02:10,000 +which we use, it can be a normal calculator + +58 +00:02:10,000 --> 00:02:13,000 +or it can be an application like Amazon website + +59 +00:02:13,000 --> 00:02:14,000 +or any application, which we use + +60 +00:02:14,000 --> 00:02:16,000 +a banking application as well. + +61 +00:02:16,000 --> 00:02:19,000 +Now, what we do in that application is we use some features + +62 +00:02:19,000 --> 00:02:21,000 +using which you can compute something, + +63 +00:02:21,000 --> 00:02:22,000 +you can process something. + +64 +00:02:22,000 --> 00:02:23,000 +Example on calculator, you calculate + +65 +00:02:23,000 --> 00:02:26,000 +on the banking website, you transfer money. + +66 +00:02:26,000 --> 00:02:28,000 +So what you're doing is you are basically building + +67 +00:02:28,000 --> 00:02:30,000 +an application which will do some processing. + +68 +00:02:30,000 --> 00:02:33,000 +And the way you build an application is through algorithms. + +69 +00:02:33,000 --> 00:02:36,000 +Now, what algorithms set of instructions, + +70 +00:02:36,000 --> 00:02:39,000 +let's say if I want to add two numbers, it's very simple. + +71 +00:02:39,000 --> 00:02:42,000 +What you do is you say, take two values from the user, + +72 +00:02:42,000 --> 00:02:44,000 +then perform the operation + +73 +00:02:44,000 --> 00:02:47,000 +and give the value back to the user. + +74 +00:02:47,000 --> 00:02:48,000 +Now those are the steps, right? + +75 +00:02:48,000 --> 00:02:49,000 +Those are instructions. + +76 +00:02:49,000 --> 00:02:51,000 +Now what I have mentioned, the instructions, + +77 +00:02:51,000 --> 00:02:53,000 +those are called pseudo code. + +78 +00:02:53,000 --> 00:02:55,000 +Because we're not actually typing a code here. + +79 +00:02:55,000 --> 00:02:57,000 +We are not being specific to a language, + +80 +00:02:57,000 --> 00:03:00,000 +let's say C, C++, Java, Python, JavaScript. + +81 +00:03:00,000 --> 00:03:02,000 +What we are simply saying is these are the steps + +82 +00:03:02,000 --> 00:03:05,000 +you have to follow and that's your algorithm, right? + +83 +00:03:05,000 --> 00:03:08,000 +But yes, when you want to make it work, + +84 +00:03:08,000 --> 00:03:11,000 +you have to convert that into a code + +85 +00:03:11,000 --> 00:03:13,000 +which will run on the computer, right? + +86 +00:03:13,000 --> 00:03:15,000 +That's your actual software code. + +87 +00:03:15,000 --> 00:03:17,000 +And you can use any language as a matter, right? + +88 +00:03:17,000 --> 00:03:20,000 +But the pseudo code will remain same. + +89 +00:03:20,000 --> 00:03:23,000 +Now, when it comes to processing of data for any task, + +90 +00:03:23,000 --> 00:03:27,000 +it's important for us to make it fast and also save memory. + +91 +00:03:27,000 --> 00:03:29,000 +Now, most of the companies are focusing + +92 +00:03:29,000 --> 00:03:31,000 +on this concept of data structures + +93 +00:03:31,000 --> 00:03:34,000 +is because they want to give a good experience to the user. + +94 +00:03:34,000 --> 00:03:35,000 +Of course, right? + +95 +00:03:35,000 --> 00:03:37,000 +If I'm using some application, + +96 +00:03:37,000 --> 00:03:38,000 +I want it to be fast. + +97 +00:03:38,000 --> 00:03:41,000 +Now you will say, okay, to make the system faster, + +98 +00:03:41,000 --> 00:03:43,000 +you can increase the CPU speed, + +99 +00:03:43,000 --> 00:03:45,000 +you can increase the amount of RAM, + +100 +00:03:45,000 --> 00:03:46,000 +you can do that. + +101 +00:03:46,000 --> 00:03:49,000 +But what if with the same amount of memory, + +102 +00:03:49,000 --> 00:03:51,000 +same amount of CPU power, + +103 +00:03:51,000 --> 00:03:54,000 +you can still make it more faster, right? + +104 +00:03:54,000 --> 00:03:57,000 +And that can be done with the help of data structures. + +105 +00:03:57,000 --> 00:03:58,000 +Now, if you know, how do you store + +106 +00:03:58,000 --> 00:04:01,000 +that data efficiently in a proper structure, + +107 +00:04:01,000 --> 00:04:04,000 +and by doing that, you can make your application faster. + +108 +00:04:04,000 --> 00:04:06,000 +Because in data structures we have different + +109 +00:04:06,000 --> 00:04:08,000 +type of data structures. + +110 +00:04:08,000 --> 00:04:10,000 +Example, let's say if you have bunch of data, + +111 +00:04:10,000 --> 00:04:12,000 +you can store that in an array. + +112 +00:04:12,000 --> 00:04:14,000 +But apart from array, we have other types as well. + +113 +00:04:14,000 --> 00:04:17,000 +We have set, we have LinkedList. + +114 +00:04:17,000 --> 00:04:18,000 +So when to use what? + +115 +00:04:18,000 --> 00:04:20,000 +It's not that this is best or that is best, + +116 +00:04:20,000 --> 00:04:22,000 +it's all about when to use what. + +117 +00:04:22,000 --> 00:04:24,000 +And to understand when to use what, + +118 +00:04:24,000 --> 00:04:28,000 +you have to first understand what those things are, right? + +119 +00:04:28,000 --> 00:04:30,000 +How do you decide that this time you have to use array. + +120 +00:04:30,000 --> 00:04:31,000 +This time you have to use set. + +121 +00:04:31,000 --> 00:04:32,000 +And that's where understanding + +122 +00:04:32,000 --> 00:04:34,000 +these concepts are very important. + +123 +00:04:34,000 --> 00:04:35,000 +Now, these are not the only options we have. + +124 +00:04:35,000 --> 00:04:37,000 +We have tree, we have graph. + +125 +00:04:37,000 --> 00:04:39,000 +When to use them? + +126 +00:04:39,000 --> 00:04:41,000 +So when you understand those concepts, + +127 +00:04:41,000 --> 00:04:42,000 +then we can think about, okay, + +128 +00:04:42,000 --> 00:04:44,000 +for this situation, we will use this. + +129 +00:04:44,000 --> 00:04:46,000 +And this is why companies are pre candidates + +130 +00:04:46,000 --> 00:04:48,000 +who knows DSA. + +131 +00:04:48,000 --> 00:04:50,000 +It'll help them in multiple ways. + +132 +00:04:50,000 --> 00:04:52,000 +First, it helps them to reduce the cost. + +133 +00:04:52,000 --> 00:04:55,000 +It's because every computation, see, + +134 +00:04:55,000 --> 00:04:57,000 +I know, you might be thinking, + +135 +00:04:57,000 --> 00:04:59,000 +most of the application which we use are free, right? + +136 +00:04:59,000 --> 00:05:00,000 +Think about Instagram. + +137 +00:05:00,000 --> 00:05:01,000 +Now, when we use Instagram, + +138 +00:05:01,000 --> 00:05:03,000 +of course we are not paying for it, + +139 +00:05:03,000 --> 00:05:04,000 +but then companies are paying for it, right? + +140 +00:05:04,000 --> 00:05:09,000 +So the Meta is paying for for you to use Instagram. + +141 +00:05:09,000 --> 00:05:12,000 +So because those computations will be happening somewhere, + +142 +00:05:12,000 --> 00:05:14,000 +maybe Meta is using some cloud service. + +143 +00:05:14,000 --> 00:05:15,000 +Let's say Amazon is in this case. + +144 +00:05:15,000 --> 00:05:19,000 +So Meta is basically paying to Amazon for every computation, + +145 +00:05:19,000 --> 00:05:21,000 +which you do, okay? + +146 +00:05:21,000 --> 00:05:23,000 +Of course, they earn from ads, + +147 +00:05:23,000 --> 00:05:25,000 +but then they are paying for it. + +148 +00:05:25,000 --> 00:05:27,000 +So what they will do is they will try to optimize it. + +149 +00:05:27,000 --> 00:05:30,000 +They will say, okay, if one query takes, + +150 +00:05:30,000 --> 00:05:33,000 +let's say, $1 or maybe half a dollar, + +151 +00:05:33,000 --> 00:05:34,000 +can we just reduce it more? + +152 +00:05:34,000 --> 00:05:36,000 +Can we just make it 10 cents? + +153 +00:05:36,000 --> 00:05:39,000 +So that's the thing they're trying to do. + +154 +00:05:39,000 --> 00:05:40,000 +And the way you can do that + +155 +00:05:40,000 --> 00:05:44,000 +is by making sure you use a proper algorithm + +156 +00:05:44,000 --> 00:05:46,000 +with a proper data structure. + +157 +00:05:46,000 --> 00:05:49,000 +So why companies are doing it to reduce the cost. + +158 +00:05:49,000 --> 00:05:51,000 +Second, to give a better customer experience + +159 +00:05:51,000 --> 00:05:53,000 +so that it'll run faster. + +160 +00:05:53,000 --> 00:05:54,000 +And if I search something, + +161 +00:05:54,000 --> 00:05:55,000 +it should be faster for me as well. + +162 +00:05:55,000 --> 00:05:56,000 +So as a customer. + +163 +00:05:56,000 --> 00:05:59,000 +Next, when companies want to hire people, + +164 +00:05:59,000 --> 00:06:00,000 +they have so many candidates, right? + +165 +00:06:00,000 --> 00:06:02,000 +How will they filter those candidates? + +166 +00:06:02,000 --> 00:06:04,000 +Now, data structures, algorithm becomes one + +167 +00:06:04,000 --> 00:06:06,000 +of the way to filter the candidates. + +168 +00:06:06,000 --> 00:06:08,000 +Because if you know data structures, + +169 +00:06:08,000 --> 00:06:11,000 +that means you have worked a lot on that particular language + +170 +00:06:11,000 --> 00:06:13,000 +and you understand how a particular system works. + +171 +00:06:13,000 --> 00:06:17,000 +On the other hand, data structures are not the only thing + +172 +00:06:17,000 --> 00:06:19,000 +you need to know if you want to be a good developer, + +173 +00:06:19,000 --> 00:06:22,000 +if you want to get hired, there are multiple things needed. + +174 +00:06:22,000 --> 00:06:24,000 +Example, you need to have a good hold on + +175 +00:06:24,000 --> 00:06:26,000 +a particular language, a particular technology, + +176 +00:06:26,000 --> 00:06:28,000 +you should have worked on few projects, + +177 +00:06:28,000 --> 00:06:30,000 +understanding the entire ecosystem, + +178 +00:06:30,000 --> 00:06:33,000 +not just one language, working with databases, + +179 +00:06:33,000 --> 00:06:34,000 +working with networking. + +180 +00:06:34,000 --> 00:06:36,000 +But then DSA becomes one of the important thing there. + +181 +00:06:36,000 --> 00:06:39,000 +So just to summarize, what are data structures? + +182 +00:06:39,000 --> 00:06:41,000 +So data structures is basically a way to organize + +183 +00:06:41,000 --> 00:06:44,000 +and store your data in the efficient way. + +184 +00:06:44,000 --> 00:06:45,000 +And there are multiple data structures + +185 +00:06:45,000 --> 00:06:46,000 +option available there. + +186 +00:06:46,000 --> 00:06:48,000 +And you'll understand that in the upcoming sessions. + +187 +00:06:48,000 --> 00:06:51,000 +We can talk about in the upcoming videos, + +188 +00:06:51,000 --> 00:06:53,000 +we will talk about what are types + +189 +00:06:53,000 --> 00:06:55,000 +of data structures we have, how to use them, + +190 +00:06:55,000 --> 00:06:57,000 +when to use what, and what are the algorithms + +191 +00:06:57,000 --> 00:06:57,000 +available there. + +192 +00:06:57,000 --> 00:07:00,000 +So I hope you got some idea regarding data structures. + +193 +00:07:00,000 --> 00:07:02,000 +So of course entire series is important + +194 +00:07:02,000 --> 00:07:03,000 +to understand that properly. + +195 +00:07:03,000 --> 00:07:04,000 +So I hope you're excited. + +196 +00:07:04,000 --> 00:07:05,000 +See you in the next video. + diff --git a/28 - DSA (Optional)/002 Abstract Data Types_en.srt b/28 - DSA (Optional)/002 Abstract Data Types_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..08683891803dbe15e399defa9468cf400df9580f --- /dev/null +++ b/28 - DSA (Optional)/002 Abstract Data Types_en.srt @@ -0,0 +1,968 @@ +1 +00:00:00,000 --> 00:00:01,000 +-: Welcome back, aliens. + +2 +00:00:01,000 --> 00:00:02,000 +My name is Navin Reddy. + +3 +00:00:02,000 --> 00:00:02,000 +And in this video, + +4 +00:00:02,000 --> 00:00:05,000 +we'll talk about ADT, + +5 +00:00:05,000 --> 00:00:07,000 +which is abstract datatype. + +6 +00:00:07,000 --> 00:00:09,000 +Now, before we move towards ADT, + +7 +00:00:09,000 --> 00:00:11,000 +let's talk about data here. + +8 +00:00:11,000 --> 00:00:12,000 +Of course, in the previous video + +9 +00:00:12,000 --> 00:00:14,000 +when we talked about what are data structures, + +10 +00:00:14,000 --> 00:00:16,000 +we have talked about data. + +11 +00:00:16,000 --> 00:00:16,000 +And we also mentioned + +12 +00:00:16,000 --> 00:00:18,000 +that in the software industry, + +13 +00:00:18,000 --> 00:00:20,000 +everything is about data. + +14 +00:00:20,000 --> 00:00:21,000 +So whatever you do, + +15 +00:00:21,000 --> 00:00:23,000 +you are doing it for data, right? + +16 +00:00:23,000 --> 00:00:24,000 +Now with this data, + +17 +00:00:24,000 --> 00:00:26,000 +basically, you get the data from the user, + +18 +00:00:26,000 --> 00:00:28,000 +you process data, + +19 +00:00:28,000 --> 00:00:30,000 +you also give the output to the user, + +20 +00:00:30,000 --> 00:00:33,000 +or maybe you want to store this data in the database + +21 +00:00:33,000 --> 00:00:34,000 +for the permanent storage. + +22 +00:00:34,000 --> 00:00:36,000 +But the thing is, it's all about data. + +23 +00:00:36,000 --> 00:00:38,000 +And whenever we use any language, + +24 +00:00:38,000 --> 00:00:40,000 +it doesn't matter which language you use, + +25 +00:00:40,000 --> 00:00:42,000 +what you do is you store that data + +26 +00:00:42,000 --> 00:00:44,000 +in your code somewhere. + +27 +00:00:44,000 --> 00:00:45,000 +And to store that data, + +28 +00:00:45,000 --> 00:00:47,000 +we use something called a variable. + +29 +00:00:47,000 --> 00:00:49,000 +Now, imagine variable as a box + +30 +00:00:49,000 --> 00:00:52,000 +and you are keeping your data inside that box. + +31 +00:00:52,000 --> 00:00:54,000 +The problem is, + +32 +00:00:54,000 --> 00:00:56,000 +every box need to have a type. + +33 +00:00:56,000 --> 00:00:57,000 +Of course, there are languages + +34 +00:00:57,000 --> 00:00:59,000 +which are the dynamically-typed language + +35 +00:00:59,000 --> 00:01:01,000 +where you don't actually mention + +36 +00:01:01,000 --> 00:01:02,000 +the type of the variable. + +37 +00:01:02,000 --> 00:01:05,000 +But in general, this box will have a type. + +38 +00:01:05,000 --> 00:01:07,000 +So that means if you want to store the data, + +39 +00:01:07,000 --> 00:01:09,000 +you need a box, which is your variable. + +40 +00:01:09,000 --> 00:01:12,000 +And this variable also needs a type to it, + +41 +00:01:12,000 --> 00:01:15,000 +or this box needs a type to it. + +42 +00:01:15,000 --> 00:01:17,000 +Now example, let's say, if you want to store a number, + +43 +00:01:17,000 --> 00:01:19,000 +so we can use something called an integer, + +44 +00:01:19,000 --> 00:01:21,000 +or if you have any other point values, + +45 +00:01:21,000 --> 00:01:23,000 +you can also use float or double. + +46 +00:01:23,000 --> 00:01:26,000 +Now, it depends upon of the languages how they work, + +47 +00:01:26,000 --> 00:01:27,000 +but in most of the languages, + +48 +00:01:27,000 --> 00:01:28,000 +we have something in common. + +49 +00:01:28,000 --> 00:01:31,000 +We have integer for the normal numbers, + +50 +00:01:31,000 --> 00:01:34,000 +we have a float for the point values. + +51 +00:01:34,000 --> 00:01:36,000 +And what if you want to store a text? + +52 +00:01:36,000 --> 00:01:37,000 +That's why we have string. + +53 +00:01:37,000 --> 00:01:39,000 +And in few languages, + +54 +00:01:39,000 --> 00:01:41,000 +we don't have all these types, + +55 +00:01:41,000 --> 00:01:42,000 +we have limited types, + +56 +00:01:42,000 --> 00:01:44,000 +and in other languages we have many. + +57 +00:01:44,000 --> 00:01:45,000 +But that doesn't matter, right? + +58 +00:01:45,000 --> 00:01:48,000 +In general, we'll be having a variable + +59 +00:01:48,000 --> 00:01:49,000 +and a type to it. + +60 +00:01:49,000 --> 00:01:51,000 +Now, all these type are called datatypes + +61 +00:01:51,000 --> 00:01:55,000 +and they are the system defined datatypes, + +62 +00:01:55,000 --> 00:01:57,000 +or you can also call them as primitive types + +63 +00:01:57,000 --> 00:01:59,000 +is because it's there in the language itself, + +64 +00:01:59,000 --> 00:02:00,000 +you can directly use them. + +65 +00:02:00,000 --> 00:02:04,000 +But sometime, you want to use some complex datatype, + +66 +00:02:04,000 --> 00:02:05,000 +and we can create that + +67 +00:02:05,000 --> 00:02:06,000 +with the help of some other concepts. + +68 +00:02:06,000 --> 00:02:09,000 +Example, let's say, if you want to represent a human + +69 +00:02:09,000 --> 00:02:11,000 +or if you want to represent a phone. + +70 +00:02:11,000 --> 00:02:12,000 +Now, I love phone, + +71 +00:02:12,000 --> 00:02:13,000 +so I'll be using that example. + +72 +00:02:13,000 --> 00:02:16,000 +So if we talk about this phone here, + +73 +00:02:16,000 --> 00:02:18,000 +now this phone will have a name to it, right? + +74 +00:02:18,000 --> 00:02:19,000 +So it'll have a name, + +75 +00:02:19,000 --> 00:02:20,000 +it'll have a brand. + +76 +00:02:20,000 --> 00:02:21,000 +Of course, name is model number. + +77 +00:02:21,000 --> 00:02:23,000 +Then brand, then the configuration, + +78 +00:02:23,000 --> 00:02:25,000 +the amount of CPU, RAM. + +79 +00:02:25,000 --> 00:02:27,000 +All this are your data, right? + +80 +00:02:27,000 --> 00:02:28,000 +So if you want to represent + +81 +00:02:28,000 --> 00:02:31,000 +any physical thing in the virtual world, + +82 +00:02:31,000 --> 00:02:33,000 +we use objects there, right? + +83 +00:02:33,000 --> 00:02:34,000 +Now, in some languages + +84 +00:02:34,000 --> 00:02:36,000 +which are object oriented programming, + +85 +00:02:36,000 --> 00:02:37,000 +we do that with the help of objects. + +86 +00:02:37,000 --> 00:02:41,000 +Example, in C, we use something called structures. + +87 +00:02:41,000 --> 00:02:43,000 +So we can define a structure name, + +88 +00:02:43,000 --> 00:02:44,000 +let's say, phone, + +89 +00:02:44,000 --> 00:02:45,000 +and inside that you can specify + +90 +00:02:45,000 --> 00:02:47,000 +what are the properties there. + +91 +00:02:47,000 --> 00:02:48,000 +In the same way, + +92 +00:02:48,000 --> 00:02:49,000 +in the object oriented languages, + +93 +00:02:49,000 --> 00:02:51,000 +we use something called class. + +94 +00:02:51,000 --> 00:02:52,000 +In this class, you create + +95 +00:02:52,000 --> 00:02:54,000 +different variables with primitive type. + +96 +00:02:54,000 --> 00:02:56,000 +And then the box itself + +97 +00:02:56,000 --> 00:02:58,000 +or the class itself + +98 +00:02:58,000 --> 00:02:59,000 +is your complex datatype, + +99 +00:02:59,000 --> 00:03:02,000 +or you can say user defined datatype. + +100 +00:03:02,000 --> 00:03:05,000 +User defined, because system is not giving you, + +101 +00:03:05,000 --> 00:03:07,000 +we are defining our own type, right? + +102 +00:03:07,000 --> 00:03:10,000 +So that's about data and datatypes. + +103 +00:03:10,000 --> 00:03:13,000 +But, let's say, if you want to store a bunch of data, + +104 +00:03:13,000 --> 00:03:15,000 +how will you work with that? + +105 +00:03:15,000 --> 00:03:17,000 +And when it comes to data structure, + +106 +00:03:17,000 --> 00:03:19,000 +how will you store this data in a proper way? + +107 +00:03:19,000 --> 00:03:20,000 +See what happens is, + +108 +00:03:20,000 --> 00:03:22,000 +when you talk about primitive types as well, + +109 +00:03:22,000 --> 00:03:25,000 +every type will have a way to store data. + +110 +00:03:25,000 --> 00:03:26,000 +Of course, that's why we're getting them, right? + +111 +00:03:26,000 --> 00:03:29,000 +So example, if you are getting a variable called num. + +112 +00:03:29,000 --> 00:03:32,000 +Now, let's say this num is of type integer, + +113 +00:03:32,000 --> 00:03:34,000 +and then you are storing a value to it. + +114 +00:03:34,000 --> 00:03:35,000 +Now with this variable num, + +115 +00:03:35,000 --> 00:03:37,000 +you can perform operations as well. + +116 +00:03:37,000 --> 00:03:39,000 +Now, what are the options you can do within integer? + +117 +00:03:39,000 --> 00:03:39,000 +You can add two values, + +118 +00:03:39,000 --> 00:03:41,000 +you can subtract two values, + +119 +00:03:41,000 --> 00:03:42,000 +you can divide, multiply. + +120 +00:03:42,000 --> 00:03:45,000 +So you can perform those operations on the number. + +121 +00:03:45,000 --> 00:03:47,000 +But, let's say, you have a string there. + +122 +00:03:47,000 --> 00:03:48,000 +Now with string, of course, + +123 +00:03:48,000 --> 00:03:49,000 +you will not be adding two string. + +124 +00:03:49,000 --> 00:03:51,000 +That doesn't make sense, right? + +125 +00:03:51,000 --> 00:03:52,000 +Why you will add two names? + +126 +00:03:52,000 --> 00:03:53,000 +Let's say, Navin and Kiran, + +127 +00:03:53,000 --> 00:03:54,000 +you will get a new name. + +128 +00:03:54,000 --> 00:03:56,000 +Okay, a lot of parents are doing that for their kids, + +129 +00:03:56,000 --> 00:03:57,000 +but that's not the idea here, right? + +130 +00:03:57,000 --> 00:03:58,000 +We can't add a string, + +131 +00:03:58,000 --> 00:04:00,000 +we cannot subtract strings. + +132 +00:04:00,000 --> 00:04:01,000 +But yes, with string you can do something else. + +133 +00:04:01,000 --> 00:04:04,000 +Maybe you want to see the size of a string, + +134 +00:04:04,000 --> 00:04:06,000 +maybe you want to concatenate two strings, + +135 +00:04:06,000 --> 00:04:08,000 +maybe you want to cut two strings. + +136 +00:04:08,000 --> 00:04:09,000 +So string has different operations. + +137 +00:04:09,000 --> 00:04:10,000 +In the same way, + +138 +00:04:10,000 --> 00:04:13,000 +if you are creating your own type, + +139 +00:04:13,000 --> 00:04:15,000 +this will have data, of course, + +140 +00:04:15,000 --> 00:04:18,000 +but also you have to mention the operations + +141 +00:04:18,000 --> 00:04:18,000 +which you're going to perform. + +142 +00:04:18,000 --> 00:04:19,000 +So we have two things, right? + +143 +00:04:19,000 --> 00:04:22,000 +We have data and we have operations. + +144 +00:04:22,000 --> 00:04:24,000 +Now, let's say, in data structures, + +145 +00:04:24,000 --> 00:04:26,000 +you want to store a bunch of data, + +146 +00:04:26,000 --> 00:04:27,000 +as we mentioned, + +147 +00:04:27,000 --> 00:04:28,000 +and you want to store. + +148 +00:04:28,000 --> 00:04:29,000 +So there's a concept of array. + +149 +00:04:29,000 --> 00:04:30,000 +So if you have multiple data, + +150 +00:04:30,000 --> 00:04:33,000 +instead of showing them in multiple variables, + +151 +00:04:33,000 --> 00:04:35,000 +let's say, you have five numbers, + +152 +00:04:35,000 --> 00:04:38,000 +two, six, 12, 21, and so on. + +153 +00:04:38,000 --> 00:04:39,000 +Now, if you have all these values, + +154 +00:04:39,000 --> 00:04:42,000 +you basically store that in five different variables, + +155 +00:04:42,000 --> 00:04:45,000 +or you can create a single array + +156 +00:04:45,000 --> 00:04:47,000 +and you can store all the values there. + +157 +00:04:47,000 --> 00:04:48,000 +The advantage would be, + +158 +00:04:48,000 --> 00:04:50,000 +you have just have to use one variable name there. + +159 +00:04:50,000 --> 00:04:53,000 +So instead of saying A, B, C, D, E, + +160 +00:04:53,000 --> 00:04:55,000 +you can simply say nums. + +161 +00:04:55,000 --> 00:04:57,000 +You can use any variable, doesn't matter. + +162 +00:04:57,000 --> 00:04:59,000 +So what's important is you can use array here. + +163 +00:04:59,000 --> 00:05:00,000 +Now, when we use array, + +164 +00:05:00,000 --> 00:05:03,000 +we are not basically using a primitive type here. + +165 +00:05:03,000 --> 00:05:05,000 +We are creating our own type here, + +166 +00:05:05,000 --> 00:05:07,000 +own datatype, which is an array. + +167 +00:05:07,000 --> 00:05:09,000 +Now, in this array, + +168 +00:05:09,000 --> 00:05:11,000 +basically, you should be also able + +169 +00:05:11,000 --> 00:05:12,000 +to perform some operation. + +170 +00:05:12,000 --> 00:05:15,000 +Example, let's say, what if you want to read some value, + +171 +00:05:15,000 --> 00:05:17,000 +you want to fetch a particular element, + +172 +00:05:17,000 --> 00:05:18,000 +you want to search the array, + +173 +00:05:18,000 --> 00:05:20,000 +maybe you want to add a element, + +174 +00:05:20,000 --> 00:05:22,000 +maybe you want to delete a element. + +175 +00:05:22,000 --> 00:05:25,000 +So this operation should be possible on that type. + +176 +00:05:25,000 --> 00:05:27,000 +So in the concept way, + +177 +00:05:27,000 --> 00:05:28,000 +when you have a type + +178 +00:05:28,000 --> 00:05:29,000 +where you can perform some operation, + +179 +00:05:29,000 --> 00:05:31,000 +we call them as abstract. + +180 +00:05:31,000 --> 00:05:33,000 +This is because the implementation for array + +181 +00:05:33,000 --> 00:05:34,000 +changes from language to language. + +182 +00:05:34,000 --> 00:05:36,000 +And we don't just have array, right? + +183 +00:05:36,000 --> 00:05:38,000 +So, let's say, if you want to store a bunch of values, + +184 +00:05:38,000 --> 00:05:39,000 +we can use list, + +185 +00:05:39,000 --> 00:05:41,000 +we can use set, queue. + +186 +00:05:41,000 --> 00:05:44,000 +Now, when you talk about a queue here, let's say. + +187 +00:05:44,000 --> 00:05:46,000 +Now inside queue, we have, + +188 +00:05:46,000 --> 00:05:47,000 +I mean, we have one more option for that, + +189 +00:05:47,000 --> 00:05:48,000 +which is stack. + +190 +00:05:48,000 --> 00:05:49,000 +They follow different concepts. + +191 +00:05:49,000 --> 00:05:51,000 +So let's say, if you want to add element in the queue, + +192 +00:05:51,000 --> 00:05:53,000 +which follows last in first out, + +193 +00:05:53,000 --> 00:05:56,000 +which means you have to insert from the end + +194 +00:05:56,000 --> 00:05:57,000 +and you will get it from the first. + +195 +00:05:57,000 --> 00:05:58,000 +Of course, you can reverse it. + +196 +00:05:58,000 --> 00:05:59,000 +You can insert from the start + +197 +00:05:59,000 --> 00:06:01,000 +and you can take it from the end. + +198 +00:06:01,000 --> 00:06:03,000 +The thing is, you will insert from one end + +199 +00:06:03,000 --> 00:06:04,000 +and you will remove from the other end. + +200 +00:06:04,000 --> 00:06:06,000 +So that is first in first out. + +201 +00:06:06,000 --> 00:06:07,000 +In stack, it is reverse. + +202 +00:06:07,000 --> 00:06:09,000 +You do last in first out. + +203 +00:06:09,000 --> 00:06:11,000 +Example, let's say, took an example, + +204 +00:06:11,000 --> 00:06:12,000 +let's say, for queue, + +205 +00:06:12,000 --> 00:06:14,000 +we can say a queue, which you follow, right? + +206 +00:06:14,000 --> 00:06:15,000 +Example, if you want to buy a coffee + +207 +00:06:15,000 --> 00:06:17,000 +from a cafe shop, + +208 +00:06:17,000 --> 00:06:18,000 +of course, there's a queue there. + +209 +00:06:18,000 --> 00:06:19,000 +You have to stand in a queue. + +210 +00:06:19,000 --> 00:06:21,000 +And then when your number comes, + +211 +00:06:21,000 --> 00:06:23,000 +you will get the coffee. + +212 +00:06:23,000 --> 00:06:24,000 +So basically, the first person + +213 +00:06:24,000 --> 00:06:26,000 +who went there first + +214 +00:06:26,000 --> 00:06:28,000 +will be getting the coffee first, right? + +215 +00:06:28,000 --> 00:06:29,000 +In terms of stack, it is different. + +216 +00:06:29,000 --> 00:06:31,000 +So when you keep multiple books, + +217 +00:06:31,000 --> 00:06:33,000 +the first book which you can take out + +218 +00:06:33,000 --> 00:06:34,000 +is the last book which you have kept there, right, + +219 +00:06:34,000 --> 00:06:36,000 +which is the last in first out. + +220 +00:06:36,000 --> 00:06:37,000 +That is your stack. + +221 +00:06:37,000 --> 00:06:39,000 +Now, we have different way + +222 +00:06:39,000 --> 00:06:41,000 +of implementing in different languages. + +223 +00:06:41,000 --> 00:06:42,000 +The concept remain same, right? + +224 +00:06:42,000 --> 00:06:44,000 +So when you have a concept + +225 +00:06:44,000 --> 00:06:47,000 +and the associated operations to it, + +226 +00:06:47,000 --> 00:06:48,000 +and, of course, with data, + +227 +00:06:48,000 --> 00:06:51,000 +which we call them as abstract data type. + +228 +00:06:51,000 --> 00:06:52,000 +We also have map, + +229 +00:06:52,000 --> 00:06:54,000 +but we'll talk about those things later. + +230 +00:06:54,000 --> 00:06:56,000 +At this point, the point to remember + +231 +00:06:56,000 --> 00:06:59,000 +is we can create our own types, + +232 +00:06:59,000 --> 00:07:00,000 +and which is a concept. + +233 +00:07:00,000 --> 00:07:03,000 +And if you want to have data inside that, + +234 +00:07:03,000 --> 00:07:04,000 +and also you want to specify + +235 +00:07:04,000 --> 00:07:06,000 +what operations you can perform, + +236 +00:07:06,000 --> 00:07:09,000 +that is your abstract data type. + +237 +00:07:09,000 --> 00:07:11,000 +Of course, in the subsequent videos, + +238 +00:07:11,000 --> 00:07:13,000 +we'll talk about how do you create them, + +239 +00:07:13,000 --> 00:07:14,000 +how do you work with them, + +240 +00:07:14,000 --> 00:07:15,000 +and it'll be fun. + +241 +00:07:15,000 --> 00:07:18,000 +So, see you in the upcoming videos everyone. + +242 +00:07:18,000 --> 00:07:18,000 +Bye-Bye. + diff --git a/28 - DSA (Optional)/003 Arrays_en.srt b/28 - DSA (Optional)/003 Arrays_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..a0f783fa991fb9986054ec3303229431f4009794 --- /dev/null +++ b/28 - DSA (Optional)/003 Arrays_en.srt @@ -0,0 +1,948 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: In this video, let's talk about arrays. + +2 +00:00:02,000 --> 00:00:04,000 +So let's say you have bunch of values, + +3 +00:00:04,000 --> 00:00:06,000 +let's say five values. + +4 +00:00:06,000 --> 00:00:08,000 +Now, instead of storing them in five different variables, + +5 +00:00:08,000 --> 00:00:12,000 +we can store that in one particular sequence, right? + +6 +00:00:12,000 --> 00:00:13,000 +Or one variable, you can say. + +7 +00:00:13,000 --> 00:00:15,000 +Now in this array you can store five values. + +8 +00:00:15,000 --> 00:00:17,000 +So let's say we have this array here, + +9 +00:00:17,000 --> 00:00:19,000 +and then this will have a name to it. + +10 +00:00:19,000 --> 00:00:22,000 +Now of course, this data will be stored in a memory, right? + +11 +00:00:22,000 --> 00:00:24,000 +And every memory will have something called + +12 +00:00:24,000 --> 00:00:25,000 +a memory location. + +13 +00:00:25,000 --> 00:00:28,000 +So let's say we are storing this data + +14 +00:00:28,000 --> 00:00:31,000 +at the location 101. + +15 +00:00:31,000 --> 00:00:33,000 +Now what happens is every time you store a normal variable, + +16 +00:00:33,000 --> 00:00:36,000 +let's say a premature type, let's say int, + +17 +00:00:36,000 --> 00:00:38,000 +and depending upon how much size it takes. + +18 +00:00:38,000 --> 00:00:40,000 +So every type will have a different size to it, + +19 +00:00:40,000 --> 00:00:42,000 +depending upon the language. + +20 +00:00:42,000 --> 00:00:43,000 +But let's say we have two bytes. + +21 +00:00:43,000 --> 00:00:45,000 +Now inside your memory, + +22 +00:00:45,000 --> 00:00:46,000 +let's say if you create a normal variable, + +23 +00:00:46,000 --> 00:00:48,000 +which is int a equal to five. + +24 +00:00:48,000 --> 00:00:51,000 +Now this particular a variable will take + +25 +00:00:51,000 --> 00:00:53,000 +some space in the memory, of course, let's say two bytes, + +26 +00:00:53,000 --> 00:00:56,000 +and it will also have an address to it, right? + +27 +00:00:56,000 --> 00:00:58,000 +So every time you, in your programming language, you say, + +28 +00:00:58,000 --> 00:01:01,000 +"Hey, I want a value for a", + +29 +00:01:01,000 --> 00:01:03,000 +the program will jump to the memory location + +30 +00:01:03,000 --> 00:01:04,000 +by searching for the address + +31 +00:01:04,000 --> 00:01:06,000 +and it will get the value. + +32 +00:01:06,000 --> 00:01:08,000 +But the problem is when it comes to array, + +33 +00:01:08,000 --> 00:01:10,000 +we have multiple values, right? + +34 +00:01:10,000 --> 00:01:11,000 +And we only have one address. + +35 +00:01:11,000 --> 00:01:12,000 +Now that's tricky. + +36 +00:01:12,000 --> 00:01:15,000 +Of course, each element here will take the same size. + +37 +00:01:15,000 --> 00:01:17,000 +If you say a takes two bytes, + +38 +00:01:17,000 --> 00:01:19,000 +the complete array will take 10 bytes. + +39 +00:01:19,000 --> 00:01:21,000 +It's because every element will take two bytes. + +40 +00:01:21,000 --> 00:01:23,000 +How will you allocate memory now? + +41 +00:01:23,000 --> 00:01:24,000 +That's an issue. + +42 +00:01:24,000 --> 00:01:26,000 +So what you do is, the array will have a memory, + +43 +00:01:26,000 --> 00:01:28,000 +but that will, that is only pointing + +44 +00:01:28,000 --> 00:01:30,000 +to the first location, right? + +45 +00:01:30,000 --> 00:01:33,000 +So if you say, the memory for this is 101, + +46 +00:01:33,000 --> 00:01:35,000 +the first element is 101. + +47 +00:01:35,000 --> 00:01:36,000 +What about the second element? + +48 +00:01:36,000 --> 00:01:38,000 +Now that's where we have to use + +49 +00:01:38,000 --> 00:01:40,000 +something called a index value. + +50 +00:01:40,000 --> 00:01:44,000 +So let's say the first element is, look, is at 101. + +51 +00:01:44,000 --> 00:01:47,000 +So it'll have a index value, which is zero. + +52 +00:01:47,000 --> 00:01:48,000 +The second element is one, + +53 +00:01:48,000 --> 00:01:50,000 +then two, then three, then four, right? + +54 +00:01:50,000 --> 00:01:53,000 +Now for five values, the index value starts from zero + +55 +00:01:53,000 --> 00:01:55,000 +and ends at four. + +56 +00:01:55,000 --> 00:01:57,000 +Now why zero, is because we are start, + +57 +00:01:57,000 --> 00:01:59,000 +we have a memory allocated, right, 101, + +58 +00:01:59,000 --> 00:02:00,000 +which represent to the first element, + +59 +00:02:00,000 --> 00:02:02,000 +so we don't have to give one to it. + +60 +00:02:02,000 --> 00:02:05,000 +So what you do is the next element is plus one. + +61 +00:02:05,000 --> 00:02:07,000 +That's why 1, 2, 3, and then you can just get it right. + +62 +00:02:07,000 --> 00:02:11,000 +So 101, let's say if you want to get the third element, + +63 +00:02:11,000 --> 00:02:13,000 +you have to say plus two, that's how we do it. + +64 +00:02:13,000 --> 00:02:14,000 +So you got the values, + +65 +00:02:14,000 --> 00:02:17,000 +you got the index, and you also got a memory address to it. + +66 +00:02:17,000 --> 00:02:20,000 +Now with this array, as we've already talked about a ADT, + +67 +00:02:20,000 --> 00:02:22,000 +which is abstract data type, we should be able + +68 +00:02:22,000 --> 00:02:23,000 +to perform some operations. + +69 +00:02:23,000 --> 00:02:25,000 +Now, what operations you can perform on this array? + +70 +00:02:25,000 --> 00:02:28,000 +Now we can perform something called a read operation. + +71 +00:02:28,000 --> 00:02:29,000 +So what is read? + +72 +00:02:29,000 --> 00:02:31,000 +So let's say you want to get the value + +73 +00:02:31,000 --> 00:02:33,000 +of index number three. + +74 +00:02:33,000 --> 00:02:35,000 +Now what what you do in this case is you say, + +75 +00:02:35,000 --> 00:02:37,000 +okay, my array name is nums, + +76 +00:02:37,000 --> 00:02:40,000 +and then for this nums, I want the value at index three. + +77 +00:02:40,000 --> 00:02:42,000 +So in different languages we have + +78 +00:02:42,000 --> 00:02:43,000 +different representation for this, + +79 +00:02:43,000 --> 00:02:44,000 +but let's say this is common one. + +80 +00:02:44,000 --> 00:02:46,000 +So NUMs bracket of three. + +81 +00:02:46,000 --> 00:02:50,000 +Now what your computer will do is it'll directly jump + +82 +00:02:50,000 --> 00:02:51,000 +to the memory address. + +83 +00:02:51,000 --> 00:02:53,000 +It's very simple for that, right? + +84 +00:02:53,000 --> 00:02:55,000 +So you will say 101 plus three, you will get the value. + +85 +00:02:55,000 --> 00:02:57,000 +So that's how you do it. + +86 +00:02:57,000 --> 00:02:58,000 +Now, reading is very easy and + +87 +00:02:58,000 --> 00:03:00,000 +computer takes less time to + +88 +00:03:00,000 --> 00:03:01,000 +jump to that particular array + +89 +00:03:01,000 --> 00:03:03,000 +is because the computer knows the memory, right? + +90 +00:03:03,000 --> 00:03:06,000 +Because you, you are mentioning the index value. + +91 +00:03:06,000 --> 00:03:07,000 +Reading is good, it's fast also. + +92 +00:03:07,000 --> 00:03:09,000 +What about searching? + +93 +00:03:09,000 --> 00:03:11,000 +Now this is tricky, because when you are searching, + +94 +00:03:11,000 --> 00:03:13,000 +you're not searching for a index, + +95 +00:03:13,000 --> 00:03:15,000 +you are basically searching for a value now. + +96 +00:03:15,000 --> 00:03:17,000 +So let's say from this array, + +97 +00:03:17,000 --> 00:03:18,000 +I want to search where is 17? + +98 +00:03:18,000 --> 00:03:21,000 +Now, in this case, your computer has no idea where 17 is, + +99 +00:03:21,000 --> 00:03:24,000 +because computer only knows about the memory address. + +100 +00:03:24,000 --> 00:03:25,000 +Of course the values are there, + +101 +00:03:25,000 --> 00:03:28,000 +but computer has no idea in which location + +102 +00:03:28,000 --> 00:03:31,000 +we have that value, and do we even have that value? + +103 +00:03:31,000 --> 00:03:32,000 +So what a computer will do is + +104 +00:03:32,000 --> 00:03:33,000 +it will start on the first location. + +105 +00:03:33,000 --> 00:03:35,000 +So basically you have to write a code + +106 +00:03:35,000 --> 00:03:36,000 +to search from the first location. + +107 +00:03:36,000 --> 00:03:39,000 +You have to check, is the first one 17? + +108 +00:03:39,000 --> 00:03:41,000 +If yes, good, we can exit. + +109 +00:03:41,000 --> 00:03:43,000 +No, then you have to jump to the next location. + +110 +00:03:43,000 --> 00:03:45,000 +Is it matching? + +111 +00:03:45,000 --> 00:03:47,000 +If yes, good, if not, next. + +112 +00:03:47,000 --> 00:03:48,000 +Matching? + +113 +00:03:48,000 --> 00:03:49,000 +Yes, no, next. + +114 +00:03:49,000 --> 00:03:53,000 +So basically you have to search each element, right? + +115 +00:03:53,000 --> 00:03:56,000 +And let's say the element is at the end, you are searching, + +116 +00:03:56,000 --> 00:03:59,000 +you are basically tracking to all the different locations. + +117 +00:03:59,000 --> 00:04:01,000 +So this is time-consuming, right? + +118 +00:04:01,000 --> 00:04:04,000 +What if you have array of, let's say, a thousand values? + +119 +00:04:04,000 --> 00:04:06,000 +To move between these values, + +120 +00:04:06,000 --> 00:04:07,000 +it'll take some time. + +121 +00:04:07,000 --> 00:04:08,000 +What about inserting? + +122 +00:04:08,000 --> 00:04:10,000 +Now this will be tricky, is because, + +123 +00:04:10,000 --> 00:04:13,000 +see, if you want to insert a element, what do you think? + +124 +00:04:13,000 --> 00:04:15,000 +Will it take a lot of time? + +125 +00:04:15,000 --> 00:04:17,000 +See if you want to insert the element at the end, + +126 +00:04:17,000 --> 00:04:18,000 +that's very simple. + +127 +00:04:18,000 --> 00:04:20,000 +You can get the size of the array, + +128 +00:04:20,000 --> 00:04:22,000 +because we also have to have that option + +129 +00:04:22,000 --> 00:04:24,000 +of getting the size of the array, right? + +130 +00:04:24,000 --> 00:04:25,000 +So let's say you have five values, + +131 +00:04:25,000 --> 00:04:27,000 +you will simply jump to the sixth location, + +132 +00:04:27,000 --> 00:04:28,000 +which is index five. + +133 +00:04:28,000 --> 00:04:30,000 +And then you'll say, okay, I want to add a value here. + +134 +00:04:30,000 --> 00:04:33,000 +Let's say the value value is 21, + +135 +00:04:33,000 --> 00:04:35,000 +and you can add the value, it's very fast. + +136 +00:04:35,000 --> 00:04:38,000 +But what if you want to add a value in between? + +137 +00:04:38,000 --> 00:04:40,000 +So let's say you want to add that value + +138 +00:04:40,000 --> 00:04:41,000 +at the second location. + +139 +00:04:41,000 --> 00:04:43,000 +So after the first, you want to add the new value. + +140 +00:04:43,000 --> 00:04:44,000 +Now what you will do is, + +141 +00:04:44,000 --> 00:04:46,000 +you don't have a space there, right? + +142 +00:04:46,000 --> 00:04:48,000 +You can't simply create a new space. + +143 +00:04:48,000 --> 00:04:49,000 +You can create space at the end. + +144 +00:04:49,000 --> 00:04:51,000 +So what you do is you basically have to move + +145 +00:04:51,000 --> 00:04:53,000 +all the elements and you, it's not like you can + +146 +00:04:53,000 --> 00:04:55,000 +simply move the elements. + +147 +00:04:55,000 --> 00:04:57,000 +What you do is, create a new block. + +148 +00:04:57,000 --> 00:04:59,000 +So move the second last element now to the last block. + +149 +00:04:59,000 --> 00:05:03,000 +Then again, you have to move every element one by one. + +150 +00:05:03,000 --> 00:05:07,000 +Then you can add your value to the second location. + +151 +00:05:07,000 --> 00:05:10,000 +So if you are inserting at the end, that's fine, + +152 +00:05:10,000 --> 00:05:12,000 +but if you're inserting in between, + +153 +00:05:12,000 --> 00:05:14,000 +that will take a lot of time. + +154 +00:05:14,000 --> 00:05:16,000 +And that depends upon the number of elements + +155 +00:05:16,000 --> 00:05:18,000 +after the position which you're adding. + +156 +00:05:18,000 --> 00:05:19,000 +What about deleting? + +157 +00:05:19,000 --> 00:05:21,000 +See, deleting from a from the end + +158 +00:05:21,000 --> 00:05:22,000 +is always welcome is + +159 +00:05:22,000 --> 00:05:25,000 +because you're not affecting your array in total. + +160 +00:05:25,000 --> 00:05:27,000 +But what if you want to delete + +161 +00:05:27,000 --> 00:05:29,000 +a particular element from between? + +162 +00:05:29,000 --> 00:05:31,000 +Now, you can't simply delete from the between, right? + +163 +00:05:31,000 --> 00:05:33,000 +You delete the end block, but how will you move? + +164 +00:05:33,000 --> 00:05:35,000 +So again, when you have to, when you want to delete this, + +165 +00:05:35,000 --> 00:05:38,000 +just replace all the values, right? + +166 +00:05:38,000 --> 00:05:39,000 +So that's, that's tricky. + +167 +00:05:39,000 --> 00:05:41,000 +So it'll take a lot of time, depend upon + +168 +00:05:41,000 --> 00:05:43,000 +how many elements you have after that index value. + +169 +00:05:43,000 --> 00:05:45,000 +Now, why we are talking about all this thing? + +170 +00:05:45,000 --> 00:05:46,000 +Array was simple, right? + +171 +00:05:46,000 --> 00:05:47,000 +We can perform the operations. + +172 +00:05:47,000 --> 00:05:51,000 +The important thing here is time taken for each operation. + +173 +00:05:51,000 --> 00:05:53,000 +And it's not about CPU time. + +174 +00:05:53,000 --> 00:05:56,000 +If you're thinking, "My computer is super fast, + +175 +00:05:56,000 --> 00:05:58,000 +it'll be happening very fast there", + +176 +00:05:58,000 --> 00:06:00,000 +see, in the world we have different computers + +177 +00:06:00,000 --> 00:06:01,000 +and different computer have different CPU power, + +178 +00:06:01,000 --> 00:06:04,000 +different RAM power, different configuration. + +179 +00:06:04,000 --> 00:06:06,000 +And that's why let's not calculate a speed of our, + +180 +00:06:06,000 --> 00:06:09,000 +a speed of a code or the algorithm by the actual runtime. + +181 +00:06:09,000 --> 00:06:13,000 +Important is the number of steps it takes. + +182 +00:06:13,000 --> 00:06:14,000 +Example, let's say if you want to + +183 +00:06:14,000 --> 00:06:17,000 +insert the element at the end, that's good. + +184 +00:06:17,000 --> 00:06:19,000 +If you want to insert the element in between, + +185 +00:06:19,000 --> 00:06:20,000 +that will take a lot of steps, + +186 +00:06:20,000 --> 00:06:23,000 +it's because you want to move the elements, right? + +187 +00:06:23,000 --> 00:06:25,000 +Now in the upcoming videos + +188 +00:06:25,000 --> 00:06:27,000 +we want to also talk about time complexity, + +189 +00:06:27,000 --> 00:06:29,000 +which is a very important concept. + +190 +00:06:29,000 --> 00:06:31,000 +Something called "Big O Notation". + +191 +00:06:31,000 --> 00:06:33,000 +But yeah, we'll talk about that in detail later. + +192 +00:06:33,000 --> 00:06:34,000 +I know you're excited to hear that. + +193 +00:06:34,000 --> 00:06:37,000 +But important thing is, when you write a code, + +194 +00:06:37,000 --> 00:06:39,000 +think about the number of steps your code takes + +195 +00:06:39,000 --> 00:06:42,000 +because that defines your time complexity. + +196 +00:06:42,000 --> 00:06:44,000 +And if you say my algorithm is good, + +197 +00:06:44,000 --> 00:06:47,000 +it should be time efficient, okay? + +198 +00:06:47,000 --> 00:06:49,000 +So for the same particular operation, + +199 +00:06:49,000 --> 00:06:50,000 +you can write two different codes. + +200 +00:06:50,000 --> 00:06:52,000 +Example on the screen, if you want to print, + +201 +00:06:52,000 --> 00:06:55,000 +let's say 10 numbers from 1 to 10, + +202 +00:06:55,000 --> 00:06:58,000 +and maybe you just want to print the even numbers, + +203 +00:06:58,000 --> 00:07:00,000 +we have two options there. + +204 +00:07:00,000 --> 00:07:00,000 +Which one is good? + +205 +00:07:00,000 --> 00:07:02,000 +Of course you'll let me know in the comments section. + +206 +00:07:02,000 --> 00:07:04,000 +But the code which is not treading + +207 +00:07:04,000 --> 00:07:06,000 +between all the values is good, right? + +208 +00:07:06,000 --> 00:07:08,000 +So that's how you define how your algorithm is good. + +209 +00:07:08,000 --> 00:07:11,000 +Now we can also expand the array example. + +210 +00:07:11,000 --> 00:07:12,000 +Let's say if you have a sorted array. + +211 +00:07:12,000 --> 00:07:13,000 +So let's say you have an array, + +212 +00:07:13,000 --> 00:07:14,000 +and this is sorted by default. + +213 +00:07:14,000 --> 00:07:16,000 +So every time you insert the element, + +214 +00:07:16,000 --> 00:07:18,000 +it gets sorted by default. + +215 +00:07:18,000 --> 00:07:19,000 +You don't have to mention + +216 +00:07:19,000 --> 00:07:20,000 +where you want to insert this. + +217 +00:07:20,000 --> 00:07:22,000 +Example, Let's say you have a bunch of values here, + +218 +00:07:22,000 --> 00:07:23,000 +all are sorted. + +219 +00:07:23,000 --> 00:07:25,000 +And now you want to insert element, which is 11. + +220 +00:07:25,000 --> 00:07:27,000 +Now if you want to insert 11 there, + +221 +00:07:27,000 --> 00:07:28,000 +where it'll get inserted? + +222 +00:07:28,000 --> 00:07:30,000 +Of course it will search for the location,. + +223 +00:07:30,000 --> 00:07:31,000 +let's say we have a number nine, + +224 +00:07:31,000 --> 00:07:34,000 +it'll get inserted after nine. + +225 +00:07:34,000 --> 00:07:37,000 +Now the question is how do your code knows where's nine? + +226 +00:07:37,000 --> 00:07:39,000 +So you have to basically search, you have to match. + +227 +00:07:39,000 --> 00:07:41,000 +Is it greater than, is it greater than, is it greater than? + +228 +00:07:41,000 --> 00:07:42,000 +And then you have to insert. + +229 +00:07:42,000 --> 00:07:43,000 +So that will take a lot of time, + +230 +00:07:43,000 --> 00:07:45,000 +but what if you're inserting in between? + +231 +00:07:45,000 --> 00:07:46,000 +You have to shift all the elements. + +232 +00:07:46,000 --> 00:07:47,000 +So that's about the array + +233 +00:07:47,000 --> 00:07:50,000 +and a basic introduction of time complexity. + +234 +00:07:50,000 --> 00:07:51,000 +We'll discuss that in detail later. + +235 +00:07:51,000 --> 00:07:53,000 +So I hope you enjoyed the video. + +236 +00:07:53,000 --> 00:07:55,000 +And in the upcoming videos we'll talk about more about + +237 +00:07:55,000 --> 00:07:56,000 +different data structures. + diff --git a/28 - DSA (Optional)/004 Big O Notation, Time Complexity_en.srt b/28 - DSA (Optional)/004 Big O Notation, Time Complexity_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..4cdf0b5ceb4566f684b7f1e26964d67b236f88ad --- /dev/null +++ b/28 - DSA (Optional)/004 Big O Notation, Time Complexity_en.srt @@ -0,0 +1,2140 @@ +1 +00:00:00,000 --> 00:00:03,000 +-: In this video, we'll talk about time complexity. + +2 +00:00:03,000 --> 00:00:04,000 +See, the thing is, in the previous video, + +3 +00:00:04,000 --> 00:00:06,000 +we have talked about algorithms, right? + +4 +00:00:06,000 --> 00:00:09,000 +The thing is, if you want to build an application, + +5 +00:00:09,000 --> 00:00:11,000 +basically, we are trying to solve a problem, right? + +6 +00:00:11,000 --> 00:00:15,000 +And every problem will have multiple solutions, + +7 +00:00:15,000 --> 00:00:18,000 +so the steps which we write to solve that problem, + +8 +00:00:18,000 --> 00:00:20,000 +that's what we call algorithm. + +9 +00:00:20,000 --> 00:00:22,000 +Example, let's say if you want to cook something, + +10 +00:00:22,000 --> 00:00:23,000 +you follow some steps, right? + +11 +00:00:23,000 --> 00:00:26,000 +Now, those steps are your algorithm. + +12 +00:00:26,000 --> 00:00:29,000 +Example, if I want to record a video, we do multiple things. + +13 +00:00:29,000 --> 00:00:33,000 +We prepare the content, we set up this camera, mic, + +14 +00:00:33,000 --> 00:00:36,000 +and then we start recording, then we edit the video, + +15 +00:00:36,000 --> 00:00:38,000 +so there are multiple things involved. + +16 +00:00:38,000 --> 00:00:40,000 +In the same way, if you want to solve any problem, + +17 +00:00:40,000 --> 00:00:43,000 +we have to follow some steps which we call algorithms. + +18 +00:00:43,000 --> 00:00:46,000 +And the thing is, for one particular problem, + +19 +00:00:46,000 --> 00:00:49,000 +we don't have one solution, we have multiple solutions, + +20 +00:00:49,000 --> 00:00:51,000 +and we have to pick the best one. + +21 +00:00:51,000 --> 00:00:53,000 +Now question arise, how will you choose the best one? + +22 +00:00:53,000 --> 00:00:55,000 +So when you say best one, what exactly it means? + +23 +00:00:55,000 --> 00:00:57,000 +So let's say when I run this application, + +24 +00:00:57,000 --> 00:01:00,000 +I want my application to use less memory, + +25 +00:01:00,000 --> 00:01:02,000 +or maybe I want, when I run this application, + +26 +00:01:02,000 --> 00:01:05,000 +I want it should take less time, or maybe both, + +27 +00:01:05,000 --> 00:01:09,000 +and that's why we have this term called algorithm analysis. + +28 +00:01:09,000 --> 00:01:11,000 +So basically you have to analyze your algorithm + +29 +00:01:11,000 --> 00:01:14,000 +so that you can make it more efficient, right? + +30 +00:01:14,000 --> 00:01:15,000 +Of course, as a developer, + +31 +00:01:15,000 --> 00:01:17,000 +first focus is first the production work, + +32 +00:01:17,000 --> 00:01:20,000 +and then you think about optimizing it. + +33 +00:01:20,000 --> 00:01:22,000 +But when you say optimizing, you can optimize in two ways. + +34 +00:01:22,000 --> 00:01:26,000 +One, think about the space complexity, + +35 +00:01:26,000 --> 00:01:28,000 +and second is the time complexity. + +36 +00:01:28,000 --> 00:01:30,000 +Space complexity simply means can we use an algorithm + +37 +00:01:30,000 --> 00:01:32,000 +which will take less memory, + +38 +00:01:32,000 --> 00:01:35,000 +and time complexity means it should take less time. + +39 +00:01:35,000 --> 00:01:37,000 +But there is one more problem here, + +40 +00:01:37,000 --> 00:01:40,000 +and the problem is how will you calculate the time, + +41 +00:01:40,000 --> 00:01:42,000 +or how do you specify the exact time? + +42 +00:01:42,000 --> 00:01:44,000 +Because every machine will have a different output, right? + +43 +00:01:44,000 --> 00:01:46,000 +So if you are testing the algorithm in your machine, + +44 +00:01:46,000 --> 00:01:48,000 +it might give you different time, + +45 +00:01:48,000 --> 00:01:51,000 +and then the moment you test this in your office machine, + +46 +00:01:51,000 --> 00:01:52,000 +it will give you a different time, + +47 +00:01:52,000 --> 00:01:53,000 +so that's not the best thing + +48 +00:01:53,000 --> 00:01:56,000 +to calculate the number of seconds it takes. + +49 +00:01:56,000 --> 00:01:58,000 +But what you can do is there's a way + +50 +00:01:58,000 --> 00:02:00,000 +you can calculate with the help of steps. + +51 +00:02:00,000 --> 00:02:03,000 +But before we go there, let's focus on two algorithms here, + +52 +00:02:03,000 --> 00:02:06,000 +and then this will make much more sense. + +53 +00:02:06,000 --> 00:02:08,000 +So the algorithm which we're talking about now is + +54 +00:02:08,000 --> 00:02:11,000 +the searching an element in a sorted array. + +55 +00:02:11,000 --> 00:02:13,000 +So in the previous video we have talked about array, + +56 +00:02:13,000 --> 00:02:15,000 +and then we also talked about the sorted array. + +57 +00:02:15,000 --> 00:02:17,000 +So in sorted array, by default, + +58 +00:02:17,000 --> 00:02:19,000 +all the elements will be sorted, right? + +59 +00:02:19,000 --> 00:02:22,000 +Now what I would do is I want to search a particular element + +60 +00:02:22,000 --> 00:02:23,000 +from this array. + +61 +00:02:23,000 --> 00:02:25,000 +Now, there are multiple ways of doing it. + +62 +00:02:25,000 --> 00:02:26,000 +Let's talk about the two. + +63 +00:02:26,000 --> 00:02:28,000 +So basically we have an option of linear search, + +64 +00:02:28,000 --> 00:02:32,000 +binary search, or many more, but let's focus on this two. + +65 +00:02:32,000 --> 00:02:34,000 +So what exactly linear search + +66 +00:02:34,000 --> 00:02:36,000 +and binary search is, let's talk about that. + +67 +00:02:36,000 --> 00:02:37,000 +So what is linear search? + +68 +00:02:37,000 --> 00:02:40,000 +So let's say you have an array with you, okay? + +69 +00:02:40,000 --> 00:02:43,000 +So this is your array, and in this you have multiple values, + +70 +00:02:43,000 --> 00:02:46,000 +so let's say I'm going with a few values here. + +71 +00:02:46,000 --> 00:02:49,000 +Let's say five values, and then I'm going to write, + +72 +00:02:49,000 --> 00:02:53,000 +let's say 5, 7, 9, 12, 17. + +73 +00:02:53,000 --> 00:02:55,000 +So we got these five values here, right? + +74 +00:02:55,000 --> 00:02:58,000 +And then I want to search a particular element. + +75 +00:02:58,000 --> 00:02:59,000 +Now, this is a sorted array + +76 +00:02:59,000 --> 00:03:01,000 +because you can see all the elements are sorted. + +77 +00:03:01,000 --> 00:03:03,000 +We got 5, 7, 9, 12, 17, + +78 +00:03:03,000 --> 00:03:04,000 +and I want to search a particular element. + +79 +00:03:04,000 --> 00:03:06,000 +Let's say I want to search 12, okay? + +80 +00:03:06,000 --> 00:03:07,000 +I want to search this element. + +81 +00:03:07,000 --> 00:03:11,000 +So maybe there is a target value which I want to search. + +82 +00:03:11,000 --> 00:03:13,000 +In this case, the target value is 12, + +83 +00:03:13,000 --> 00:03:14,000 +so I want to search 12, right? + +84 +00:03:14,000 --> 00:03:15,000 +And then this is an array. + +85 +00:03:15,000 --> 00:03:19,000 +So what we do in the linear search is you go one by one, + +86 +00:03:19,000 --> 00:03:20,000 +so what you do is, in fact, we have talked + +87 +00:03:20,000 --> 00:03:22,000 +about this in the previous video as well, + +88 +00:03:22,000 --> 00:03:23,000 +so we'll make it a bit faster. + +89 +00:03:23,000 --> 00:03:25,000 +So what you do is whatever your target value is, + +90 +00:03:25,000 --> 00:03:27,000 +you will compare that with the first element. + +91 +00:03:27,000 --> 00:03:29,000 +If it is matching, great, otherwise, + +92 +00:03:29,000 --> 00:03:31,000 +you will move to the next element, + +93 +00:03:31,000 --> 00:03:32,000 +otherwise you'll move to the next element, + +94 +00:03:32,000 --> 00:03:33,000 +otherwise you'll move to the next element, + +95 +00:03:33,000 --> 00:03:35,000 +and I mean, of course at this point, + +96 +00:03:35,000 --> 00:03:36,000 +we don't have to go to the next element, + +97 +00:03:36,000 --> 00:03:39,000 +so here, we basically got the value, + +98 +00:03:39,000 --> 00:03:40,000 +and once you've got the value, + +99 +00:03:40,000 --> 00:03:41,000 +you can return the value, right? + +100 +00:03:41,000 --> 00:03:42,000 +Now there's a problem here. + +101 +00:03:42,000 --> 00:03:46,000 +The problem is what if the element is not there + +102 +00:03:46,000 --> 00:03:46,000 +in the array? + +103 +00:03:46,000 --> 00:03:49,000 +So of course you have to reach till the end, okay? + +104 +00:03:49,000 --> 00:03:50,000 +What could be the best scenario here? + +105 +00:03:50,000 --> 00:03:52,000 +So let's say you are searching for element, + +106 +00:03:52,000 --> 00:03:54,000 +let's say element is five, + +107 +00:03:54,000 --> 00:03:56,000 +and now we know element five is in the start itself. + +108 +00:03:56,000 --> 00:03:59,000 +So when you have the element at the start, + +109 +00:03:59,000 --> 00:04:01,000 +it will take only one step, + +110 +00:04:01,000 --> 00:04:03,000 +but what if you have element at the end? + +111 +00:04:03,000 --> 00:04:05,000 +So it will take the steps depending upon the length + +112 +00:04:05,000 --> 00:04:08,000 +of the array, so let's say if you have five values, + +113 +00:04:08,000 --> 00:04:09,000 +it will take five steps. + +114 +00:04:09,000 --> 00:04:12,000 +When you have 10 values, it will take 10 steps. + +115 +00:04:12,000 --> 00:04:15,000 +When you have 1 million values, okay, that's huge. + +116 +00:04:15,000 --> 00:04:16,000 +Let's talk about thousands, so let's say + +117 +00:04:16,000 --> 00:04:21,000 +when you have 1,000 values, it will take 1,000 steps. + +118 +00:04:22,000 --> 00:04:23,000 +That's tricky, right? + +119 +00:04:23,000 --> 00:04:25,000 +Now, the problem is not it is taking 1,000 steps. + +120 +00:04:25,000 --> 00:04:29,000 +The problem is as your size increases, + +121 +00:04:29,000 --> 00:04:31,000 +it is also increase in the number of steps to search. + +122 +00:04:31,000 --> 00:04:33,000 +Okay, that's one parameter you have to remember. + +123 +00:04:33,000 --> 00:04:35,000 +So that's a theory, right? + +124 +00:04:35,000 --> 00:04:36,000 +Now let's understand that + +125 +00:04:36,000 --> 00:04:37,000 +with the help of a pseudocode here. + +126 +00:04:37,000 --> 00:04:40,000 +So you can see I'm writing a pseudocode, why pseudocode? + +127 +00:04:40,000 --> 00:04:42,000 +Why not some programming language syntax here? + +128 +00:04:42,000 --> 00:04:44,000 +It's because we wanted to make a generic + +129 +00:04:44,000 --> 00:04:47,000 +so that doesn't matter in which language you work, + +130 +00:04:47,000 --> 00:04:49,000 +maybe C, C++, Java, JavaScript, Python, + +131 +00:04:49,000 --> 00:04:51,000 +maybe Global, your choice. + +132 +00:04:51,000 --> 00:04:54,000 +This pseudocode elements remain the same, right? + +133 +00:04:54,000 --> 00:04:56,000 +Now, this can be function, procedure, doesn't matter. + +134 +00:04:56,000 --> 00:04:59,000 +So what I'm doing here is we have a procedure, right? + +135 +00:04:59,000 --> 00:05:00,000 +It can be a function, and the name + +136 +00:05:00,000 --> 00:05:04,000 +of the function here is the LinearSearch, okay? + +137 +00:05:04,000 --> 00:05:07,000 +Now, we are accepting a list of items. + +138 +00:05:07,000 --> 00:05:08,000 +Now, this is your array. + +139 +00:05:08,000 --> 00:05:10,000 +The name of the array is A, okay? + +140 +00:05:10,000 --> 00:05:14,000 +And then you are also passing a target, + +141 +00:05:14,000 --> 00:05:15,000 +what you're searching, so in this case, + +142 +00:05:15,000 --> 00:05:18,000 +we were searching for 12, but it doesn't matter. + +143 +00:05:18,000 --> 00:05:19,000 +So you have an array with multiple values. + +144 +00:05:19,000 --> 00:05:21,000 +It can be one value, 10 values, + +145 +00:05:21,000 --> 00:05:23,000 +1,000 values, doesn't matter, + +146 +00:05:23,000 --> 00:05:25,000 +and then you're also passing a target, + +147 +00:05:25,000 --> 00:05:26,000 +what you want to search. + +148 +00:05:26,000 --> 00:05:28,000 +Now, you will go inside this, + +149 +00:05:28,000 --> 00:05:30,000 +and then for the first thing you will do is + +150 +00:05:30,000 --> 00:05:32,000 +you will want to find the length of the array, + +151 +00:05:32,000 --> 00:05:34,000 +because of course you should know, right? + +152 +00:05:34,000 --> 00:05:35,000 +Till when you have to search. + +153 +00:05:35,000 --> 00:05:38,000 +So you've got a length which is stored in the N, + +154 +00:05:38,000 --> 00:05:41,000 +and then we are going to basically going from zero + +155 +00:05:41,000 --> 00:05:43,000 +to N minus one because when you start with zero, + +156 +00:05:43,000 --> 00:05:45,000 +you have to end with one less, right? + +157 +00:05:45,000 --> 00:05:48,000 +So if you have 10 values, you will go to zero to nine. + +158 +00:05:48,000 --> 00:05:51,000 +So you will go from the first value to the last value, + +159 +00:05:51,000 --> 00:05:53,000 +and then you will search + +160 +00:05:53,000 --> 00:05:56,000 +if the target which you're searching is equal + +161 +00:05:56,000 --> 00:05:58,000 +to the current where you're searching. + +162 +00:05:58,000 --> 00:05:59,000 +So when you're searching for the first element, + +163 +00:05:59,000 --> 00:06:00,000 +that's the first element here. + +164 +00:06:00,000 --> 00:06:02,000 +Target is matching? + +165 +00:06:02,000 --> 00:06:05,000 +If yes, you're good, you can simply return the value, + +166 +00:06:05,000 --> 00:06:08,000 +but what if you're not match, it's not matching? + +167 +00:06:08,000 --> 00:06:10,000 +It will go for the next iteration, next value. + +168 +00:06:10,000 --> 00:06:11,000 +Is it matching? + +169 +00:06:11,000 --> 00:06:13,000 +Yes, good, return. + +170 +00:06:13,000 --> 00:06:15,000 +What if it's not matching? + +171 +00:06:15,000 --> 00:06:16,000 +Next value. + +172 +00:06:16,000 --> 00:06:19,000 +So basically you have to search each value + +173 +00:06:19,000 --> 00:06:20,000 +till you're not finding it. + +174 +00:06:20,000 --> 00:06:21,000 +So that's basically a pseudocode here. + +175 +00:06:21,000 --> 00:06:23,000 +And what if there's no element? + +176 +00:06:23,000 --> 00:06:25,000 +Example, you have five values + +177 +00:06:25,000 --> 00:06:27,000 +and you're searching for a value which is not there, + +178 +00:06:27,000 --> 00:06:29,000 +you can return minus one by specifying, + +179 +00:06:29,000 --> 00:06:31,000 +hey, the value's not there. + +180 +00:06:31,000 --> 00:06:34,000 +This is simple linear search, the code is small, + +181 +00:06:34,000 --> 00:06:36,000 +but then the time complexity, + +182 +00:06:36,000 --> 00:06:39,000 +which is the amount of time it takes, is huge, okay? + +183 +00:06:39,000 --> 00:06:42,000 +Now let's go for the second approach, + +184 +00:06:42,000 --> 00:06:45,000 +which is your binary search. + +185 +00:06:45,000 --> 00:06:47,000 +Now, what happens in binary searches, it's very simple, + +186 +00:06:47,000 --> 00:06:50,000 +basically it's not that simple, but let's say, + +187 +00:06:50,000 --> 00:06:53,000 +so what you do is you have a array, again, a sorted array, + +188 +00:06:53,000 --> 00:06:55,000 +and let's say we have multiple values here. + +189 +00:06:55,000 --> 00:06:57,000 +I'm not sure how many lines I'm going to draw, + +190 +00:06:57,000 --> 00:06:58,000 +so let's count it later. + +191 +00:06:58,000 --> 00:07:00,000 +So let's say we got five here again, + +192 +00:07:00,000 --> 00:07:05,000 +6, 8, 9, 11, 13, 17. + +193 +00:07:05,000 --> 00:07:07,000 +Okay, so I maybe I love odd numbers. + +194 +00:07:07,000 --> 00:07:08,000 +So it doesn't matter what values you have there. + +195 +00:07:08,000 --> 00:07:13,000 +So we got 1, 2, 3, 4, 5, 6, 7, + +196 +00:07:13,000 --> 00:07:14,000 +so we've got seven values, right? + +197 +00:07:14,000 --> 00:07:17,000 +Now, what you do is in binary search, + +198 +00:07:17,000 --> 00:07:19,000 +now since this is sorted, if you're searching + +199 +00:07:19,000 --> 00:07:20,000 +for a particular element, let's say I'm searching + +200 +00:07:20,000 --> 00:07:22,000 +for eight here, okay? + +201 +00:07:22,000 --> 00:07:24,000 +And we know eight is here, we know it, + +202 +00:07:24,000 --> 00:07:27,000 +but computer has no idea where eight is, + +203 +00:07:27,000 --> 00:07:28,000 +so how it is going to search? + +204 +00:07:28,000 --> 00:07:31,000 +Now in linear search, what we were doing is we were jumping + +205 +00:07:31,000 --> 00:07:34,000 +basically from value to value searching, checking this, + +206 +00:07:34,000 --> 00:07:35,000 +checking this, and then look goes on. + +207 +00:07:35,000 --> 00:07:37,000 +So now why this is called binary is + +208 +00:07:37,000 --> 00:07:39,000 +because you divide your array into two parts. + +209 +00:07:39,000 --> 00:07:40,000 +That's the binary is, now how you do it? + +210 +00:07:40,000 --> 00:07:44,000 +So basically what you do is you give a starting point, + +211 +00:07:44,000 --> 00:07:45,000 +and then you give a ending point, + +212 +00:07:45,000 --> 00:07:48,000 +and of course, every element here will have an index value. + +213 +00:07:48,000 --> 00:07:52,000 +So in total here, we've got seven values, right? + +214 +00:07:52,000 --> 00:07:54,000 +Which goes from zero to six. + +215 +00:07:54,000 --> 00:07:57,000 +Now, what you do is you first divide your array + +216 +00:07:57,000 --> 00:07:59,000 +into two parts, and to do that, + +217 +00:07:59,000 --> 00:08:01,000 +we have to find a mid value, okay? + +218 +00:08:01,000 --> 00:08:03,000 +So even before you divide, you find a mid value. + +219 +00:08:03,000 --> 00:08:04,000 +So how do you find a mid value? + +220 +00:08:04,000 --> 00:08:07,000 +So mid value is equal to the starting position + +221 +00:08:07,000 --> 00:08:09,000 +plus ending position divided by two, okay? + +222 +00:08:09,000 --> 00:08:13,000 +Now, this is six divided by two, which is three, okay? + +223 +00:08:13,000 --> 00:08:17,000 +So your mid now is three, and this is your mid, okay? + +224 +00:08:17,000 --> 00:08:20,000 +Now basically you check with the mid, okay? + +225 +00:08:20,000 --> 00:08:21,000 +So you always check with the mid, + +226 +00:08:21,000 --> 00:08:26,000 +is the value which you're searching for, which is here, + +227 +00:08:26,000 --> 00:08:28,000 +is it matching with the mid value, which is nine? + +228 +00:08:28,000 --> 00:08:31,000 +No, it's not matching, but is it less than or greater than? + +229 +00:08:31,000 --> 00:08:33,000 +If the value which you're searching for is less + +230 +00:08:33,000 --> 00:08:35,000 +than the value which you've got as a mid value, + +231 +00:08:35,000 --> 00:08:39,000 +then the values after the mid value is + +232 +00:08:39,000 --> 00:08:41,000 +of no importance, right? + +233 +00:08:41,000 --> 00:08:43,000 +So these values here has no importance. + +234 +00:08:43,000 --> 00:08:44,000 +You can directly skip them. + +235 +00:08:44,000 --> 00:08:47,000 +So you can divide your add into two parts. + +236 +00:08:47,000 --> 00:08:52,000 +So you've got the first section here, which is 5, 6, 8, 9, + +237 +00:08:52,000 --> 00:08:53,000 +and then you've got the second section here, + +238 +00:08:53,000 --> 00:08:55,000 +which is 11, 13, 17. + +239 +00:08:55,000 --> 00:08:59,000 +So what you can do is you can basically skip this part, + +240 +00:08:59,000 --> 00:09:01,000 +or maybe I will just do that with a color. + +241 +00:09:01,000 --> 00:09:04,000 +Now we can skip this part, we don't need this value. + +242 +00:09:04,000 --> 00:09:07,000 +The value which you want to focus on is 5, 6, 8, 9. + +243 +00:09:07,000 --> 00:09:11,000 +So what you do now is you make your mid as the end, okay? + +244 +00:09:11,000 --> 00:09:15,000 +Because we have shifted to the left part of the array. + +245 +00:09:15,000 --> 00:09:17,000 +Now, of course when you shift to the right part + +246 +00:09:17,000 --> 00:09:20,000 +of the array, in case, then you shift your starting value. + +247 +00:09:20,000 --> 00:09:23,000 +Now here, you've got S and E again, + +248 +00:09:23,000 --> 00:09:25,000 +and then what you do is you again check your ending, + +249 +00:09:25,000 --> 00:09:28,000 +you will find your new new mid value, + +250 +00:09:28,000 --> 00:09:30,000 +so again, S plus E divided by two. + +251 +00:09:30,000 --> 00:09:35,000 +Now S is zero, zero plus three is three, + +252 +00:09:35,000 --> 00:09:37,000 +by two, which is one. + +253 +00:09:37,000 --> 00:09:38,000 +Of course you can say 1.5, + +254 +00:09:38,000 --> 00:09:40,000 +but let's say this is one, equal to one, + +255 +00:09:40,000 --> 00:09:43,000 +we can apply a floor function to get the nearest integer. + +256 +00:09:43,000 --> 00:09:44,000 +Okay, so we got one here, right? + +257 +00:09:44,000 --> 00:09:46,000 +So what you do is you check, so this is your mid, + +258 +00:09:46,000 --> 00:09:49,000 +again you check, is it matching with the value? + +259 +00:09:49,000 --> 00:09:53,000 +No, it's not matching, so eight is not matching with six. + +260 +00:09:53,000 --> 00:09:56,000 +Then what you do, then you again divide your array + +261 +00:09:56,000 --> 00:09:58,000 +into two parts, but then which one you have to skip now? + +262 +00:09:58,000 --> 00:10:01,000 +Now we know that the mid is six, + +263 +00:10:01,000 --> 00:10:03,000 +so we have to jump to the next value, + +264 +00:10:03,000 --> 00:10:05,000 +I mean we have to look at the right side value. + +265 +00:10:05,000 --> 00:10:07,000 +So six is less than eight, right? + +266 +00:10:07,000 --> 00:10:09,000 +So you have to look at the right side. + +267 +00:10:09,000 --> 00:10:11,000 +So now what we're gonna do is let's, + +268 +00:10:11,000 --> 00:10:12,000 +we have divided into two parts, right? + +269 +00:10:12,000 --> 00:10:15,000 +So now we can skip this part and focus + +270 +00:10:15,000 --> 00:10:16,000 +on the right side now, + +271 +00:10:16,000 --> 00:10:19,000 +so your starting position has been moved here now. + +272 +00:10:19,000 --> 00:10:22,000 +Now this is your S and you've got E. + +273 +00:10:22,000 --> 00:10:26,000 +So again, you'll find a new mid, which is S plus E divided + +274 +00:10:26,000 --> 00:10:30,000 +by two, which is a one plus three is four. + +275 +00:10:30,000 --> 00:10:32,000 +Four divided by two is equal to two, + +276 +00:10:32,000 --> 00:10:35,000 +so your mid now is eight. + +277 +00:10:35,000 --> 00:10:36,000 +Now is it matching? + +278 +00:10:36,000 --> 00:10:39,000 +Yes, okay, and that's how you got the value. + +279 +00:10:39,000 --> 00:10:40,000 +Now, you might be thinking, + +280 +00:10:40,000 --> 00:10:41,000 +we have done so many steps, right? + +281 +00:10:41,000 --> 00:10:42,000 +Not exactly. + +282 +00:10:42,000 --> 00:10:45,000 +Basically at the start itself, we have removed + +283 +00:10:45,000 --> 00:10:48,000 +half of the array, and which is very important, right? + +284 +00:10:48,000 --> 00:10:50,000 +So let's say if you have a array a size of 1,000. + +285 +00:10:50,000 --> 00:10:53,000 +at the start of the first operation, + +286 +00:10:53,000 --> 00:10:56,000 +you have removed 500 values. + +287 +00:10:56,000 --> 00:10:57,000 +For the next operation when you do it, + +288 +00:10:57,000 --> 00:10:59,000 +you remove 250 values. + +289 +00:10:59,000 --> 00:11:01,000 +So the number of operation which you're doing + +290 +00:11:01,000 --> 00:11:03,000 +with one array search is less, right? + +291 +00:11:03,000 --> 00:11:04,000 +And that's how you know + +292 +00:11:04,000 --> 00:11:06,000 +that this is better than the linear search. + +293 +00:11:06,000 --> 00:11:08,000 +Of course in linear search, + +294 +00:11:08,000 --> 00:11:10,000 +if the value which you're searching for is the first value, + +295 +00:11:10,000 --> 00:11:12,000 +it's very fast, right? + +296 +00:11:12,000 --> 00:11:13,000 +It will take a lot of time for binary search, + +297 +00:11:13,000 --> 00:11:16,000 +but in general, binary search works faster. + +298 +00:11:16,000 --> 00:11:18,000 +But how do you do that? + +299 +00:11:18,000 --> 00:11:19,000 +So with this theory, + +300 +00:11:19,000 --> 00:11:21,000 +let's talk about the practical implementation here. + +301 +00:11:21,000 --> 00:11:24,000 +So let's look at the code here very fast + +302 +00:11:24,000 --> 00:11:26,000 +because we have already done the theory here. + +303 +00:11:26,000 --> 00:11:28,000 +So what you do is you basically write a function, + +304 +00:11:28,000 --> 00:11:30,000 +let's say binary search, + +305 +00:11:30,000 --> 00:11:32,000 +and then you pass a list of values, right? + +306 +00:11:32,000 --> 00:11:34,000 +And then you also pass a target, + +307 +00:11:34,000 --> 00:11:36,000 +same thing we have done in the linear search. + +308 +00:11:36,000 --> 00:11:38,000 +And here, then you go with the left and right. + +309 +00:11:38,000 --> 00:11:40,000 +So basically we have mentioned the start and the end. + +310 +00:11:40,000 --> 00:11:43,000 +Here in this, we are taking left and right. + +311 +00:11:43,000 --> 00:11:45,000 +Left starts with zero, as we mentioned, right? + +312 +00:11:45,000 --> 00:11:47,000 +The starting start with zero. + +313 +00:11:47,000 --> 00:11:49,000 +Ending ends at the last value. + +314 +00:11:49,000 --> 00:11:53,000 +Okay, we have done that, and then you run a loop + +315 +00:11:53,000 --> 00:11:55,000 +till you find the value, of course, + +316 +00:11:55,000 --> 00:11:58,000 +then what you do is you check the mid value. + +317 +00:11:58,000 --> 00:12:00,000 +So this is a mid value, we have done that. + +318 +00:12:00,000 --> 00:12:03,000 +Mid is the starting point, ending point divided by two, + +319 +00:12:03,000 --> 00:12:07,000 +and then you check is the middle matching with the target? + +320 +00:12:07,000 --> 00:12:10,000 +If yes, you are done, you can simply return the value, + +321 +00:12:10,000 --> 00:12:13,000 +otherwise, we have to do something more. + +322 +00:12:13,000 --> 00:12:15,000 +Then you check if the value which you got, + +323 +00:12:15,000 --> 00:12:19,000 +which are mid value, and the target, + +324 +00:12:19,000 --> 00:12:22,000 +if the mid value is less than the target, in this case, + +325 +00:12:22,000 --> 00:12:25,000 +you will focus on the right hand side. + +326 +00:12:25,000 --> 00:12:27,000 +Okay, so basically you have to shift to right hand side. + +327 +00:12:27,000 --> 00:12:30,000 +So your left, your starting point will shift, + +328 +00:12:30,000 --> 00:12:31,000 +and we have discussed that here, right? + +329 +00:12:31,000 --> 00:12:32,000 +Your starting point will shift. + +330 +00:12:32,000 --> 00:12:35,000 +And then what if the mid value is greater + +331 +00:12:35,000 --> 00:12:36,000 +than the target value? + +332 +00:12:36,000 --> 00:12:40,000 +In that case, it will shift to the left position, okay? + +333 +00:12:40,000 --> 00:12:42,000 +So that's what we have doing here. + +334 +00:12:42,000 --> 00:12:43,000 +And then every time we do that, + +335 +00:12:43,000 --> 00:12:46,000 +you run the loop again, you find a new mid value, + +336 +00:12:46,000 --> 00:12:47,000 +and that's what we have done multiple times, right? + +337 +00:12:47,000 --> 00:12:49,000 +We found the mid value three times + +338 +00:12:49,000 --> 00:12:51,000 +in the same way you're going to do here, + +339 +00:12:51,000 --> 00:12:54,000 +and then at the end, you will get a value. + +340 +00:12:54,000 --> 00:12:58,000 +If it's not there, you will simply return minus one + +341 +00:12:58,000 --> 00:12:59,000 +as we have done before. + +342 +00:12:59,000 --> 00:13:01,000 +So basically by doing this, what we're doing is + +343 +00:13:01,000 --> 00:13:02,000 +we have seen two algorithms, right? + +344 +00:13:02,000 --> 00:13:06,000 +Now, we have to find the time complexity. + +345 +00:13:06,000 --> 00:13:08,000 +Now, what exactly time complexity is, it's not about number + +346 +00:13:08,000 --> 00:13:13,000 +of seconds it takes to run your code, it's all about this. + +347 +00:13:13,000 --> 00:13:15,000 +So time complexity simply means the measure + +348 +00:13:15,000 --> 00:13:18,000 +of how the running time of an algorithm increases + +349 +00:13:18,000 --> 00:13:21,000 +with the size of input data. + +350 +00:13:21,000 --> 00:13:23,000 +So basically, let's say when you are testing + +351 +00:13:23,000 --> 00:13:24,000 +a particular algorithm, you're testing + +352 +00:13:24,000 --> 00:13:26,000 +for let's say 10 values or 20 values, + +353 +00:13:26,000 --> 00:13:29,000 +but what if the values goes up? + +354 +00:13:29,000 --> 00:13:30,000 +What if you have 1,000 values, + +355 +00:13:30,000 --> 00:13:33,000 +how will it will affect your time? + +356 +00:13:33,000 --> 00:13:35,000 +Will it increase the time in linear section? + +357 +00:13:35,000 --> 00:13:38,000 +Let's say if it takes five seconds for 10 values, + +358 +00:13:38,000 --> 00:13:40,000 +will it take 10 seconds for 20 values, + +359 +00:13:40,000 --> 00:13:43,000 +or will it take more or will it take less? + +360 +00:13:43,000 --> 00:13:45,000 +Okay, or let's say you have 1 million records, + +361 +00:13:45,000 --> 00:13:49,000 +will it take 1 million seconds? + +362 +00:13:49,000 --> 00:13:51,000 +That's the question we have to answer, okay? + +363 +00:13:51,000 --> 00:13:53,000 +Now, how will you do that? + +364 +00:13:53,000 --> 00:13:58,000 +And that's where we have something called big O notation. + +365 +00:13:58,000 --> 00:14:00,000 +Now what happens in big O notation is we use + +366 +00:14:00,000 --> 00:14:04,000 +this particular concept to understand your time complexity + +367 +00:14:04,000 --> 00:14:05,000 +of your algorithm. + +368 +00:14:05,000 --> 00:14:08,000 +If someone says, "Hey, your algorithm is not fast enough," + +369 +00:14:08,000 --> 00:14:11,000 +you have to first find the big O notation of it. + +370 +00:14:11,000 --> 00:14:14,000 +So why big O is because it is represented + +371 +00:14:14,000 --> 00:14:17,000 +with the help of O, the big O in bracket, + +372 +00:14:17,000 --> 00:14:20,000 +you mention the order of N or something. + +373 +00:14:20,000 --> 00:14:24,000 +So basically, it's also called order of or big O notation, + +374 +00:14:24,000 --> 00:14:27,000 +anything will do, so how do you represent that? + +375 +00:14:27,000 --> 00:14:29,000 +You represent that with something like this. + +376 +00:14:29,000 --> 00:14:31,000 +So we have O of, and then the value + +377 +00:14:31,000 --> 00:14:33,000 +which is one will keep changing. + +378 +00:14:33,000 --> 00:14:34,000 +So there are multiple things here. + +379 +00:14:34,000 --> 00:14:39,000 +You can say we have big O of log N, we got big O of N, + +380 +00:14:39,000 --> 00:14:43,000 +we got big O of N log N, we say big O of N squared, + +381 +00:14:43,000 --> 00:14:45,000 +which is quarterly time. + +382 +00:14:45,000 --> 00:14:47,000 +We got two less to N, we got N factorial. + +383 +00:14:47,000 --> 00:14:51,000 +Okay, so we got all this notation, but how do you find this? + +384 +00:14:51,000 --> 00:14:52,000 +Okay, now this is tricky. + +385 +00:14:52,000 --> 00:14:54,000 +So of course in this particular video, + +386 +00:14:54,000 --> 00:14:56,000 +you will not understand everything, + +387 +00:14:56,000 --> 00:14:57,000 +but let's start with the basic. + +388 +00:14:57,000 --> 00:14:59,000 +Now let's talk about the linear search. + +389 +00:14:59,000 --> 00:15:00,000 +Now, if you go back to linear search, + +390 +00:15:00,000 --> 00:15:02,000 +what we were doing there? + +391 +00:15:02,000 --> 00:15:04,000 +Now, in linear search, + +392 +00:15:04,000 --> 00:15:07,000 +when you have five values, you take five steps. + +393 +00:15:07,000 --> 00:15:09,000 +When you have 10 values, you take 10 steps. + +394 +00:15:09,000 --> 00:15:13,000 +When you have 100 values, you take 100 steps, right? + +395 +00:15:13,000 --> 00:15:15,000 +But let's say that is for searching, right? + +396 +00:15:15,000 --> 00:15:16,000 +What about reading? + +397 +00:15:16,000 --> 00:15:18,000 +If you want to read a particular element + +398 +00:15:18,000 --> 00:15:21,000 +from the linear search or from the array, + +399 +00:15:21,000 --> 00:15:22,000 +it's very easy, right? + +400 +00:15:22,000 --> 00:15:23,000 +You specify the index value. + +401 +00:15:23,000 --> 00:15:27,000 +Let's say the index value is 0, 1, 2, 3, 4, + +402 +00:15:27,000 --> 00:15:31,000 +and then you specify, hey, I want the value at index two. + +403 +00:15:31,000 --> 00:15:34,000 +So let's say you say nums, which is the name of the array, + +404 +00:15:34,000 --> 00:15:37,000 +and when you say two, basically what you get is nine. + +405 +00:15:37,000 --> 00:15:40,000 +It's very fast, so it doesn't matter what is the size + +406 +00:15:40,000 --> 00:15:43,000 +of your array, it can be 1 million records, + +407 +00:15:43,000 --> 00:15:46,000 +the time taken to get a particular element + +408 +00:15:46,000 --> 00:15:49,000 +with the help of index value is constant, right? + +409 +00:15:49,000 --> 00:15:52,000 +Because computer knows where that element is. + +410 +00:15:52,000 --> 00:15:56,000 +So in that case, I can say the big O, the time complexity + +411 +00:15:56,000 --> 00:16:00,000 +for this is one, because it is constant, okay? + +412 +00:16:00,000 --> 00:16:03,000 +Now see, it's not about one step. + +413 +00:16:03,000 --> 00:16:05,000 +If you're thinking just because I mentioned + +414 +00:16:05,000 --> 00:16:08,000 +that it is only one step, it can be three steps as well. + +415 +00:16:08,000 --> 00:16:10,000 +So let's say if you are reading a particular element, + +416 +00:16:10,000 --> 00:16:12,000 +and maybe behind the scene it takes three steps. + +417 +00:16:12,000 --> 00:16:15,000 +It doesn't matter, it's constant, right? + +418 +00:16:15,000 --> 00:16:17,000 +It can be one step, three step, 10 steps, + +419 +00:16:17,000 --> 00:16:18,000 +it should be constant. + +420 +00:16:18,000 --> 00:16:22,000 +If it is constant, you can say O of one. + +421 +00:16:22,000 --> 00:16:24,000 +But what if you are searching an element? + +422 +00:16:24,000 --> 00:16:27,000 +So if you have an array size of let's say 10, + +423 +00:16:27,000 --> 00:16:29,000 +it takes 10 steps. + +424 +00:16:29,000 --> 00:16:30,000 +Now you'll be saying, "Hey, you know, + +425 +00:16:30,000 --> 00:16:32,000 +what if your element is at the start itself?" + +426 +00:16:32,000 --> 00:16:33,000 +Of course it will take one step, + +427 +00:16:33,000 --> 00:16:36,000 +but we have to go for the worst case scenario here. + +428 +00:16:36,000 --> 00:16:39,000 +The worst case is what if the element is at the end? + +429 +00:16:39,000 --> 00:16:41,000 +What if the element is not even there? + +430 +00:16:41,000 --> 00:16:43,000 +It will take 10 steps, + +431 +00:16:43,000 --> 00:16:46,000 +and that's why we can say the order, + +432 +00:16:46,000 --> 00:16:50,000 +the big O notation here will be N, what is N here? + +433 +00:16:50,000 --> 00:16:52,000 +The number of elements in the array. + +434 +00:16:52,000 --> 00:16:56,000 +As it increases, the time also increases, + +435 +00:16:56,000 --> 00:16:57,000 +the time complexity. + +436 +00:16:57,000 --> 00:16:59,000 +I'm not talking about the actual seconds, okay? + +437 +00:16:59,000 --> 00:17:01,000 +So that's your linear search. + +438 +00:17:01,000 --> 00:17:03,000 +Now, coming back to the binary search, + +439 +00:17:03,000 --> 00:17:06,000 +what could be the big O notation for binary search? + +440 +00:17:06,000 --> 00:17:09,000 +See, the thing is as the number increases, + +441 +00:17:09,000 --> 00:17:12,000 +it will not directly increase the number of steps. + +442 +00:17:12,000 --> 00:17:15,000 +Reason for that is, let's say we have seven values. + +443 +00:17:15,000 --> 00:17:16,000 +How many steps it takes? + +444 +00:17:16,000 --> 00:17:18,000 +Let's say three steps, + +445 +00:17:18,000 --> 00:17:20,000 +because we have to find the mid value three times, right? + +446 +00:17:20,000 --> 00:17:22,000 +What if the size increases to double? + +447 +00:17:22,000 --> 00:17:25,000 +So let's say from seven, we got 14, so what do you think? + +448 +00:17:25,000 --> 00:17:27,000 +How many steps will increase? + +449 +00:17:27,000 --> 00:17:29,000 +What if I say only one? + +450 +00:17:29,000 --> 00:17:30,000 +The reason being, even if you have, + +451 +00:17:30,000 --> 00:17:32,000 +let's say 14 values here, let's say we have + +452 +00:17:32,000 --> 00:17:36,000 +so many values, at the start itself, it'll be half, right? + +453 +00:17:36,000 --> 00:17:39,000 +You will get seven values after one step, + +454 +00:17:39,000 --> 00:17:41,000 +and then you basically increase one more step + +455 +00:17:41,000 --> 00:17:45,000 +to find the mid, so that means when your value increases, + +456 +00:17:45,000 --> 00:17:47,000 +it will also increase the number of steps taken, + +457 +00:17:47,000 --> 00:17:50,000 +but not exactly N, because in the linear search, + +458 +00:17:50,000 --> 00:17:52,000 +if your number of values increases by 10, + +459 +00:17:52,000 --> 00:17:54,000 +it will increase 10 steps. + +460 +00:17:54,000 --> 00:17:57,000 +Here, as the number of value increases, + +461 +00:17:57,000 --> 00:18:00,000 +it will not increase the steps in the same sequence. + +462 +00:18:00,000 --> 00:18:03,000 +So what we can say is, we can say it is something + +463 +00:18:03,000 --> 00:18:05,000 +between constant, it's not exactly constant, + +464 +00:18:05,000 --> 00:18:09,000 +but it is between the O of one and O of N, + +465 +00:18:09,000 --> 00:18:11,000 +so it is between this two, + +466 +00:18:11,000 --> 00:18:16,000 +and what comes between this two is O of log N. + +467 +00:18:16,000 --> 00:18:18,000 +Now, what is log N basically? + +468 +00:18:18,000 --> 00:18:23,000 +So log N basically means we have a base two here by default, + +469 +00:18:23,000 --> 00:18:24,000 +okay, so number of steps it takes, + +470 +00:18:24,000 --> 00:18:26,000 +so what exactly this N here? + +471 +00:18:26,000 --> 00:18:28,000 +So let's say we have eight values here. + +472 +00:18:28,000 --> 00:18:30,000 +So when you say log two of eight, + +473 +00:18:30,000 --> 00:18:32,000 +it will give the value which is three. + +474 +00:18:32,000 --> 00:18:34,000 +Okay now, why it is three is because log + +475 +00:18:34,000 --> 00:18:39,000 +of eight simply means it will take two, + +476 +00:18:39,000 --> 00:18:43,000 +multiply itself three times to get eight, okay? + +477 +00:18:43,000 --> 00:18:46,000 +So as the number increases, let's say now instead + +478 +00:18:46,000 --> 00:18:50,000 +of eight values, we got 16 values, it will give you four, + +479 +00:18:50,000 --> 00:18:54,000 +because it will take two to multiply itself four times + +480 +00:18:54,000 --> 00:18:56,000 +to get 16, right? + +481 +00:18:56,000 --> 00:18:57,000 +So it will give you four. + +482 +00:18:57,000 --> 00:19:00,000 +That means as your number of value increases + +483 +00:19:00,000 --> 00:19:04,000 +from eight to 16, it'll only increase one step, right? + +484 +00:19:04,000 --> 00:19:09,000 +And that's why the binary search is log N. + +485 +00:19:09,000 --> 00:19:11,000 +So your linear search is O of N, + +486 +00:19:11,000 --> 00:19:13,000 +and your binary search is O of log N, + +487 +00:19:13,000 --> 00:19:17,000 +so which will take less time, the log of N, okay? + +488 +00:19:17,000 --> 00:19:18,000 +And you can see the graph here as well. + +489 +00:19:18,000 --> 00:19:20,000 +So this is elements, + +490 +00:19:20,000 --> 00:19:22,000 +the number of elements you have in the array, + +491 +00:19:22,000 --> 00:19:24,000 +and this is the number of operation it takes, + +492 +00:19:24,000 --> 00:19:26,000 +okay, so number of steps. + +493 +00:19:26,000 --> 00:19:30,000 +So if your algorithm is following the constant time, + +494 +00:19:30,000 --> 00:19:33,000 +it means it's here. + +495 +00:19:33,000 --> 00:19:35,000 +It doesn't matter how many values you increase, + +496 +00:19:35,000 --> 00:19:36,000 +it will be constant time. + +497 +00:19:36,000 --> 00:19:40,000 +Log N is similar to, it's almost same as constant, + +498 +00:19:40,000 --> 00:19:43,000 +not exactly same as constant, but it will take less time. + +499 +00:19:43,000 --> 00:19:45,000 +So you can see log N is mentioned here. + +500 +00:19:45,000 --> 00:19:46,000 +It's there, the wave is there, + +501 +00:19:46,000 --> 00:19:49,000 +you can't see it, but it's there. + +502 +00:19:49,000 --> 00:19:51,000 +So what if you go with big O of N? + +503 +00:19:51,000 --> 00:19:54,000 +So as your value increases, your time also increases, right? + +504 +00:19:54,000 --> 00:19:57,000 +But there can be some worse scenarios as well. + +505 +00:19:57,000 --> 00:20:00,000 +There can be some other algorithms which you implement, + +506 +00:20:00,000 --> 00:20:03,000 +which might take N log of N, which is worse than O of N. + +507 +00:20:03,000 --> 00:20:05,000 +There might be some algorithm which will take O + +508 +00:20:05,000 --> 00:20:09,000 +of N squared, which is worse than the O N log N. + +509 +00:20:09,000 --> 00:20:13,000 +So basically, if you want to build an algorithm, + +510 +00:20:13,000 --> 00:20:15,000 +try to make sure that you are here. + +511 +00:20:15,000 --> 00:20:18,000 +If you are here, you're good, okay? + +512 +00:20:18,000 --> 00:20:21,000 +If you are here, it's okay. + +513 +00:20:21,000 --> 00:20:24,000 +If you are here, try to make it better. + +514 +00:20:24,000 --> 00:20:26,000 +If you are here, that's the worst scenario. + +515 +00:20:26,000 --> 00:20:27,000 +That means you're building + +516 +00:20:27,000 --> 00:20:28,000 +an application which is not scalable. + +517 +00:20:28,000 --> 00:20:31,000 +The moment you have five users on your server, + +518 +00:20:31,000 --> 00:20:32,000 +your server says, "I'm good." + +519 +00:20:32,000 --> 00:20:37,000 +The moment you have 1,000 users, it will struggle, okay? + +520 +00:20:37,000 --> 00:20:41,000 +So make sure that which algorithm you write, + +521 +00:20:41,000 --> 00:20:42,000 +you follow this. + +522 +00:20:42,000 --> 00:20:44,000 +Of course, in the upcoming videos, + +523 +00:20:44,000 --> 00:20:45,000 +we'll work on some more algorithms. + +524 +00:20:45,000 --> 00:20:47,000 +We'll see which one matches there, + +525 +00:20:47,000 --> 00:20:49,000 +but you got the idea, right? + +526 +00:20:49,000 --> 00:20:52,000 +So the time complexity simply means this definition, + +527 +00:20:52,000 --> 00:20:56,000 +the measure of how the runtime of the algorithm increases + +528 +00:20:56,000 --> 00:20:58,000 +with the size of the input data, + +529 +00:20:58,000 --> 00:21:00,000 +and to represent that, we use big O notation. + +530 +00:21:00,000 --> 00:21:01,000 +So I hope you got some idea. + +531 +00:21:01,000 --> 00:21:02,000 +In the upcoming videos, + +532 +00:21:02,000 --> 00:21:05,000 +we'll focus more on different algorithms + +533 +00:21:05,000 --> 00:21:07,000 +and also find the big O notation for that. + +534 +00:21:07,000 --> 00:21:09,000 +Apart from this, we also have some other notations as well. + +535 +00:21:09,000 --> 00:21:12,000 +We'll discuss those things in the upcoming videos. + diff --git a/28 - DSA (Optional)/005 Linear And Binary Search Example_en.srt b/28 - DSA (Optional)/005 Linear And Binary Search Example_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..c48e0aadb9daa80efab4f00cae4ea74e015e2ed2 --- /dev/null +++ b/28 - DSA (Optional)/005 Linear And Binary Search Example_en.srt @@ -0,0 +1,2164 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: Welcome back, aliens. My name is Navin Reddy. + +2 +00:00:02,000 --> 00:00:04,000 +And in this video we'll talk about + +3 +00:00:04,000 --> 00:00:07,000 +the practical implementation of linear search + +4 +00:00:07,000 --> 00:00:11,000 +and binary search to understand the time complexity, + +5 +00:00:11,000 --> 00:00:13,000 +which we can represent with the help of big O notation. + +6 +00:00:13,000 --> 00:00:16,000 +Now, basically, we'll write two examples here. + +7 +00:00:16,000 --> 00:00:20,000 +In fact, we will also do binary search in two ways + +8 +00:00:20,000 --> 00:00:22,000 +just to understand how thing works + +9 +00:00:22,000 --> 00:00:25,000 +and then how will you find the amount of time it takes, + +10 +00:00:25,000 --> 00:00:27,000 +not in seconds, of course, + +11 +00:00:27,000 --> 00:00:29,000 +but the number of steps, right? + +12 +00:00:29,000 --> 00:00:31,000 +So we have discussed about big O notation, + +13 +00:00:31,000 --> 00:00:34,000 +which is the big O of one, big O of n, + +14 +00:00:34,000 --> 00:00:36,000 +big O of log n, right? + +15 +00:00:36,000 --> 00:00:37,000 +But we also have multiple options. + +16 +00:00:37,000 --> 00:00:39,000 +But then at this point, let's focus only on two + +17 +00:00:39,000 --> 00:00:41,000 +with the help of two algorithms. + +18 +00:00:41,000 --> 00:00:43,000 +Linear search, which is big O of n, + +19 +00:00:43,000 --> 00:00:47,000 +and binary search, which is big O of log n. + +20 +00:00:47,000 --> 00:00:48,000 +Okay, so what I will do + +21 +00:00:48,000 --> 00:00:49,000 +is I will write this code in Java. + +22 +00:00:49,000 --> 00:00:51,000 +And of course, we'll not + +23 +00:00:51,000 --> 00:00:53,000 +be doing very complex stuff in Java. + +24 +00:00:53,000 --> 00:00:56,000 +If you know any of the language, you can follow this. + +25 +00:00:56,000 --> 00:00:57,000 +So I'm using an IDE. + +26 +00:00:57,000 --> 00:00:59,000 +And again, you can use any IDE. + +27 +00:00:59,000 --> 00:01:00,000 +I'm using IntelliJ IDEA. + +28 +00:01:00,000 --> 00:01:02,000 +So in this, I have a project. + +29 +00:01:02,000 --> 00:01:04,000 +I will simply create a file here. + +30 +00:01:04,000 --> 00:01:07,000 +And we'll name this file as Demo. + +31 +00:01:07,000 --> 00:01:09,000 +So we got a file called Demo.java. + +32 +00:01:09,000 --> 00:01:10,000 +And this is where basically + +33 +00:01:10,000 --> 00:01:12,000 +we'll also have our main method. + +34 +00:01:12,000 --> 00:01:14,000 +So if you're coming from different languages, + +35 +00:01:14,000 --> 00:01:16,000 +you know, we also have main there. + +36 +00:01:16,000 --> 00:01:17,000 +So we have a main. + +37 +00:01:17,000 --> 00:01:18,000 +Now, let's start with the actual work. + +38 +00:01:18,000 --> 00:01:22,000 +Imagine this as just a template to run the code. + +39 +00:01:22,000 --> 00:01:25,000 +We'll write our code here. Now, what we want to achieve? + +40 +00:01:25,000 --> 00:01:28,000 +Now, basically, we will be having a list of elements, + +41 +00:01:28,000 --> 00:01:31,000 +and we want to search a element out of it, okay? + +42 +00:01:31,000 --> 00:01:34,000 +Now, the size can be anything, it can be five, it can be 10, + +43 +00:01:34,000 --> 00:01:36,000 +it can be 20, 100, doesn't matter. + +44 +00:01:36,000 --> 00:01:38,000 +Initially, we'll go for less number of values, + +45 +00:01:38,000 --> 00:01:39,000 +and then we'll also see how + +46 +00:01:39,000 --> 00:01:41,000 +do you work with thousands of values + +47 +00:01:41,000 --> 00:01:43,000 +and how do you find element. + +48 +00:01:43,000 --> 00:01:44,000 +And then how much steps it takes + +49 +00:01:44,000 --> 00:01:47,000 +to search a particular element there. + +50 +00:01:47,000 --> 00:01:48,000 +So what I will do is first of all, + +51 +00:01:48,000 --> 00:01:50,000 +I will create a simple array here. + +52 +00:01:50,000 --> 00:01:53,000 +And we'll say this array name as nums. + +53 +00:01:53,000 --> 00:01:54,000 +So let's say we got an array. + +54 +00:01:54,000 --> 00:01:59,000 +And then I will also specify the initial values to it. + +55 +00:01:59,000 --> 00:02:01,000 +And since we are going for a sorted array, + +56 +00:02:01,000 --> 00:02:04,000 +of course, when you talk about the linear search, + +57 +00:02:04,000 --> 00:02:07,000 +we don't have to specify a sorted array. + +58 +00:02:07,000 --> 00:02:10,000 +It can be applicable on any kind of array. + +59 +00:02:10,000 --> 00:02:12,000 +But when it comes to the binary search, + +60 +00:02:12,000 --> 00:02:14,000 +you need to pass a sorted element array + +61 +00:02:14,000 --> 00:02:16,000 +or a list, you can say. + +62 +00:02:16,000 --> 00:02:18,000 +So here, I will simply assign some values. + +63 +00:02:18,000 --> 00:02:20,000 +Let's say we got five values here. + +64 +00:02:20,000 --> 00:02:23,000 +And then I just want to keep it simple with those values. + +65 +00:02:23,000 --> 00:02:25,000 +Of course, you can have any value, it doesn't matter. + +66 +00:02:25,000 --> 00:02:27,000 +I'm just trying to keep it simple. + +67 +00:02:27,000 --> 00:02:28,000 +Or maybe if you want, + +68 +00:02:28,000 --> 00:02:30,000 +I can say seven, + +69 +00:02:31,000 --> 00:02:35,000 +9, 11, 13. + +70 +00:02:35,000 --> 00:02:37,000 +That's my love for odd numbers, okay? + +71 +00:02:37,000 --> 00:02:39,000 +And I want to search a value. + +72 +00:02:39,000 --> 00:02:40,000 +So whatever value I want to search, + +73 +00:02:40,000 --> 00:02:42,000 +we can also specify here. + +74 +00:02:42,000 --> 00:02:43,000 +Now, we can use any variable name, + +75 +00:02:43,000 --> 00:02:46,000 +but we have already used target as a variable, right, + +76 +00:02:46,000 --> 00:02:47,000 +when we're discussing the theory. + +77 +00:02:47,000 --> 00:02:50,000 +So we'll set target equal to, and we'll mention the target. + +78 +00:02:50,000 --> 00:02:53,000 +Let's say I want to search an element which is 11, okay? + +79 +00:02:53,000 --> 00:02:56,000 +So this is what I want to search from this particular list. + +80 +00:02:56,000 --> 00:02:58,000 +We can do this with multiple algorithms. + +81 +00:02:58,000 --> 00:03:00,000 +We can use linear search, binary search, + +82 +00:03:00,000 --> 00:03:01,000 +or whatever you think about. + +83 +00:03:01,000 --> 00:03:03,000 +But let's go for linear search initially, + +84 +00:03:03,000 --> 00:03:04,000 +and then we'll look at the binary search. + +85 +00:03:04,000 --> 00:03:05,000 +Now, how do we do that? + +86 +00:03:05,000 --> 00:03:07,000 +So what we can do is we can say, + +87 +00:03:07,000 --> 00:03:09,000 +I want to get the result, okay? + +88 +00:03:09,000 --> 00:03:13,000 +I want to find this 11 in this list + +89 +00:03:13,000 --> 00:03:14,000 +and give me the index number. + +90 +00:03:14,000 --> 00:03:15,000 +I don't want anything else, + +91 +00:03:15,000 --> 00:03:18,000 +just the index number where it belongs to, okay? + +92 +00:03:18,000 --> 00:03:19,000 +So who will give me that? + +93 +00:03:19,000 --> 00:03:21,000 +So what I can do is I can assign this target + +94 +00:03:21,000 --> 00:03:23,000 +to someone else, let's say linearSearch. + +95 +00:03:23,000 --> 00:03:25,000 +Now, this is another method + +96 +00:03:25,000 --> 00:03:27,000 +which will give the value to us. + +97 +00:03:27,000 --> 00:03:29,000 +So I don't want to populate everything + +98 +00:03:29,000 --> 00:03:30,000 +in this particular main. + +99 +00:03:30,000 --> 00:03:32,000 +Main will simply say, "Hey, you know, + +100 +00:03:32,000 --> 00:03:34,000 +I got this two things, I got the list, + +101 +00:03:34,000 --> 00:03:36,000 +I got the target value. + +102 +00:03:36,000 --> 00:03:39,000 +You tell me which index this value belongs to, + +103 +00:03:39,000 --> 00:03:41,000 +or if you don't have this value in the list, + +104 +00:03:41,000 --> 00:03:43,000 +also let me know." + +105 +00:03:43,000 --> 00:03:46,000 +So linearSearch will say, "Okay, accepted the challenge." + +106 +00:03:46,000 --> 00:03:47,000 +It will accept two things. + +107 +00:03:47,000 --> 00:03:49,000 +First, nums and target. + +108 +00:03:49,000 --> 00:03:50,000 +Why you have to pass nums? + +109 +00:03:50,000 --> 00:03:52,000 +Of course, linearSearch will say, + +110 +00:03:52,000 --> 00:03:54,000 +"I will search, but tell me where to search." + +111 +00:03:54,000 --> 00:03:55,000 +And target will say, + +112 +00:03:55,000 --> 00:03:57,000 +"This is a value you want to search." + +113 +00:03:57,000 --> 00:03:59,000 +Now, the thing is we don't have this method yet. + +114 +00:03:59,000 --> 00:04:01,000 +So what I will do is I will just + +115 +00:04:01,000 --> 00:04:03,000 +use my IDE to generate. + +116 +00:04:03,000 --> 00:04:07,000 +Okay, so I will say More actions, Create this method. + +117 +00:04:07,000 --> 00:04:08,000 +And you can see we got this method. + +118 +00:04:08,000 --> 00:04:11,000 +I don't want this to be private, I want this to be public. + +119 +00:04:11,000 --> 00:04:11,000 +And that's it! + +120 +00:04:11,000 --> 00:04:13,000 +What you're doing is you are calling another method. + +121 +00:04:13,000 --> 00:04:16,000 +At this point, since we are returning a value, + +122 +00:04:17,000 --> 00:04:20,000 +what we can say, we can say return -1. + +123 +00:04:20,000 --> 00:04:22,000 +Okay, nothing fancy, I just want this to work, + +124 +00:04:22,000 --> 00:04:24,000 +and that's why I'm returning -1, okay? + +125 +00:04:24,000 --> 00:04:26,000 +And what if you get the result? + +126 +00:04:26,000 --> 00:04:27,000 +Of course, if there's no, + +127 +00:04:27,000 --> 00:04:29,000 +if this value is not there in the list, + +128 +00:04:29,000 --> 00:04:31,000 +it will return -1. + +129 +00:04:31,000 --> 00:04:33,000 +Otherwise, we can return the index value. + +130 +00:04:33,000 --> 00:04:35,000 +So whatever value you get, + +131 +00:04:35,000 --> 00:04:36,000 +you can simply print here + +132 +00:04:36,000 --> 00:04:41,000 +and you can say, "Element found at Index," + +133 +00:04:41,000 --> 00:04:43,000 +and then you can specify the index value here. + +134 +00:04:43,000 --> 00:04:44,000 +So let me just give a space. + +135 +00:04:44,000 --> 00:04:46,000 +So I can specify the result here. + +136 +00:04:46,000 --> 00:04:48,000 +So whatever index is there, okay? + +137 +00:04:48,000 --> 00:04:50,000 +And let's run this code just to see. + +138 +00:04:50,000 --> 00:04:52,000 +I just want to make sure that everything is working. + +139 +00:04:52,000 --> 00:04:53,000 +Let me just run this code. + +140 +00:04:53,000 --> 00:04:56,000 +And you can see we got the answer. + +141 +00:04:56,000 --> 00:04:59,000 +And also, I want to move this to right-hand side, + +142 +00:05:00,000 --> 00:05:02,000 +Okay, so it looks good. + +143 +00:05:02,000 --> 00:05:03,000 +So you can see the output says, + +144 +00:05:03,000 --> 00:05:07,000 +"Element found at Index : -1," that's weird. + +145 +00:05:07,000 --> 00:05:09,000 +It's just that we are returning -1. + +146 +00:05:09,000 --> 00:05:11,000 +That's why it's printing -1. + +147 +00:05:11,000 --> 00:05:13,000 +What we should be doing is we should be checking + +148 +00:05:13,000 --> 00:05:17,000 +if the result != 1 + +149 +00:05:19,000 --> 00:05:21,000 +or != -1. + +150 +00:05:21,000 --> 00:05:22,000 +So this statement, it should print only + +151 +00:05:22,000 --> 00:05:25,000 +when the result != -1. + +152 +00:05:25,000 --> 00:05:29,000 +In case it's -1, that means the element was not there. + +153 +00:05:29,000 --> 00:05:34,000 +So I can simply print, "Element not found," okay? + +154 +00:05:34,000 --> 00:05:35,000 +And I found this code, + +155 +00:05:35,000 --> 00:05:36,000 +it will print, "Element not found." + +156 +00:05:36,000 --> 00:05:39,000 +Because we're not even searching, right? + +157 +00:05:39,000 --> 00:05:40,000 +But we'll search now. + +158 +00:05:40,000 --> 00:05:42,000 +So how do you search? It's very simple. + +159 +00:05:42,000 --> 00:05:45,000 +So what you can do is you can use a simple loop here. + +160 +00:05:45,000 --> 00:05:47,000 +Okay, nothing fancy again, simple loop. + +161 +00:05:47,000 --> 00:05:48,000 +And in this, you're going to iterate. + +162 +00:05:48,000 --> 00:05:50,000 +So we have seen the linear search, right? + +163 +00:05:50,000 --> 00:05:52,000 +So familiar theory, we have talked about this. + +164 +00:05:52,000 --> 00:05:55,000 +So basically, we'll search from the first element, right? + +165 +00:05:55,000 --> 00:05:56,000 +So we're going to start from five + +166 +00:05:56,000 --> 00:05:58,000 +and then will look for is it matching? + +167 +00:05:58,000 --> 00:05:59,000 +Is it matching? Is it matching? + +168 +00:05:59,000 --> 00:06:01,000 +Of course, the values are different, + +169 +00:06:01,000 --> 00:06:03,000 +but we'll basically search for each element. + +170 +00:06:03,000 --> 00:06:05,000 +In the same way, in linear search, + +171 +00:06:05,000 --> 00:06:07,000 +if you want to move, you have to use a loop. + +172 +00:06:07,000 --> 00:06:09,000 +So it will start from zero + +173 +00:06:09,000 --> 00:06:11,000 +and will go 'til the nums.length. + +174 +00:06:11,000 --> 00:06:13,000 +So basically, if the value, + +175 +00:06:13,000 --> 00:06:16,000 +number of values, you have five, you have to reach four. + +176 +00:06:16,000 --> 00:06:18,000 +Okay, so that's why we are saying less than. + +177 +00:06:18,000 --> 00:06:20,000 +And then i++. That's your for loop, right? + +178 +00:06:20,000 --> 00:06:22,000 +So in this for loop, you don't have to do much. + +179 +00:06:22,000 --> 00:06:24,000 +You simply have to check. + +180 +00:06:24,000 --> 00:06:26,000 +So whatever element you got, the recent, + +181 +00:06:26,000 --> 00:06:28,000 +the current element which you're focusing on. + +182 +00:06:28,000 --> 00:06:30,000 +So in the list, we have five values. + +183 +00:06:30,000 --> 00:06:31,000 +If you're focusing on five, + +184 +00:06:31,000 --> 00:06:34,000 +we'll check if five, which is the first element, + +185 +00:06:34,000 --> 00:06:36,000 +is it matching with the target? + +186 +00:06:36,000 --> 00:06:38,000 +If it is matching with the target, our job is done. + +187 +00:06:38,000 --> 00:06:41,000 +We can simply return the index, the current index. + +188 +00:06:41,000 --> 00:06:43,000 +So let's say if you're searching for five, + +189 +00:06:43,000 --> 00:06:45,000 +and then you got five, return zero. + +190 +00:06:45,000 --> 00:06:48,000 +Your job is done. That's what you're doing here, okay? + +191 +00:06:48,000 --> 00:06:52,000 +And that's it, okay? It should work. + +192 +00:06:52,000 --> 00:06:53,000 +That's what we're expecting. + +193 +00:06:53,000 --> 00:06:54,000 +Linear search is so simple. + +194 +00:06:54,000 --> 00:06:55,000 +If you run this code, it says, + +195 +00:06:55,000 --> 00:06:57,000 +"The element found at Index : 3." + +196 +00:06:57,000 --> 00:07:02,000 +That's right, so this is zero, one, two, three. + +197 +00:07:02,000 --> 00:07:05,000 +And our job is done, okay? + +198 +00:07:05,000 --> 00:07:07,000 +What if you're searching for, let's say, five? + +199 +00:07:07,000 --> 00:07:10,000 +In this case, it will print at index zero, + +200 +00:07:10,000 --> 00:07:11,000 +and that's what we got. + +201 +00:07:11,000 --> 00:07:13,000 +So this is working, but what if you're searching + +202 +00:07:13,000 --> 00:07:15,000 +for something, let's say 77, + +203 +00:07:15,000 --> 00:07:17,000 +which is not there in the list. + +204 +00:07:17,000 --> 00:07:18,000 +You will run this code. + +205 +00:07:18,000 --> 00:07:19,000 +It will say, "Element not found." + +206 +00:07:19,000 --> 00:07:21,000 +So linear search is working, right? + +207 +00:07:21,000 --> 00:07:22,000 +Now, we can do the same thing + +208 +00:07:22,000 --> 00:07:24,000 +with the help of binary search. + +209 +00:07:24,000 --> 00:07:25,000 +Now, let's see how binary search works. + +210 +00:07:25,000 --> 00:07:28,000 +So what I will do is I will just write a code here, + +211 +00:07:28,000 --> 00:07:30,000 +which is public... + +212 +00:07:30,000 --> 00:07:31,000 +In fact, I will just copy paste the code, + +213 +00:07:31,000 --> 00:07:34,000 +not a copy pasting, but reusing the code. + +214 +00:07:34,000 --> 00:07:36,000 +I can simply copy this code and paste it here. + +215 +00:07:36,000 --> 00:07:37,000 +Now, if you're coming from Java, + +216 +00:07:37,000 --> 00:07:39,000 +and if you're getting confused + +217 +00:07:39,000 --> 00:07:40,000 +why I'm getting a static method, + +218 +00:07:40,000 --> 00:07:41,000 +why not non-static methods? + +219 +00:07:41,000 --> 00:07:44,000 +It's because I don't want to deal with objects here, okay? + +220 +00:07:44,000 --> 00:07:45,000 +I just want to keep it simple. + +221 +00:07:45,000 --> 00:07:48,000 +So we got a static method and a binarySearch. + +222 +00:07:48,000 --> 00:07:49,000 +The thing is only method name will change. + +223 +00:07:49,000 --> 00:07:51,000 +The parameter will remain same. + +224 +00:07:51,000 --> 00:07:53,000 +In binarySearch also, you pass a list + +225 +00:07:53,000 --> 00:07:55,000 +and you pass a target. + +226 +00:07:55,000 --> 00:07:57,000 +The algorithm will change. + +227 +00:07:57,000 --> 00:07:59,000 +Of course, it will return -1. + +228 +00:07:59,000 --> 00:08:01,000 +That is not something which is changing. + +229 +00:08:01,000 --> 00:08:03,000 +But what we will do inside this? + +230 +00:08:03,000 --> 00:08:05,000 +Now, if you go to the board here, + +231 +00:08:05,000 --> 00:08:07,000 +what we have done is if you have a list of values, + +232 +00:08:07,000 --> 00:08:09,000 +if you can see, we have a list of values here, + +233 +00:08:09,000 --> 00:08:12,000 +now, initially, you have to specify the starting point, + +234 +00:08:12,000 --> 00:08:13,000 +and then you have to specify the ending point + +235 +00:08:13,000 --> 00:08:14,000 +as well, right? + +236 +00:08:14,000 --> 00:08:16,000 +So starting and ending you have to specify. + +237 +00:08:16,000 --> 00:08:20,000 +In the example, we'll say left and right, okay? + +238 +00:08:20,000 --> 00:08:21,000 +Because that's how we, + +239 +00:08:21,000 --> 00:08:23,000 +that makes much more sense, right, left and right? + +240 +00:08:23,000 --> 00:08:26,000 +Starting point is left, ending point is right. + +241 +00:08:26,000 --> 00:08:27,000 +And then you will find a mid. + +242 +00:08:27,000 --> 00:08:29,000 +Every time you find a mid, + +243 +00:08:29,000 --> 00:08:30,000 +if it is matching, that's great. + +244 +00:08:30,000 --> 00:08:33,000 +Otherwise, you have to basically + +245 +00:08:33,000 --> 00:08:34,000 +skip the number of elements, + +246 +00:08:34,000 --> 00:08:36,000 +which is in the other section, okay? + +247 +00:08:36,000 --> 00:08:38,000 +So how do we do that here? + +248 +00:08:38,000 --> 00:08:40,000 +So what I can simply do here is, in fact, + +249 +00:08:40,000 --> 00:08:42,000 +I will just, for my reference, + +250 +00:08:42,000 --> 00:08:43,000 +I will also have those values here. + +251 +00:08:43,000 --> 00:08:46,000 +So I will say 5, 7, 9, 11, + +252 +00:08:46,000 --> 00:08:48,000 +and 13, okay? + +253 +00:08:48,000 --> 00:08:50,000 +And the value, let's say, which I'm searching for this time, + +254 +00:08:50,000 --> 00:08:51,000 +let's have the value. + +255 +00:08:51,000 --> 00:08:53,000 +So let's say I'm searching for 11, okay? + +256 +00:08:53,000 --> 00:08:56,000 +So what I will do is when you're searching for 11, + +257 +00:08:56,000 --> 00:08:58,000 +first of all, you have to specify the starting point. + +258 +00:08:58,000 --> 00:09:00,000 +So I will say left, + +259 +00:09:00,000 --> 00:09:03,000 +in fact, left, int left is equal to. + +260 +00:09:03,000 --> 00:09:06,000 +Now, left is a starting point, right, which is zero. + +261 +00:09:06,000 --> 00:09:09,000 +Then int right, which is the ending point, + +262 +00:09:09,000 --> 00:09:13,000 +which could be four, but then why do hard-coded? + +263 +00:09:13,000 --> 00:09:16,000 +What you can do is you can say nums.length - 1 + +264 +00:09:16,000 --> 00:09:19,000 +because nums.length will give you five. + +265 +00:09:19,000 --> 00:09:20,000 +You have to say -1, which will give you four. + +266 +00:09:20,000 --> 00:09:22,000 +So now we've got the starting point, ending point, + +267 +00:09:22,000 --> 00:09:24,000 +and then our journey begins, right? + +268 +00:09:24,000 --> 00:09:26,000 +So what are we going to do here? + +269 +00:09:26,000 --> 00:09:27,000 +So we'll use a while loop here. + +270 +00:09:27,000 --> 00:09:29,000 +Now, inside this while loop, + +271 +00:09:29,000 --> 00:09:31,000 +what I'm going to do is I'm going to check + +272 +00:09:31,000 --> 00:09:35,000 +if my left <= right. + +273 +00:09:35,000 --> 00:09:37,000 +We'll check if left is less than right + +274 +00:09:37,000 --> 00:09:38,000 +because that's what we want, right? + +275 +00:09:38,000 --> 00:09:39,000 +Left should be on the left-hand side, + +276 +00:09:39,000 --> 00:09:42,000 +which is the smaller values. + +277 +00:09:42,000 --> 00:09:45,000 +And then here, what we'll do is we'll find the mid, okay? + +278 +00:09:45,000 --> 00:09:46,000 +That's the journey we're going to start. + +279 +00:09:46,000 --> 00:09:49,000 +So mid is basically your starting point, right, + +280 +00:09:49,000 --> 00:09:51,000 +which is left + right. + +281 +00:09:51,000 --> 00:09:53,000 +And we want this to be in the bracket, + +282 +00:09:53,000 --> 00:09:56,000 +so that we can divide, so (left + right)/2. + +283 +00:09:57,000 --> 00:09:59,000 +Okay, that's how you find the mid, right? + +284 +00:09:59,000 --> 00:10:01,000 +Now, once you've got your mid, + +285 +00:10:01,000 --> 00:10:03,000 +you can basically check if the nums[mid], + +286 +00:10:05,000 --> 00:10:07,000 +is it matching with target? + +287 +00:10:07,000 --> 00:10:08,000 +If it is matching, that's great. + +288 +00:10:08,000 --> 00:10:11,000 +We can simply return the index, which is mid. + +289 +00:10:11,000 --> 00:10:14,000 +Of course, right? Mid is representing our index here. + +290 +00:10:14,000 --> 00:10:16,000 +If it is not matching, + +291 +00:10:16,000 --> 00:10:18,000 +then we have to change the value of left + +292 +00:10:18,000 --> 00:10:21,000 +or right depend upon what you got in the mid. + +293 +00:10:21,000 --> 00:10:24,000 +So what we'll do is we'll say if, else if. + +294 +00:10:24,000 --> 00:10:25,000 +Now, how do you check? + +295 +00:10:25,000 --> 00:10:29,000 +So we can check if nums[mid] < target. + +296 +00:10:29,000 --> 00:10:31,000 +So let me take this up as well. + +297 +00:10:31,000 --> 00:10:33,000 +Yeah, so if nums[mid] < target, + +298 +00:10:33,000 --> 00:10:36,000 +so let's say when you talk about these elements, + +299 +00:10:36,000 --> 00:10:38,000 +and then we are searching for 11, right? + +300 +00:10:38,000 --> 00:10:41,000 +The mid which we got is nine. + +301 +00:10:41,000 --> 00:10:44,000 +If the 9 is less than the 11, + +302 +00:10:44,000 --> 00:10:46,000 +we have to shift our left, right? + +303 +00:10:46,000 --> 00:10:48,000 +So this left, which was positioning five + +304 +00:10:48,000 --> 00:10:49,000 +will shift here now. + +305 +00:10:49,000 --> 00:10:51,000 +I mean, not even nine. + +306 +00:10:51,000 --> 00:10:53,000 +We don't have to even consider nine, right? + +307 +00:10:53,000 --> 00:10:54,000 +So what we can do is we can say left + +308 +00:10:54,000 --> 00:10:57,000 +will shift to mid + 1, okay? + +309 +00:10:57,000 --> 00:10:59,000 +So basically, now we have two values, 11 and 13, + +310 +00:10:59,000 --> 00:11:02,000 +which we are going to focus in the next equation. + +311 +00:11:02,000 --> 00:11:04,000 +But what if you're searching for something else, + +312 +00:11:04,000 --> 00:11:06,000 +which is in this side? + +313 +00:11:06,000 --> 00:11:08,000 +Let's say we were searching for seven. + +314 +00:11:08,000 --> 00:11:11,000 +In this case, we have to shift our right. + +315 +00:11:11,000 --> 00:11:13,000 +So right = mid - 1. + +316 +00:11:13,000 --> 00:11:16,000 +So basically, let's say if you're searching for seven here, + +317 +00:11:16,000 --> 00:11:18,000 +we've got a mid as nine, + +318 +00:11:18,000 --> 00:11:18,000 +what you will do is you will say, + +319 +00:11:18,000 --> 00:11:21,000 +"Okay, so the value is less than the mid, + +320 +00:11:21,000 --> 00:11:23,000 +so we have to, you know, we have to skip these two." + +321 +00:11:23,000 --> 00:11:25,000 +The focus will be only on five and seven. + +322 +00:11:25,000 --> 00:11:27,000 +That's what we have done here + +323 +00:11:27,000 --> 00:11:29,000 +if you're searching for seven, okay? + +324 +00:11:29,000 --> 00:11:32,000 +I think this looks cool, our job is done. + +325 +00:11:32,000 --> 00:11:35,000 +Let's verify if things are working out. + +326 +00:11:35,000 --> 00:11:36,000 +The only thing that we do is instead + +327 +00:11:36,000 --> 00:11:39,000 +of searching for a linear search this time, + +328 +00:11:39,000 --> 00:11:41,000 +we'll go for a binary search. + +329 +00:11:41,000 --> 00:11:43,000 +Let's run, it works! + +330 +00:11:43,000 --> 00:11:46,000 +You can see it says, "Found, Index : 3." + +331 +00:11:46,000 --> 00:11:48,000 +And what if you are searching for, + +332 +00:11:48,000 --> 00:11:50,000 +let's say 17, which is not there? + +333 +00:11:50,000 --> 00:11:51,000 +Run, it says not found. + +334 +00:11:51,000 --> 00:11:54,000 +Let's search for five, and it says it works. + +335 +00:11:54,000 --> 00:11:56,000 +You can see, "Index : 0." + +336 +00:11:56,000 --> 00:11:57,000 +So that means you can use linearSearch + +337 +00:11:57,000 --> 00:12:00,000 +or binarySearch, both works. + +338 +00:12:00,000 --> 00:12:02,000 +But what if you want to really + +339 +00:12:02,000 --> 00:12:04,000 +see how many steps it is getting? + +340 +00:12:04,000 --> 00:12:05,000 +So what I will do is let me just + +341 +00:12:05,000 --> 00:12:09,000 +say this is result2, which is binarySearch. + +342 +00:12:09,000 --> 00:12:11,000 +And let me also say result1. + +343 +00:12:11,000 --> 00:12:13,000 +Of course, I'm not going to use both the things. + +344 +00:12:13,000 --> 00:12:15,000 +I just want to call both the methods, okay? + +345 +00:12:15,000 --> 00:12:17,000 +I'm just going to use result1 + +346 +00:12:17,000 --> 00:12:19,000 +because if linearSearch is able to find it, + +347 +00:12:19,000 --> 00:12:21,000 +binarySearch will also able to find it. + +348 +00:12:21,000 --> 00:12:22,000 +Okay, let's print result1 here. + +349 +00:12:22,000 --> 00:12:24,000 +So I just want to call both the methods. + +350 +00:12:24,000 --> 00:12:26,000 +That's the reason I'm using result2. + +351 +00:12:26,000 --> 00:12:28,000 +But result2, we're not using it anywhere else. + +352 +00:12:28,000 --> 00:12:30,000 +Okay, so let's do this with both the algorithms, + +353 +00:12:30,000 --> 00:12:32,000 +and you can see both will print the same thing. + +354 +00:12:32,000 --> 00:12:34,000 +Oh, we're not printing the second part. + +355 +00:12:34,000 --> 00:12:35,000 +Okay, doesn't matter. + +356 +00:12:35,000 --> 00:12:37,000 +Both will give the same result. + +357 +00:12:37,000 --> 00:12:40,000 +But what I'm really concerned about is the number of steps. + +358 +00:12:40,000 --> 00:12:41,000 +So what I'm going to do here + +359 +00:12:41,000 --> 00:12:44,000 +is let's calculate the number of steps it takes. + +360 +00:12:44,000 --> 00:12:48,000 +So I will say for linearSearch, steps = 0 initially. + +361 +00:12:48,000 --> 00:12:51,000 +And every time the loop it reads, + +362 +00:12:51,000 --> 00:12:53,000 +okay, I will simply say steps, + +363 +00:12:53,000 --> 00:12:55,000 +number of steps taken, so steps++. + +364 +00:12:55,000 --> 00:12:57,000 +And then I'm going to print. + +365 +00:12:57,000 --> 00:13:00,000 +So just before return, I'm going to print, + +366 +00:13:00,000 --> 00:13:05,000 +"Steps taken by Linear :," + +367 +00:13:06,000 --> 00:13:08,000 +and I will print steps. + +368 +00:13:08,000 --> 00:13:11,000 +The same thing I want to print here as well. + +369 +00:13:11,000 --> 00:13:13,000 +What if there's no element found? + +370 +00:13:13,000 --> 00:13:15,000 +So this will not, never be executed, right? + +371 +00:13:15,000 --> 00:13:19,000 +Okay, we have to put this into a curly brackets, cool. + +372 +00:13:19,000 --> 00:13:21,000 +So we're printing the steps here. + +373 +00:13:21,000 --> 00:13:22,000 +The same thing I think we should do + +374 +00:13:22,000 --> 00:13:24,000 +for binarySearch as well. + +375 +00:13:24,000 --> 00:13:29,000 +So here, I will say int steps = 0. + +376 +00:13:29,000 --> 00:13:32,000 +And every time you iterate in this loop, + +377 +00:13:32,000 --> 00:13:35,000 +you will say steps++. + +378 +00:13:35,000 --> 00:13:37,000 +And then basically here, + +379 +00:13:37,000 --> 00:13:38,000 +you'll print the same statement. + +380 +00:13:38,000 --> 00:13:40,000 +So I will just copy this. + +381 +00:13:40,000 --> 00:13:41,000 +So we'll print + +382 +00:13:42,000 --> 00:13:43,000 +before returning the value, + +383 +00:13:44,000 --> 00:13:46,000 +but here, we'll say, "Binary." + +384 +00:13:47,000 --> 00:13:50,000 +And we'll also print this before returning, + +385 +00:13:50,000 --> 00:13:52,000 +just to cover all the cases. + +386 +00:13:52,000 --> 00:13:54,000 +Okay, let's see what it does. + +387 +00:13:54,000 --> 00:13:55,000 +Run this code. + +388 +00:13:55,000 --> 00:13:57,000 +And you can see for five elements, + +389 +00:13:57,000 --> 00:13:59,000 +linearSearch is searching in one. + +390 +00:13:59,000 --> 00:14:01,000 +Okay, reason being we're searching for five, right? + +391 +00:14:01,000 --> 00:14:02,000 +I think we're searching for five. + +392 +00:14:02,000 --> 00:14:05,000 +Let's search for 11, okay? + +393 +00:14:05,000 --> 00:14:07,000 +In some cases, linearSearch works well. + +394 +00:14:07,000 --> 00:14:11,000 +Run, and you can see the linearSearch takes four steps. + +395 +00:14:11,000 --> 00:14:14,000 +The binarySearch takes two steps. That's crazy. + +396 +00:14:14,000 --> 00:14:16,000 +Run again. Of course, you will get the same output. + +397 +00:14:16,000 --> 00:14:18,000 +But what if the values are higher? + +398 +00:14:18,000 --> 00:14:21,000 +Let's say I have more values here, + +399 +00:14:21,000 --> 00:14:26,000 +let me add 10, let me add 1, 2, 3. + +400 +00:14:26,000 --> 00:14:27,000 +So now we have more values, right? + +401 +00:14:27,000 --> 00:14:28,000 +I don't know how many values we have, + +402 +00:14:28,000 --> 00:14:29,000 +but let's say if I run this code, + +403 +00:14:29,000 --> 00:14:31,000 +the linearSearch takes eight steps, + +404 +00:14:31,000 --> 00:14:34,000 +binarySearch takes three steps, okay? + +405 +00:14:34,000 --> 00:14:35,000 +But what if you have 1,000 values? + +406 +00:14:35,000 --> 00:14:37,000 +Now, how do you check 1,000 values? + +407 +00:14:37,000 --> 00:14:38,000 +Now, that will be tricky. + +408 +00:14:38,000 --> 00:14:39,000 +So what I'm gonna do + +409 +00:14:39,000 --> 00:14:42,000 +is let's add some random values here. + +410 +00:14:42,000 --> 00:14:45,000 +Let me create a array of size 1,000. + +411 +00:14:45,000 --> 00:14:48,000 +I don't want to specify my own values, let's say 1,000. + +412 +00:14:48,000 --> 00:14:51,000 +And the value which I'm searching for is, let's say, 500. + +413 +00:14:51,000 --> 00:14:52,000 +Okay, somewhere between, + +414 +00:14:52,000 --> 00:14:54,000 +or maybe I will say 900. + +415 +00:14:54,000 --> 00:14:55,000 +Let's see what happens. + +416 +00:14:55,000 --> 00:14:58,000 +So I have a value, which is 1,000. + +417 +00:14:58,000 --> 00:15:01,000 +I'm searching, I mean, I have 1,000 values in the array, + +418 +00:15:01,000 --> 00:15:02,000 +and I'm searching for 900. + +419 +00:15:02,000 --> 00:15:05,000 +But by default, the value will be zero, okay? + +420 +00:15:05,000 --> 00:15:06,000 +Example, if you search this, + +421 +00:15:06,000 --> 00:15:08,000 +it will say, "Not found." + +422 +00:15:08,000 --> 00:15:09,000 +But look at the number of steps. + +423 +00:15:09,000 --> 00:15:13,000 +LinearSearch takes 1,000, binary takes 10. + +424 +00:15:13,000 --> 00:15:15,000 +And what if you have more values here? + +425 +00:15:15,000 --> 00:15:19,000 +Run, linearSearch takes 10,000, binary takes 4. + +426 +00:15:19,000 --> 00:15:20,000 +And look at the size. + +427 +00:15:20,000 --> 00:15:22,000 +Example, let's say if the size is eight, + +428 +00:15:22,000 --> 00:15:25,000 +and by default, all the values in the array is zero. + +429 +00:15:25,000 --> 00:15:27,000 +So anyway, it is sorted by default. + +430 +00:15:27,000 --> 00:15:29,000 +And you can see if I run this code, + +431 +00:15:29,000 --> 00:15:32,000 +which says eight for linear, four for binary, + +432 +00:15:32,000 --> 00:15:35,000 +but the moment I double the values, + +433 +00:15:35,000 --> 00:15:38,000 +look at these steps taken by binary is only one extra. + +434 +00:15:38,000 --> 00:15:41,000 +Linear is double. + +435 +00:15:41,000 --> 00:15:44,000 +If I double the value again, 32. + +436 +00:15:44,000 --> 00:15:46,000 +So every time you double the value, + +437 +00:15:46,000 --> 00:15:48,000 +binarySearch will take only one extra step. + +438 +00:15:48,000 --> 00:15:50,000 +And that's why we say it's a big O notation + +439 +00:15:50,000 --> 00:15:52,000 +for this is log n, okay? + +440 +00:15:52,000 --> 00:15:54,000 +So in this case, the binarySearch + +441 +00:15:54,000 --> 00:15:56,000 +works better than the linearSearch. + +442 +00:15:56,000 --> 00:15:58,000 +Now, the same code you can write with the help + +443 +00:15:58,000 --> 00:15:59,000 +of binarySearch as well. + +444 +00:15:59,000 --> 00:16:02,000 +Example, instead of using a loop here, + +445 +00:16:02,000 --> 00:16:04,000 +I can simply comment the entire section, + +446 +00:16:04,000 --> 00:16:07,000 +and you can replace this with a recursive function. + +447 +00:16:07,000 --> 00:16:08,000 +The only thing is in recursive, + +448 +00:16:08,000 --> 00:16:10,000 +that the changes you'll be doing is, + +449 +00:16:10,000 --> 00:16:12,000 +in recursive, you're going to call the same function again + +450 +00:16:12,000 --> 00:16:14,000 +and again by changing the values. + +451 +00:16:14,000 --> 00:16:16,000 +Let's say you have this big array, + +452 +00:16:16,000 --> 00:16:20,000 +and then you've done your first search, you found the mid. + +453 +00:16:20,000 --> 00:16:22,000 +Now, the moment you found this section, + +454 +00:16:22,000 --> 00:16:26,000 +let's say this part or this part, which was matching, + +455 +00:16:26,000 --> 00:16:28,000 +you will remove the other half. + +456 +00:16:28,000 --> 00:16:33,000 +And again, you will run the same binarySearch on this list. + +457 +00:16:33,000 --> 00:16:35,000 +And then again you will break it down in two parts. + +458 +00:16:35,000 --> 00:16:36,000 +You will remove one, + +459 +00:16:36,000 --> 00:16:39,000 +and you will run the binarySearch on the other one. + +460 +00:16:39,000 --> 00:16:40,000 +So in binarySearch, basically, + +461 +00:16:40,000 --> 00:16:41,000 +if you want to make it recursive, + +462 +00:16:41,000 --> 00:16:42,000 +you have to pass two more things, + +463 +00:16:42,000 --> 00:16:44,000 +which is int left + +464 +00:16:44,000 --> 00:16:47,000 +and okay, not int left. + +465 +00:16:47,000 --> 00:16:48,000 +You need to specify the left and right. + +466 +00:16:48,000 --> 00:16:53,000 +So it will say zero and nums.length - 1. + +467 +00:16:54,000 --> 00:16:57,000 +So basically, you have to specify the start and the end. + +468 +00:16:57,000 --> 00:17:00,000 +And every time you call binary, + +469 +00:17:00,000 --> 00:17:02,000 +you will simply accept those two things. + +470 +00:17:02,000 --> 00:17:06,000 +You will say left and right, + +471 +00:17:06,000 --> 00:17:08,000 +so that you don't have to specify them here. + +472 +00:17:08,000 --> 00:17:09,000 +In fact, you know, instead of removing it, + +473 +00:17:09,000 --> 00:17:11,000 +maybe you want to refer this later, + +474 +00:17:11,000 --> 00:17:12,000 +I'll just comment this section. + +475 +00:17:12,000 --> 00:17:14,000 +Now, since this is recursive, + +476 +00:17:14,000 --> 00:17:15,000 +you don't need a loop here, right? + +477 +00:17:15,000 --> 00:17:16,000 +What you can do is you can check + +478 +00:17:16,000 --> 00:17:20,000 +if the same step left <= right, + +479 +00:17:22,000 --> 00:17:24,000 +if this is the scenario, + +480 +00:17:24,000 --> 00:17:26,000 +what you will do is, of course, you have to find the mid. + +481 +00:17:26,000 --> 00:17:28,000 +I will just copy this code. + +482 +00:17:28,000 --> 00:17:29,000 +This is where you're finding mid. + +483 +00:17:29,000 --> 00:17:31,000 +And after finding the mid, + +484 +00:17:31,000 --> 00:17:34,000 +you can basically also check the same logic. + +485 +00:17:34,000 --> 00:17:36,000 +This is the logic you're going to use. + +486 +00:17:36,000 --> 00:17:40,000 +The changes is we're gonna change in the else part, + +487 +00:17:40,000 --> 00:17:41,000 +I mean the if-else part. + +488 +00:17:41,000 --> 00:17:43,000 +Let me uncomment the entire section. + +489 +00:17:43,000 --> 00:17:45,000 +The changes you're gonna do, let's not print the steps. + +490 +00:17:45,000 --> 00:17:48,000 +Now, we know the steps, right, how many steps it takes, + +491 +00:17:48,000 --> 00:17:49,000 +but I'm just trying to convert that + +492 +00:17:49,000 --> 00:17:52,000 +into a recursive function. + +493 +00:17:52,000 --> 00:17:53,000 +You will return mid here, + +494 +00:17:53,000 --> 00:17:54,000 +there's no problem with this. + +495 +00:17:54,000 --> 00:17:56,000 +The changes happens in the else if. + +496 +00:17:56,000 --> 00:18:00,000 +So in the else if, instead of doing this, + +497 +00:18:00,000 --> 00:18:02,000 +we are going to basically call the binarySearch + +498 +00:18:02,000 --> 00:18:05,000 +once again by passing the same nums. + +499 +00:18:05,000 --> 00:18:07,000 +And then you will change the target value now. + +500 +00:18:07,000 --> 00:18:08,000 +So you will pass target value. + +501 +00:18:08,000 --> 00:18:10,000 +So you have to pass nums and target value. + +502 +00:18:10,000 --> 00:18:12,000 +Then you have to pass the left value. + +503 +00:18:12,000 --> 00:18:13,000 +Now, what is left? + +504 +00:18:13,000 --> 00:18:16,000 +So in this case, we have to change the left, right? + +505 +00:18:16,000 --> 00:18:18,000 +Left will become mid + 1 + +506 +00:18:18,000 --> 00:18:20,000 +and right will remain same, right will remain right. + +507 +00:18:20,000 --> 00:18:23,000 +But here, again, we have to make the changes. + +508 +00:18:23,000 --> 00:18:26,000 +Else part, instead of this, left will remain left + +509 +00:18:26,000 --> 00:18:29,000 +and right becomes mid - 1. + +510 +00:18:29,000 --> 00:18:31,000 +So basically, what you're doing + +511 +00:18:31,000 --> 00:18:32,000 +is you're changing the values like this. + +512 +00:18:32,000 --> 00:18:33,000 +If you have this list of values, + +513 +00:18:33,000 --> 00:18:36,000 +you break it down into two parts, again after checking. + +514 +00:18:36,000 --> 00:18:38,000 +And then you're, again, calling the same + +515 +00:18:38,000 --> 00:18:40,000 +binarySearch with the new values. + +516 +00:18:40,000 --> 00:18:42,000 +Okay, that's what we're doing here. + +517 +00:18:42,000 --> 00:18:46,000 +So let me remove the extra curly brackets + +518 +00:18:46,000 --> 00:18:47,000 +just to make the code look good + +519 +00:18:48,000 --> 00:18:50,000 +or short basically. + +520 +00:18:50,000 --> 00:18:51,000 +And that's it, this is your binary search. + +521 +00:18:51,000 --> 00:18:53,000 +Let's see if this works. + +522 +00:18:53,000 --> 00:18:56,000 +Let's restart. I mean, rerun. + +523 +00:18:56,000 --> 00:18:58,000 +And it says not found because yeah, + +524 +00:18:58,000 --> 00:19:00,000 +of course, it's not found + +525 +00:19:00,000 --> 00:19:01,000 +because we don't have that value. + +526 +00:19:01,000 --> 00:19:04,000 +But yeah, this is a code for the binary search + +527 +00:19:04,000 --> 00:19:06,000 +with the help of recursive. + +528 +00:19:06,000 --> 00:19:07,000 +Now, there's one more thing. + +529 +00:19:07,000 --> 00:19:09,000 +You can try with different examples. + +530 +00:19:09,000 --> 00:19:12,000 +So what you can do is you can take a array of 1,000 values + +531 +00:19:12,000 --> 00:19:14,000 +and add random value. + +532 +00:19:14,000 --> 00:19:16,000 +So in Java, we have this Random class. + +533 +00:19:16,000 --> 00:19:17,000 +You can add the values + +534 +00:19:17,000 --> 00:19:20,000 +and see how much time it takes. + +535 +00:19:20,000 --> 00:19:21,000 +So yeah, that's it from this. + +536 +00:19:21,000 --> 00:19:24,000 +So we understood the big O notation for linear + +537 +00:19:24,000 --> 00:19:25,000 +and binary search. + +538 +00:19:25,000 --> 00:19:26,000 +And if you have any more questions, + +539 +00:19:26,000 --> 00:19:27,000 +let me know in the Comment section + +540 +00:19:27,000 --> 00:19:29,000 +and do subscribe for further videos. + +541 +00:19:29,000 --> 00:19:30,000 +Bye-bye. + diff --git a/28 - DSA (Optional)/006 Bubble Sort Theory_en.srt b/28 - DSA (Optional)/006 Bubble Sort Theory_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..cd71da842da52034f172cbc0ae812d5631474d76 --- /dev/null +++ b/28 - DSA (Optional)/006 Bubble Sort Theory_en.srt @@ -0,0 +1,840 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: Welcome back, Aliens, my name is Navin Reddy, + +2 +00:00:02,000 --> 00:00:05,000 +and in this video, we'll talk about the sorting techniques. + +3 +00:00:05,000 --> 00:00:06,000 +Now, basically, we have talked about the big + +4 +00:00:06,000 --> 00:00:07,000 +O notation, right? + +5 +00:00:07,000 --> 00:00:10,000 +And now, we know that if you want to judge + +6 +00:00:10,000 --> 00:00:12,000 +the algorithm which you are writing, + +7 +00:00:12,000 --> 00:00:15,000 +you have to make sure that you are using an algorithm + +8 +00:00:15,000 --> 00:00:19,000 +which is efficient in terms of time and space as well. + +9 +00:00:19,000 --> 00:00:22,000 +We are focusing more on the time complexity, + +10 +00:00:22,000 --> 00:00:24,000 +where as your data increases, + +11 +00:00:24,000 --> 00:00:27,000 +it should not increase your time exponentially. + +12 +00:00:27,000 --> 00:00:31,000 +So, we have seen different levels of time complexity, + +13 +00:00:31,000 --> 00:00:33,000 +and we want to be into that lower zone, + +14 +00:00:33,000 --> 00:00:35,000 +not on the upper zone, right? + +15 +00:00:35,000 --> 00:00:39,000 +Now, when it comes to understanding different algorithms, + +16 +00:00:39,000 --> 00:00:41,000 +we have to go for some in-built algorithms, right? + +17 +00:00:41,000 --> 00:00:44,000 +Or some old algorithm, which normally we use. + +18 +00:00:44,000 --> 00:00:47,000 +So, sorting, so, we have talked about searching. + +19 +00:00:47,000 --> 00:00:49,000 +Sorting is another way you can learn more about algorithms + +20 +00:00:49,000 --> 00:00:52,000 +and how do you make it more efficient. + +21 +00:00:52,000 --> 00:00:55,000 +Also, in the real world, we do use sorting a lot. + +22 +00:00:55,000 --> 00:00:56,000 +So, let's say, when you say you want + +23 +00:00:56,000 --> 00:00:58,000 +to search something on the internet, + +24 +00:00:58,000 --> 00:01:01,000 +or maybe you want to buy something on the Amazon, + +25 +00:01:01,000 --> 00:01:05,000 +you do that sort by price, sort by reviews, right? + +26 +00:01:05,000 --> 00:01:09,000 +So, you do that multiple times, so, sorting is majorly used. + +27 +00:01:09,000 --> 00:01:10,000 +So, there are a lot of different sorting + +28 +00:01:10,000 --> 00:01:11,000 +techniques available, + +29 +00:01:11,000 --> 00:01:12,000 +so, we'll try to understand them. + +30 +00:01:12,000 --> 00:01:15,000 +Not everything, or not every algorithm, + +31 +00:01:15,000 --> 00:01:17,000 +but let's try to understand few of them. + +32 +00:01:17,000 --> 00:01:18,000 +Now, if we talk about these algorithms, + +33 +00:01:18,000 --> 00:01:21,000 +some algorithms are easy or simple to understand, + +34 +00:01:21,000 --> 00:01:24,000 +but they are not time efficient, + +35 +00:01:24,000 --> 00:01:26,000 +and then there are some algorithms which are good + +36 +00:01:26,000 --> 00:01:28,000 +in terms of time, so, they're time efficient, + +37 +00:01:28,000 --> 00:01:31,000 +but they're not simple to understand, okay? + +38 +00:01:31,000 --> 00:01:33,000 +So, we have to do a trade off here. + +39 +00:01:33,000 --> 00:01:34,000 +Now, which is the best one? + +40 +00:01:34,000 --> 00:01:35,000 +That depends. + +41 +00:01:35,000 --> 00:01:37,000 +If you have less number of elements, + +42 +00:01:37,000 --> 00:01:39,000 +and if you're learning for the education purpose, + +43 +00:01:39,000 --> 00:01:42,000 +or to understand how sorting works, + +44 +00:01:42,000 --> 00:01:44,000 +then we can talk about those algorithms, + +45 +00:01:44,000 --> 00:01:47,000 +like bubble sort, selection sort, + +46 +00:01:47,000 --> 00:01:49,000 +and then there are some algorithms which are bit difficult, + +47 +00:01:49,000 --> 00:01:51,000 +and they are very efficient when you have + +48 +00:01:51,000 --> 00:01:53,000 +a huge amount of data. + +49 +00:01:53,000 --> 00:01:54,000 +Now, what I'm talking about, + +50 +00:01:54,000 --> 00:01:55,000 +so, when you talk about sorting, + +51 +00:01:55,000 --> 00:01:57,000 +there are different options available. + +52 +00:01:57,000 --> 00:02:00,000 +So, we have bubble sort, selection sort, insertion sort, + +53 +00:02:00,000 --> 00:02:03,000 +merge sort, quick sort, counting sort, + +54 +00:02:03,000 --> 00:02:05,000 +radix, heap sort, bucket sort, + +55 +00:02:05,000 --> 00:02:07,000 +then there are multiple sorting techniques. + +56 +00:02:07,000 --> 00:02:10,000 +The famous one are bubble sort, quick sort, + +57 +00:02:10,000 --> 00:02:13,000 +selection sort, insertion, and then merge sort, + +58 +00:02:13,000 --> 00:02:15,000 +if I'm not repeated that multiple times. + +59 +00:02:15,000 --> 00:02:18,000 +So, let's start with the first one, which is bubble sort. + +60 +00:02:18,000 --> 00:02:20,000 +Now, bubble sort is not efficient, + +61 +00:02:20,000 --> 00:02:21,000 +but it is simple to understand, + +62 +00:02:21,000 --> 00:02:23,000 +so, that gives us a starting point. + +63 +00:02:23,000 --> 00:02:26,000 +We talk about bubble sort, why do we use it? + +64 +00:02:26,000 --> 00:02:28,000 +So, let's say, if you want to sort different elements, + +65 +00:02:28,000 --> 00:02:30,000 +so, let's say, we have this list in front of you. + +66 +00:02:30,000 --> 00:02:34,000 +The numbers are 8, 6, 9, 2, 4, 5. + +67 +00:02:34,000 --> 00:02:37,000 +Now, if you want to basically sort them, how will you do it? + +68 +00:02:37,000 --> 00:02:39,000 +Now, of course, we don't have a magical way + +69 +00:02:39,000 --> 00:02:40,000 +where you can find, hey, this is the first one, + +70 +00:02:40,000 --> 00:02:41,000 +this is the second one, + +71 +00:02:41,000 --> 00:02:44,000 +so, what you do is you basically try to use + +72 +00:02:44,000 --> 00:02:47,000 +some algorithm like bubble sort to sort it. + +73 +00:02:47,000 --> 00:02:50,000 +In this, what you do is you basically create a bubble. + +74 +00:02:50,000 --> 00:02:52,000 +Now, what exactly this bubble is? + +75 +00:02:52,000 --> 00:02:54,000 +Take one element, put that into bubble, + +76 +00:02:54,000 --> 00:02:56,000 +and shift it to the end. + +77 +00:02:56,000 --> 00:02:57,000 +Okay, now, that sounds weird, right? + +78 +00:02:57,000 --> 00:02:58,000 +So, let's do step by step. + +79 +00:02:58,000 --> 00:02:59,000 +Now, when you have this value, + +80 +00:02:59,000 --> 00:03:01,000 +let's say, you have six value in front of you, + +81 +00:03:01,000 --> 00:03:04,000 +you can put them in an array like this, + +82 +00:03:04,000 --> 00:03:06,000 +and then you have provided the index values. + +83 +00:03:06,000 --> 00:03:09,000 +Now, if you want to do this sorting on this, + +84 +00:03:09,000 --> 00:03:11,000 +of course, we want ascending sorting here, + +85 +00:03:11,000 --> 00:03:13,000 +so, basically, we want 2 at the start, + +86 +00:03:13,000 --> 00:03:17,000 +then 4, then 5, then 6, then 8, and then 9. + +87 +00:03:17,000 --> 00:03:19,000 +That's a sequence we want, but how will you get it? + +88 +00:03:19,000 --> 00:03:24,000 +So, what you do is you compare two values at a time. + +89 +00:03:24,000 --> 00:03:26,000 +So, let's say, if you put six people in front of you, + +90 +00:03:26,000 --> 00:03:28,000 +and they want to sort them according to their height. + +91 +00:03:28,000 --> 00:03:30,000 +So, what you do, of course, you will compare + +92 +00:03:30,000 --> 00:03:31,000 +two people at a time, right? + +93 +00:03:31,000 --> 00:03:34,000 +That's how you sort, and then you will try to swap them. + +94 +00:03:34,000 --> 00:03:36,000 +So, how exactly it works? + +95 +00:03:36,000 --> 00:03:38,000 +So, let's say, we have these two values here, 8 and 6, + +96 +00:03:38,000 --> 00:03:40,000 +and then what you do is you want to make sure + +97 +00:03:40,000 --> 00:03:44,000 +that the biggest element from this particular array + +98 +00:03:44,000 --> 00:03:47,000 +in the first iteration goes to the end. + +99 +00:03:47,000 --> 00:03:49,000 +Okay, so, the way you do that is first, + +100 +00:03:49,000 --> 00:03:50,000 +you compare the first two values. + +101 +00:03:50,000 --> 00:03:52,000 +Now, these are your first two values. + +102 +00:03:52,000 --> 00:03:54,000 +When you're comparing it, if the first value + +103 +00:03:54,000 --> 00:03:56,000 +is greater than the second value, + +104 +00:03:56,000 --> 00:03:57,000 +you basically need to swap, okay? + +105 +00:03:57,000 --> 00:03:59,000 +Swapping is very important here. + +106 +00:03:59,000 --> 00:04:01,000 +So, you have to first compare the first two values. + +107 +00:04:01,000 --> 00:04:03,000 +If the first value is greater than the second value, + +108 +00:04:03,000 --> 00:04:04,000 +then you have to swap, + +109 +00:04:04,000 --> 00:04:06,000 +and then you basically shift your pointer + +110 +00:04:06,000 --> 00:04:08,000 +to the next two values. + +111 +00:04:08,000 --> 00:04:09,000 +Okay, done, we are done with the 6. + +112 +00:04:09,000 --> 00:04:10,000 +Now, let's go for the next two. + +113 +00:04:10,000 --> 00:04:12,000 +The next two is 8 and 9. + +114 +00:04:12,000 --> 00:04:16,000 +If the first value is greater than the second value, + +115 +00:04:16,000 --> 00:04:17,000 +you have to swap. + +116 +00:04:17,000 --> 00:04:19,000 +In this case, they're not, they are... + +117 +00:04:19,000 --> 00:04:21,000 +So, the first value is less than the second value, + +118 +00:04:21,000 --> 00:04:23,000 +so, what you do is you simply say, skip, + +119 +00:04:23,000 --> 00:04:26,000 +because there's no swapping needed, + +120 +00:04:26,000 --> 00:04:27,000 +then you go for the next two values. + +121 +00:04:27,000 --> 00:04:29,000 +The first value is greater than the second value, + +122 +00:04:29,000 --> 00:04:30,000 +so, of course, you have to swap, + +123 +00:04:30,000 --> 00:04:33,000 +and then again, you shift your values. + +124 +00:04:33,000 --> 00:04:35,000 +Next two values, again, you will compare. + +125 +00:04:35,000 --> 00:04:38,000 +The first value is greater than second, swap. + +126 +00:04:38,000 --> 00:04:40,000 +Then again, you will compare the next two value. + +127 +00:04:40,000 --> 00:04:42,000 +The first value is greater than second, swap. + +128 +00:04:42,000 --> 00:04:44,000 +So, basically, after the first iteration, + +129 +00:04:44,000 --> 00:04:48,000 +you got the biggest value at the end, but is it sorted? + +130 +00:04:48,000 --> 00:04:49,000 +Unfortunately not. + +131 +00:04:49,000 --> 00:04:53,000 +You have to do this operation one more time. + +132 +00:04:53,000 --> 00:04:54,000 +Actually, not one more time, + +133 +00:04:54,000 --> 00:04:56,000 +multiple times till you get this sorted. + +134 +00:04:56,000 --> 00:04:58,000 +Now, this also depends upon the size + +135 +00:04:58,000 --> 00:05:00,000 +of your elements or array. + +136 +00:05:00,000 --> 00:05:03,000 +Now, in this case, the first five values are still unsorted. + +137 +00:05:03,000 --> 00:05:05,000 +The last one is done, so, we don't have to touch + +138 +00:05:05,000 --> 00:05:06,000 +the last element. + +139 +00:05:06,000 --> 00:05:10,000 +So, in the first iteration, if you're going for six times, + +140 +00:05:10,000 --> 00:05:12,000 +the second iteration, you can go for five times. + +141 +00:05:12,000 --> 00:05:14,000 +The third iteration, you can go for four times. + +142 +00:05:14,000 --> 00:05:15,000 +So, let's see how that works. + +143 +00:05:15,000 --> 00:05:17,000 +Now, after the first iteration, + +144 +00:05:17,000 --> 00:05:19,000 +we got the 9 value at the end. + +145 +00:05:19,000 --> 00:05:22,000 +Now, what you do is, again, you repeat the same steps. + +146 +00:05:22,000 --> 00:05:23,000 +Compare the first two values. + +147 +00:05:23,000 --> 00:05:25,000 +Is it required to swap in this case? + +148 +00:05:25,000 --> 00:05:27,000 +No, we can skip it. + +149 +00:05:27,000 --> 00:05:27,000 +Next two values, + +150 +00:05:27,000 --> 00:05:29,000 +is the first value greater than second value? + +151 +00:05:29,000 --> 00:05:31,000 +Yes, swap it. + +152 +00:05:31,000 --> 00:05:32,000 +Then again, you go for this next two values. + +153 +00:05:32,000 --> 00:05:34,000 +Is get 8 greater than 4? + +154 +00:05:34,000 --> 00:05:35,000 +Yes, swap it. + +155 +00:05:35,000 --> 00:05:38,000 +Then again, next two values. + +156 +00:05:38,000 --> 00:05:40,000 +8 greater than 5, yes, swap it. + +157 +00:05:40,000 --> 00:05:42,000 +Now, in this case, what happened + +158 +00:05:42,000 --> 00:05:45,000 +is after the two iterations, you got the two biggest values, + +159 +00:05:45,000 --> 00:05:48,000 +which is 8 and 9 at the end. + +160 +00:05:48,000 --> 00:05:51,000 +Now, we know that those two values are sorted, + +161 +00:05:51,000 --> 00:05:52,000 +but what about other values? + +162 +00:05:52,000 --> 00:05:54,000 +Those are not sorted, so, let's sort them. + +163 +00:05:54,000 --> 00:05:55,000 +Again, you have to repeat the same steps. + +164 +00:05:55,000 --> 00:05:59,000 +So, you have to say, 6, 2, which is 6 is greater than 2. + +165 +00:05:59,000 --> 00:06:01,000 +Again, we have to swap it. + +166 +00:06:01,000 --> 00:06:04,000 +6 and 4, 6 is greater than 4, swap it. + +167 +00:06:04,000 --> 00:06:06,000 +6 and 5, again, swap it. + +168 +00:06:06,000 --> 00:06:10,000 +So, now, after this iteration, you've got three values, + +169 +00:06:10,000 --> 00:06:13,000 +6, 8, 9, which is sorted, + +170 +00:06:13,000 --> 00:06:14,000 +but what about other values? + +171 +00:06:14,000 --> 00:06:16,000 +They're still unsorted. + +172 +00:06:16,000 --> 00:06:17,000 +I mean, we know they're sorted now, + +173 +00:06:17,000 --> 00:06:19,000 +but your algorithm has no idea if they're sorted. + +174 +00:06:19,000 --> 00:06:20,000 +Okay, now, this is tricky, + +175 +00:06:20,000 --> 00:06:24,000 +is because your algorithm goes from start to end, okay? + +176 +00:06:24,000 --> 00:06:27,000 +It will not check for do the other values are sorted or not, + +177 +00:06:27,000 --> 00:06:28,000 +so, it will keep doing that. + +178 +00:06:28,000 --> 00:06:33,000 +So, it will compare the first values, swap not needed. + +179 +00:06:33,000 --> 00:06:36,000 +Next two values, swap not needed, done. + +180 +00:06:36,000 --> 00:06:38,000 +We have also confirmed 5, + +181 +00:06:38,000 --> 00:06:39,000 +still, there's a confusion for 2 and 4. + +182 +00:06:39,000 --> 00:06:42,000 +Your algorithm has no idea those two are already sorted, + +183 +00:06:42,000 --> 00:06:44,000 +but still, it will try to sort them. + +184 +00:06:44,000 --> 00:06:49,000 +It will compare the two values, they are compared, + +185 +00:06:49,000 --> 00:06:50,000 +swapping not needed. + +186 +00:06:50,000 --> 00:06:52,000 +So, by doing that, four is confirmed, + +187 +00:06:52,000 --> 00:06:54,000 +and since you only have one element now, + +188 +00:06:54,000 --> 00:06:56,000 +that itself is sorted, and by doing this, + +189 +00:06:56,000 --> 00:06:58,000 +you got the entire sorted array. + +190 +00:06:58,000 --> 00:07:02,000 +So, yes, even if it is sorted, it will still check. + +191 +00:07:02,000 --> 00:07:06,000 +We are reducing one step, which is swapping, + +192 +00:07:06,000 --> 00:07:07,000 +but still, you are checking, + +193 +00:07:07,000 --> 00:07:11,000 +and that's why bubble sort is not an efficient algorithm. + +194 +00:07:11,000 --> 00:07:14,000 +And the problem is the time complexity for this + +195 +00:07:14,000 --> 00:07:16,000 +is O of n squared, + +196 +00:07:16,000 --> 00:07:17,000 +because you have to use two loops, + +197 +00:07:17,000 --> 00:07:20,000 +one for the iterations, + +198 +00:07:20,000 --> 00:07:22,000 +and the inner loop for each iteration, + +199 +00:07:22,000 --> 00:07:23,000 +you have to compare the values. + +200 +00:07:23,000 --> 00:07:26,000 +So, that's your n squared, + +201 +00:07:26,000 --> 00:07:29,000 +not a good way of of sorting the elements. + +202 +00:07:29,000 --> 00:07:31,000 +Now, think about six elements, so, that's good. + +203 +00:07:31,000 --> 00:07:34,000 +Six squared is not a big number. + +204 +00:07:34,000 --> 00:07:35,000 +It is, actually, + +205 +00:07:35,000 --> 00:07:38,000 +but what about if you have a very big array, + +206 +00:07:38,000 --> 00:07:41,000 +or a very big list, let's say, 20 values, 30 values? + +207 +00:07:41,000 --> 00:07:42,000 +Imagine the number of steps it will take. + +208 +00:07:42,000 --> 00:07:45,000 +So, not an efficient algorithm, but easy to understand. + +209 +00:07:45,000 --> 00:07:48,000 +Now, it's time to convert this into a code, + +210 +00:07:48,000 --> 00:07:50,000 +which we'll do in the next video. + diff --git a/28 - DSA (Optional)/007 Bubble Sort Code_en.srt b/28 - DSA (Optional)/007 Bubble Sort Code_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..568b624f4d8a5bb23b885fd480b0c195164af63f --- /dev/null +++ b/28 - DSA (Optional)/007 Bubble Sort Code_en.srt @@ -0,0 +1,840 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: Okay, now let's try to implement the concept + +2 +00:00:02,000 --> 00:00:04,000 +of bubble sort in the code. + +3 +00:00:04,000 --> 00:00:06,000 +So we are going to use Java as a language. + +4 +00:00:06,000 --> 00:00:08,000 +So if you want to sort, + +5 +00:00:08,000 --> 00:00:10,000 +of course you have to store the values somewhere first. + +6 +00:00:10,000 --> 00:00:13,000 +So what I will do is I will create an array of integers + +7 +00:00:13,000 --> 00:00:15,000 +and let me just name this as nums + +8 +00:00:15,000 --> 00:00:17,000 +and then I will assign some values. + +9 +00:00:17,000 --> 00:00:19,000 +Of course I can go for this new array + +10 +00:00:19,000 --> 00:00:21,000 +and then assign the value one by one + +11 +00:00:21,000 --> 00:00:24,000 +or we can give them these static values. + +12 +00:00:24,000 --> 00:00:26,000 +So here, let's say we go with values + +13 +00:00:26,000 --> 00:00:31,000 +6,5,2,8,9,4. + +14 +00:00:32,000 --> 00:00:34,000 +I don't remember the sequence in the previous video, + +15 +00:00:34,000 --> 00:00:35,000 +which we talked about the theory, + +16 +00:00:35,000 --> 00:00:37,000 +but let's go with these values. + +17 +00:00:37,000 --> 00:00:38,000 +And I want to sort them. + +18 +00:00:38,000 --> 00:00:40,000 +Okay, so what I will do is even before sorting, + +19 +00:00:40,000 --> 00:00:42,000 +let me print all the values. + +20 +00:00:42,000 --> 00:00:45,000 +The way you can print the values of an array in Java is you, + +21 +00:00:45,000 --> 00:00:47,000 +of course you can use a normal loop, + +22 +00:00:47,000 --> 00:00:49,000 +or we can use enhanced for loop. + +23 +00:00:49,000 --> 00:00:50,000 +So let me just use enhanced for loop here. + +24 +00:00:50,000 --> 00:00:54,000 +So I will say for int_num_ in nums + +25 +00:00:54,000 --> 00:00:57,000 +and then you can print the values, right? + +26 +00:00:57,000 --> 00:00:59,000 +So I'm printing num here. + +27 +00:00:59,000 --> 00:01:02,000 +So I will say this is before sorting. + +28 +00:01:02,000 --> 00:01:04,000 +And then I will do the same thing, + +29 +00:01:04,000 --> 00:01:06,000 +I will execute the same code, + +30 +00:01:06,000 --> 00:01:07,000 +but after sorting here. + +31 +00:01:07,000 --> 00:01:10,000 +So let's say this is after sorting. + +32 +00:01:10,000 --> 00:01:11,000 +Okay, now if I run this code, + +33 +00:01:11,000 --> 00:01:13,000 +of course you will get the same value two times. + +34 +00:01:13,000 --> 00:01:16,000 +So if I, and one more thing I will not be, + +35 +00:01:16,000 --> 00:01:17,000 +I want to print this on the same line + +36 +00:01:17,000 --> 00:01:20,000 +because ln will print on a new line, I don't want it. + +37 +00:01:20,000 --> 00:01:22,000 +And maybe after every printing + +38 +00:01:22,000 --> 00:01:23,000 +I want to give some space as well + +39 +00:01:23,000 --> 00:01:25,000 +to differentiate the values. + +40 +00:01:25,000 --> 00:01:27,000 +And let me do that here as well + +41 +00:01:27,000 --> 00:01:30,000 +so that it will print on the same line with spaces. + +42 +00:01:30,000 --> 00:01:33,000 +Right-click here and says Run. + +43 +00:01:33,000 --> 00:01:36,000 +And you can see we got before sorting, after sorting. + +44 +00:01:36,000 --> 00:01:40,000 +I think this should be printed on new line. + +45 +00:01:40,000 --> 00:01:42,000 +So what I can do is just before this + +46 +00:01:42,000 --> 00:01:44,000 +I would say out and run. + +47 +00:01:44,000 --> 00:01:46,000 +Okay, so this is before sorting + +48 +00:01:46,000 --> 00:01:47,000 +and this is after sorting. + +49 +00:01:47,000 --> 00:01:49,000 +At this point it is not getting sorted of course, right? + +50 +00:01:49,000 --> 00:01:51,000 +We have not done any logic here. + +51 +00:01:51,000 --> 00:01:53,000 +Now this is where in between + +52 +00:01:53,000 --> 00:01:54,000 +you have to write the logic for sorting. + +53 +00:01:54,000 --> 00:01:56,000 +Now in bubble sort what you do is, + +54 +00:01:56,000 --> 00:01:58,000 +you compare the values two values at a time + +55 +00:01:58,000 --> 00:01:59,000 +and then you swap. + +56 +00:01:59,000 --> 00:02:00,000 +So you compare these two values. + +57 +00:02:00,000 --> 00:02:03,000 +If the first one is greater than second one, just swap. + +58 +00:02:03,000 --> 00:02:06,000 +And then likewise you will do for all the elements. + +59 +00:02:06,000 --> 00:02:09,000 +Now, for sure, if you want to move to iterations, + +60 +00:02:09,000 --> 00:02:11,000 +now we have to do two loops here, + +61 +00:02:11,000 --> 00:02:13,000 +the nested loop, basically. + +62 +00:02:13,000 --> 00:02:16,000 +The outer loop is responsible for the number of iterations + +63 +00:02:16,000 --> 00:02:18,000 +or the number of passes, + +64 +00:02:18,000 --> 00:02:21,000 +and the inner loop is actually responsible for swapping. + +65 +00:02:21,000 --> 00:02:24,000 +So what I mean by that is I need a first variable i. + +66 +00:02:24,000 --> 00:02:27,000 +We'll start from zero and where are we going to end it? + +67 +00:02:27,000 --> 00:02:30,000 +So, of course, I can end it at less than six. + +68 +00:02:30,000 --> 00:02:35,000 +So you can say six or you can say nums.length, okay? + +69 +00:02:35,000 --> 00:02:37,000 +So basically we can use the array length. + +70 +00:02:37,000 --> 00:02:39,000 +You know, the better way would be + +71 +00:02:39,000 --> 00:02:40,000 +to store this value somewhere. + +72 +00:02:40,000 --> 00:02:45,000 +So I can say size nums.length. + +73 +00:02:45,000 --> 00:02:48,000 +So I'm just storing that value in a variable called size. + +74 +00:02:48,000 --> 00:02:50,000 +It'll be easier to work with. + +75 +00:02:50,000 --> 00:02:54,000 +So I'll simply say size and then I will say i++. + +76 +00:02:54,000 --> 00:02:56,000 +So it'll go from start to end. + +77 +00:02:56,000 --> 00:02:58,000 +So this is only for passing, right? + +78 +00:02:58,000 --> 00:02:59,000 +We have to do this multiple times. + +79 +00:02:59,000 --> 00:03:00,000 +You have seen that in the animations. + +80 +00:03:00,000 --> 00:03:02,000 +But now let's do the inner loop, + +81 +00:03:02,000 --> 00:03:05,000 +which will do the actual swapping. + +82 +00:03:05,000 --> 00:03:08,000 +So here I would say int j = 0 + +83 +00:03:08,000 --> 00:03:10,000 +because we're going to start from zero. + +84 +00:03:10,000 --> 00:03:13,000 +The real question is where are we going to end it. + +85 +00:03:13,000 --> 00:03:14,000 +At this point, I would say size. + +86 +00:03:14,000 --> 00:03:16,000 +And let's see what happens if we do with size. + +87 +00:03:16,000 --> 00:03:20,000 +Okay, so both the loop, the outer loop and the inner loop + +88 +00:03:20,000 --> 00:03:22,000 +are starting from zero and ending at size, + +89 +00:03:22,000 --> 00:03:24,000 +the size of the array. + +90 +00:03:24,000 --> 00:03:27,000 +And now we have to basically compare two values. + +91 +00:03:27,000 --> 00:03:29,000 +Now how will you compare two values? + +92 +00:03:29,000 --> 00:03:31,000 +And how do we even swap them? + +93 +00:03:31,000 --> 00:03:34,000 +So we will check if the first value. + +94 +00:03:34,000 --> 00:03:36,000 +Now how do you know the first value? + +95 +00:03:36,000 --> 00:03:40,000 +So nums of 6 or nums of 0, basically, 6 is nums of 0. + +96 +00:03:40,000 --> 00:03:43,000 +So nums of 0 compared with nums of 1. + +97 +00:03:43,000 --> 00:03:44,000 +That's how you compare. + +98 +00:03:44,000 --> 00:03:47,000 +Again, you can compare nums of 1 with nums of 2, + +99 +00:03:47,000 --> 00:03:49,000 +then nums of 2 with nums of 3, + +100 +00:03:49,000 --> 00:03:52,000 +then nums of 3 with nums of 4. + +101 +00:03:52,000 --> 00:03:54,000 +That means the current number. + +102 +00:03:54,000 --> 00:03:57,000 +When you say 0, 1, 2, it is represented by j. + +103 +00:03:57,000 --> 00:03:59,000 +So I can say nums of j, + +104 +00:03:59,000 --> 00:04:01,000 +but this will be compared with the next value. + +105 +00:04:01,000 --> 00:04:02,000 +And how do I know the next value, + +106 +00:04:02,000 --> 00:04:06,000 +which is your nums of j + 1. + +107 +00:04:06,000 --> 00:04:09,000 +As simple as that. So we are comparing two values. + +108 +00:04:09,000 --> 00:04:10,000 +Now, how we are comparing them? + +109 +00:04:10,000 --> 00:04:12,000 +Which operator we are going to use here? + +110 +00:04:12,000 --> 00:04:14,000 +So we are going to use greater than. + +111 +00:04:14,000 --> 00:04:17,000 +If the first value is greater than the second value, + +112 +00:04:17,000 --> 00:04:17,000 +then you have to swap. + +113 +00:04:17,000 --> 00:04:19,000 +Simple technique, right? + +114 +00:04:19,000 --> 00:04:20,000 +Now, how do you swap? + +115 +00:04:20,000 --> 00:04:23,000 +There are different ways of which you can swap the values. + +116 +00:04:23,000 --> 00:04:24,000 +I'm going to use a traditional way. + +117 +00:04:24,000 --> 00:04:26,000 +Let me use a temp variable. + +118 +00:04:26,000 --> 00:04:29,000 +In fact, you can also declare a temp variable here. + +119 +00:04:29,000 --> 00:04:30,000 +So I can say int, + +120 +00:04:30,000 --> 00:04:33,000 +you know it's better to have all the variables on top. + +121 +00:04:33,000 --> 00:04:36,000 +So we'll say int temp = 0 initially, + +122 +00:04:36,000 --> 00:04:39,000 +and then using the third variable, I can simply swap them. + +123 +00:04:39,000 --> 00:04:43,000 +So I can say temp = nums of j, + +124 +00:04:44,000 --> 00:04:49,000 +and then nums of j = nums of j + 1, + +125 +00:04:51,000 --> 00:04:55,000 +and then nums of j + 1 = temp. + +126 +00:04:55,000 --> 00:04:57,000 +So basically what we're doing is, + +127 +00:04:57,000 --> 00:04:58,000 +with these three lines, we are just swapping them. + +128 +00:04:58,000 --> 00:05:02,000 +Nothing complex, just swapping the two values, okay? + +129 +00:05:02,000 --> 00:05:04,000 +And that's what we have done here. + +130 +00:05:04,000 --> 00:05:06,000 +So every time you find this, + +131 +00:05:06,000 --> 00:05:08,000 +the first value greater than the second value, + +132 +00:05:08,000 --> 00:05:09,000 +just swap them. + +133 +00:05:09,000 --> 00:05:10,000 +And you do this multiple times + +134 +00:05:10,000 --> 00:05:14,000 +because we are keeping this in a loop and that's it. + +135 +00:05:14,000 --> 00:05:15,000 +The sorting is done. + +136 +00:05:16,000 --> 00:05:18,000 +Since it will be happening multiple times, + +137 +00:05:18,000 --> 00:05:21,000 +it will make sure that you keep the values at the end, + +138 +00:05:21,000 --> 00:05:24,000 +the bigger value at the end after each iteration. + +139 +00:05:24,000 --> 00:05:27,000 +So if you run this, okay, we got an error. + +140 +00:05:27,000 --> 00:05:29,000 +It says out of bounds. + +141 +00:05:29,000 --> 00:05:30,000 +Now there's one problem here. + +142 +00:05:31,000 --> 00:05:34,000 +Don't you think, when we are going for j + 1, + +143 +00:05:34,000 --> 00:05:35,000 +what about the last value? + +144 +00:05:35,000 --> 00:05:39,000 +So we need to make sure that we don't go to the last value. + +145 +00:05:39,000 --> 00:05:42,000 +So it should end minus one, right? + +146 +00:05:42,000 --> 00:05:45,000 +Because when you're comparing the last two values, + +147 +00:05:45,000 --> 00:05:49,000 +the value for j will be here and this will be j + 1. + +148 +00:05:49,000 --> 00:05:50,000 +So we have to make sure that your j ends + +149 +00:05:50,000 --> 00:05:55,000 +before the last value, that was the error, and Run. + +150 +00:05:55,000 --> 00:05:59,000 +And you can see sorting done and you got the sorted values. + +151 +00:05:59,000 --> 00:06:01,000 +This is how basically you sort them. + +152 +00:06:01,000 --> 00:06:03,000 +Again, we have seen this explanation in theory, + +153 +00:06:03,000 --> 00:06:04,000 +how things are working out, + +154 +00:06:04,000 --> 00:06:08,000 +but here we just try to sort it with the help of this logic. + +155 +00:06:08,000 --> 00:06:09,000 +Now there's one problem here. + +156 +00:06:10,000 --> 00:06:12,000 +Every time you do this, + +157 +00:06:12,000 --> 00:06:14,000 +in the animation, if you remember, + +158 +00:06:14,000 --> 00:06:17,000 +after every pass, we don't have to go for all the values + +159 +00:06:17,000 --> 00:06:19,000 +because the last value is sorted. + +160 +00:06:19,000 --> 00:06:21,000 +In the next pass, two values are sorted. + +161 +00:06:21,000 --> 00:06:23,000 +In the next pass, three values are sorted. + +162 +00:06:23,000 --> 00:06:25,000 +So we don't have to sort them again + +163 +00:06:25,000 --> 00:06:26,000 +or even check them again. + +164 +00:06:26,000 --> 00:06:30,000 +So here j should be not going to size minus 1. + +165 +00:06:30,000 --> 00:06:34,000 +In fact it should go size minus i minus 1. + +166 +00:06:34,000 --> 00:06:35,000 +It's because after every iteration, + +167 +00:06:35,000 --> 00:06:38,000 +so let's say we have the first situation where i is 0, + +168 +00:06:38,000 --> 00:06:41,000 +it will go to the second last value + +169 +00:06:41,000 --> 00:06:42,000 +because we are saying minus one. + +170 +00:06:42,000 --> 00:06:43,000 +In the second iteration + +171 +00:06:43,000 --> 00:06:45,000 +where the i value will be 1, + +172 +00:06:45,000 --> 00:06:47,000 +it will go to the second last position + +173 +00:06:47,000 --> 00:06:50,000 +or third last position, because the last value is sorted. + +174 +00:06:50,000 --> 00:06:51,000 +You don't have to check that. + +175 +00:06:51,000 --> 00:06:53,000 +Again, it will not affect the output, + +176 +00:06:53,000 --> 00:06:54,000 +but it will affect the speed + +177 +00:06:54,000 --> 00:06:56,000 +because we are saving some time + +178 +00:06:56,000 --> 00:07:00,000 +by not checking the same sorted value again and again, okay? + +179 +00:07:00,000 --> 00:07:02,000 +That's how basically you do it. + +180 +00:07:02,000 --> 00:07:04,000 +In fact, if you want, what we can also do is, + +181 +00:07:04,000 --> 00:07:07,000 +we can print this in every iteration. + +182 +00:07:07,000 --> 00:07:08,000 +So I can just copy this + +183 +00:07:08,000 --> 00:07:11,000 +and after every iteration of the outer loop + +184 +00:07:11,000 --> 00:07:12,000 +we can print the values + +185 +00:07:12,000 --> 00:07:15,000 +and see what is happening there, right? + +186 +00:07:15,000 --> 00:07:18,000 +So, in fact, I will also print a new line here + +187 +00:07:18,000 --> 00:07:20,000 +so that it will come on new line. + +188 +00:07:21,000 --> 00:07:25,000 +Let's run this. So this is what basically is happening. + +189 +00:07:25,000 --> 00:07:26,000 +Initially you have this value + +190 +00:07:26,000 --> 00:07:29,000 +and then this is where the sorting starts. + +191 +00:07:29,000 --> 00:07:31,000 +If you can see after the first iteration, + +192 +00:07:31,000 --> 00:07:33,000 +the biggest value, 9, just went to the end. + +193 +00:07:33,000 --> 00:07:36,000 +Then after the next, for the next iteration, + +194 +00:07:36,000 --> 00:07:39,000 +the biggest value, which is 8, + +195 +00:07:39,000 --> 00:07:41,000 +is at the end or second biggest value. + +196 +00:07:41,000 --> 00:07:43,000 +Third iteration, we got three value sorted. + +197 +00:07:43,000 --> 00:07:45,000 +Fourth iteration, we got four value sorted. + +198 +00:07:45,000 --> 00:07:48,000 +Fifth iteration we got four value sorted + +199 +00:07:48,000 --> 00:07:49,000 +or five value sorted, + +200 +00:07:49,000 --> 00:07:51,000 +and last iteration we got all the value sorted. + +201 +00:07:51,000 --> 00:07:53,000 +So that's how basically it does. + +202 +00:07:53,000 --> 00:07:56,000 +Of course here too, we got at this start itself, + +203 +00:07:56,000 --> 00:07:57,000 +we don't have to sort them again, + +204 +00:07:57,000 --> 00:07:59,000 +but that's how algorithm works, right? + +205 +00:07:59,000 --> 00:08:02,000 +How will you even check if the array is sorted + +206 +00:08:02,000 --> 00:08:04,000 +even before you compare all the values? + +207 +00:08:04,000 --> 00:08:05,000 +So that's the thing about the bubble sort, + +208 +00:08:05,000 --> 00:08:08,000 +where you compare the values, swap them, and you're done. + +209 +00:08:08,000 --> 00:08:11,000 +So yeah, that's about this particular video + +210 +00:08:11,000 --> 00:08:13,000 +where we have done the coding for bubble sort. + diff --git a/28 - DSA (Optional)/008 Selection Sort Theory_en.srt b/28 - DSA (Optional)/008 Selection Sort Theory_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..0a230e62ed3a6ceb36bdad6b5859aeed7d6ca6ed --- /dev/null +++ b/28 - DSA (Optional)/008 Selection Sort Theory_en.srt @@ -0,0 +1,740 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: In this video, we'll talk about selection sort. + +2 +00:00:02,000 --> 00:00:04,000 +So basically we'll talk about the theory in this + +3 +00:00:04,000 --> 00:00:06,000 +and then in the next video we'll talk + +4 +00:00:06,000 --> 00:00:07,000 +about the practical of it. + +5 +00:00:07,000 --> 00:00:09,000 +The thing is, when you talk about sorting, + +6 +00:00:09,000 --> 00:00:11,000 +we have different techniques available. + +7 +00:00:11,000 --> 00:00:14,000 +In the previous video we have talked about bubble sort + +8 +00:00:14,000 --> 00:00:15,000 +and it was working, right? + +9 +00:00:15,000 --> 00:00:18,000 +But also the amount of time it takes, + +10 +00:00:18,000 --> 00:00:19,000 +basically the time complexity + +11 +00:00:19,000 --> 00:00:22,000 +of the bubble sort is n squared. + +12 +00:00:22,000 --> 00:00:23,000 +Now that's not the main issue. + +13 +00:00:23,000 --> 00:00:25,000 +Of course, that's a main issue, but then in this video, + +14 +00:00:25,000 --> 00:00:29,000 +we are not going to solve that quadratic time complexity, + +15 +00:00:29,000 --> 00:00:32,000 +but at least we can reduce the number of steps. + +16 +00:00:32,000 --> 00:00:34,000 +Now in bubble sort, we are going from start + +17 +00:00:34,000 --> 00:00:36,000 +to end multiple times. + +18 +00:00:36,000 --> 00:00:37,000 +That's not the big issue. + +19 +00:00:37,000 --> 00:00:39,000 +The big issue is swapping. + +20 +00:00:39,000 --> 00:00:41,000 +Because every time in the inner loop, + +21 +00:00:41,000 --> 00:00:43,000 +we were doing this swapping multiple times + +22 +00:00:43,000 --> 00:00:46,000 +and it consumes a lot of time and memory. + +23 +00:00:46,000 --> 00:00:48,000 +So what if we can reduce that? + +24 +00:00:48,000 --> 00:00:51,000 +And that's where selection sort comes into picture. + +25 +00:00:51,000 --> 00:00:54,000 +Now what we do in selection sort is we don't actually swap + +26 +00:00:54,000 --> 00:00:57,000 +every time when you compare to values, + +27 +00:00:57,000 --> 00:00:59,000 +but what you do is from the entire + +28 +00:00:59,000 --> 00:01:02,000 +or the least, you find the minimum + +29 +00:01:02,000 --> 00:01:03,000 +or the maximum value depending upon + +30 +00:01:03,000 --> 00:01:05,000 +how you want to implement it. + +31 +00:01:05,000 --> 00:01:07,000 +So let's say we go, we are going for the minimum value. + +32 +00:01:07,000 --> 00:01:10,000 +So what you do is from the entire array, + +33 +00:01:10,000 --> 00:01:12,000 +you find the minimum value + +34 +00:01:12,000 --> 00:01:13,000 +and then you make sure + +35 +00:01:13,000 --> 00:01:15,000 +that the minimum value stays at the start. + +36 +00:01:15,000 --> 00:01:18,000 +Now once that is done, now that is your sorted part. + +37 +00:01:18,000 --> 00:01:21,000 +So from the entire list you create two sections, + +38 +00:01:21,000 --> 00:01:23,000 +the sorted section and the unsorted section. + +39 +00:01:23,000 --> 00:01:27,000 +So once you find two that goes into this sorted section, + +40 +00:01:27,000 --> 00:01:30,000 +which is at the start, and then you scan more values. + +41 +00:01:30,000 --> 00:01:32,000 +Now let's say when you scan, you find, + +42 +00:01:32,000 --> 00:01:34,000 +hey, we have three, which is the minimum + +43 +00:01:34,000 --> 00:01:36,000 +value in the unsorted data. + +44 +00:01:36,000 --> 00:01:37,000 +Then what you do is you'll pick up that three + +45 +00:01:37,000 --> 00:01:40,000 +and replace it with the second location. + +46 +00:01:40,000 --> 00:01:42,000 +Now what happens to the value of the second location? + +47 +00:01:42,000 --> 00:01:46,000 +Now that will be swapped where the value of three was. + +48 +00:01:46,000 --> 00:01:48,000 +So what you're doing here is you are swapping + +49 +00:01:48,000 --> 00:01:50,000 +by selecting the minimum value. + +50 +00:01:50,000 --> 00:01:53,000 +So when you say selecting, that's your selection sort. + +51 +00:01:53,000 --> 00:01:56,000 +Now what it does is you are not basically swapping value + +52 +00:01:56,000 --> 00:01:57,000 +inside the inner loop. + +53 +00:01:57,000 --> 00:02:00,000 +You are doing that in the outer loop, + +54 +00:02:00,000 --> 00:02:01,000 +which means the number + +55 +00:02:01,000 --> 00:02:04,000 +of swapping in a code will reduce. + +56 +00:02:04,000 --> 00:02:06,000 +Now to understand this more, let's look an example. + +57 +00:02:06,000 --> 00:02:08,000 +So let's say we have six values here, + +58 +00:02:08,000 --> 00:02:12,000 +which is 6, 5, 2, 8, 3, 7, and we want to sort them. + +59 +00:02:12,000 --> 00:02:13,000 +So how will you do it? + +60 +00:02:13,000 --> 00:02:15,000 +First of all, going from all the values. + +61 +00:02:15,000 --> 00:02:17,000 +Now of course as I mentioned before, + +62 +00:02:17,000 --> 00:02:18,000 +you can go for the minimum value + +63 +00:02:18,000 --> 00:02:20,000 +or you can go for the maximum value. + +64 +00:02:20,000 --> 00:02:21,000 +So let's go for maximum value. + +65 +00:02:21,000 --> 00:02:24,000 +Okay, so ultimately you will get the same output, + +66 +00:02:24,000 --> 00:02:26,000 +but the way you do do the code will be different. + +67 +00:02:26,000 --> 00:02:27,000 +So let's say we are going from a maximum value. + +68 +00:02:27,000 --> 00:02:29,000 +Now from all these values, + +69 +00:02:29,000 --> 00:02:32,000 +we have to find the maximum value. + +70 +00:02:32,000 --> 00:02:35,000 +Now, of course, at the start we don't have any idea + +71 +00:02:35,000 --> 00:02:36,000 +which values are maximum. + +72 +00:02:36,000 --> 00:02:39,000 +So let's say you make six as the maximum value + +73 +00:02:39,000 --> 00:02:41,000 +because at this point we don't know, right? + +74 +00:02:41,000 --> 00:02:45,000 +So let's assume that six is the biggest value + +75 +00:02:45,000 --> 00:02:46,000 +from all these values. + +76 +00:02:46,000 --> 00:02:50,000 +And then you start comparing six with all the other values. + +77 +00:02:50,000 --> 00:02:52,000 +So you will compare six with five. + +78 +00:02:52,000 --> 00:02:56,000 +If six is still bigger, no issue, go with the next value. + +79 +00:02:56,000 --> 00:02:59,000 +If six is still bigger, no issue, go with the next value. + +80 +00:02:59,000 --> 00:03:01,000 +Now at this point we are comparing six with eight + +81 +00:03:01,000 --> 00:03:05,000 +and we know now eight is the biggest value. + +82 +00:03:05,000 --> 00:03:08,000 +So now what you do is you change your variable + +83 +00:03:08,000 --> 00:03:12,000 +or you change your biggest value from six to eight, + +84 +00:03:12,000 --> 00:03:14,000 +and then you start comparing eight + +85 +00:03:14,000 --> 00:03:16,000 +with the other remaining value, which is three and seven. + +86 +00:03:16,000 --> 00:03:19,000 +So you compare with three, eight is still the big biggest. + +87 +00:03:19,000 --> 00:03:23,000 +You compare it with seven, eight is still the biggest. + +88 +00:03:23,000 --> 00:03:25,000 +Now once you go through all the values, + +89 +00:03:25,000 --> 00:03:28,000 +now you know the eight is the biggest value. + +90 +00:03:28,000 --> 00:03:32,000 +So what you do is you make sure that eight goes at the end. + +91 +00:03:32,000 --> 00:03:34,000 +Now, if you're doing this with for the minimum value, + +92 +00:03:34,000 --> 00:03:37,000 +what you do is you take the value two, + +93 +00:03:37,000 --> 00:03:39,000 +which is a minimum value and you put that at the start. + +94 +00:03:39,000 --> 00:03:41,000 +But since we are going for a different approach + +95 +00:03:41,000 --> 00:03:43,000 +where you're finding the biggest value, + +96 +00:03:43,000 --> 00:03:47,000 +you will make sure that eight goes at the end. + +97 +00:03:47,000 --> 00:03:51,000 +Now, when you move eight to the end, what happens to seven? + +98 +00:03:51,000 --> 00:03:53,000 +Now seven will say, "Okay, I don't have a place." + +99 +00:03:53,000 --> 00:03:54,000 +So eight will say, "Don't worry, + +100 +00:03:54,000 --> 00:03:56,000 +I will come to your place, + +101 +00:03:56,000 --> 00:03:57,000 +you come to my place." + +102 +00:03:57,000 --> 00:03:59,000 +That is swapping of the values. + +103 +00:03:59,000 --> 00:04:01,000 +But if you remember, we are not doing it + +104 +00:04:01,000 --> 00:04:03,000 +in every iteration of the inner loop, + +105 +00:04:03,000 --> 00:04:05,000 +we are doing that in the outer loop. + +106 +00:04:05,000 --> 00:04:07,000 +So once you complete one pass, + +107 +00:04:07,000 --> 00:04:09,000 +that's where you do the swapping. + +108 +00:04:09,000 --> 00:04:12,000 +So only once for the entire iteration. + +109 +00:04:12,000 --> 00:04:14,000 +Okay, now by doing this, we have made sure + +110 +00:04:14,000 --> 00:04:17,000 +that you have the biggest value at the end + +111 +00:04:17,000 --> 00:04:20,000 +and you've got a sorted at the end, which is the eight. + +112 +00:04:20,000 --> 00:04:23,000 +And then the first five values are still unsorted. + +113 +00:04:23,000 --> 00:04:25,000 +So what you do is you repeat the steps. + +114 +00:04:25,000 --> 00:04:25,000 +Again, start with six. + +115 +00:04:25,000 --> 00:04:28,000 +Now we'll say, okay, six is the biggest value, + +116 +00:04:28,000 --> 00:04:29,000 +let's assume that. + +117 +00:04:29,000 --> 00:04:31,000 +And then you start comparing six with five. + +118 +00:04:31,000 --> 00:04:33,000 +If it is a biggest value, six is still bigger, + +119 +00:04:33,000 --> 00:04:35,000 +no interchange the value of biggest value. + +120 +00:04:35,000 --> 00:04:37,000 +Then you compare it with two, no interchange. + +121 +00:04:37,000 --> 00:04:40,000 +But then when seven comes to the picture, + +122 +00:04:40,000 --> 00:04:43,000 +seven says, "Hey, you know, I'm bigger than you." + +123 +00:04:43,000 --> 00:04:45,000 +So six says, "Okay, you're the biggest value." + +124 +00:04:45,000 --> 00:04:47,000 +So now the current value will be seven. + +125 +00:04:47,000 --> 00:04:48,000 +The biggest value is seven. + +126 +00:04:48,000 --> 00:04:51,000 +Now again, we have to compare seven with three. + +127 +00:04:51,000 --> 00:04:53,000 +Now seven is still the biggest. + +128 +00:04:53,000 --> 00:04:55,000 +So we have to make sure that after all the situation, + +129 +00:04:55,000 --> 00:05:00,000 +we move seven at the place of three. + +130 +00:05:00,000 --> 00:05:01,000 +And that's what we are going to do. + +131 +00:05:01,000 --> 00:05:03,000 +Now by doing the second iteration, + +132 +00:05:03,000 --> 00:05:05,000 +so we have completed the second iteration + +133 +00:05:05,000 --> 00:05:07,000 +in which you got two values, + +134 +00:05:07,000 --> 00:05:09,000 +seven and eight at the end, + +135 +00:05:09,000 --> 00:05:10,000 +and the first four values are same. + +136 +00:05:10,000 --> 00:05:12,000 +So again, you do the same thing. + +137 +00:05:12,000 --> 00:05:12,000 +Next situation. + +138 +00:05:12,000 --> 00:05:15,000 +So you have to do this till you complete all the sorting. + +139 +00:05:15,000 --> 00:05:17,000 +So you again go with six and say, + +140 +00:05:17,000 --> 00:05:19,000 +"Hey, you know I'm the biggest now." + +141 +00:05:19,000 --> 00:05:20,000 +Let's compare six with five. + +142 +00:05:20,000 --> 00:05:23,000 +So still the biggest compare with two, + +143 +00:05:23,000 --> 00:05:26,000 +still the biggest, compare with three, still the biggest. + +144 +00:05:26,000 --> 00:05:30,000 +Now once you complete that unsorted iteration, + +145 +00:05:30,000 --> 00:05:33,000 +you'll make sure that you move your six to the location + +146 +00:05:33,000 --> 00:05:35,000 +of three because that's where the six should be. + +147 +00:05:35,000 --> 00:05:37,000 +So it'll swap with three. + +148 +00:05:37,000 --> 00:05:41,000 +And then the last three values 6, 7, 8 are sorted. + +149 +00:05:41,000 --> 00:05:43,000 +The first three values are not sorted. + +150 +00:05:43,000 --> 00:05:44,000 +So again, if you observe, + +151 +00:05:44,000 --> 00:05:46,000 +what we are doing is we are making sure + +152 +00:05:46,000 --> 00:05:49,000 +that you got two different sections of sorted values + +153 +00:05:49,000 --> 00:05:50,000 +and unsorted values. + +154 +00:05:50,000 --> 00:05:53,000 +Now goes for the first three values. + +155 +00:05:53,000 --> 00:05:55,000 +So three, let's assume three is the biggest + +156 +00:05:55,000 --> 00:05:57,000 +and then compared with five. + +157 +00:05:57,000 --> 00:05:59,000 +Now we know that five is the biggest of all, + +158 +00:05:59,000 --> 00:06:01,000 +or at least bigger than three. + +159 +00:06:01,000 --> 00:06:05,000 +And then you compare, you make the bigger value as five. + +160 +00:06:05,000 --> 00:06:06,000 +Now three is gone. + +161 +00:06:06,000 --> 00:06:08,000 +Then you compare five with two. + +162 +00:06:08,000 --> 00:06:10,000 +Still five is biggest. + +163 +00:06:10,000 --> 00:06:12,000 +And now we know that five need to go + +164 +00:06:12,000 --> 00:06:14,000 +at the location of two, so you have to swap it. + +165 +00:06:14,000 --> 00:06:16,000 +Now comes the first two values. + +166 +00:06:16,000 --> 00:06:17,000 +Again, the same thing. + +167 +00:06:17,000 --> 00:06:20,000 +You will keep doing this till you reach to the end + +168 +00:06:20,000 --> 00:06:23,000 +of your array and you move to three. + +169 +00:06:23,000 --> 00:06:26,000 +And since we only have one value, don't need to sort it. + +170 +00:06:26,000 --> 00:06:27,000 +That's already sorted. + +171 +00:06:27,000 --> 00:06:28,000 +And by doing this, + +172 +00:06:28,000 --> 00:06:30,000 +you basically got a sorted array. + +173 +00:06:30,000 --> 00:06:32,000 +Now again, compared to the bubble sort, + +174 +00:06:32,000 --> 00:06:34,000 +we are still going from start to end. + +175 +00:06:34,000 --> 00:06:36,000 +You have an outer loop, you have an inner loop, + +176 +00:06:36,000 --> 00:06:38,000 +but we are doing swapping only once + +177 +00:06:38,000 --> 00:06:42,000 +in every big iteration or in every pass. + +178 +00:06:42,000 --> 00:06:45,000 +So that's the advantage of using selection sort + +179 +00:06:45,000 --> 00:06:47,000 +or the bubble sort. + +180 +00:06:47,000 --> 00:06:49,000 +About the overall, the time comparability + +181 +00:06:49,000 --> 00:06:51,000 +is still O of n squared, + +182 +00:06:51,000 --> 00:06:54,000 +but at least it is better than the bubble sort + +183 +00:06:54,000 --> 00:06:55,000 +in terms of swapping. + +184 +00:06:55,000 --> 00:06:57,000 +Now question, how you're going to implement this? + +185 +00:06:57,000 --> 00:06:59,000 +Let's see that in the next video. + diff --git a/28 - DSA (Optional)/009 Selection Sort Code_en.srt b/28 - DSA (Optional)/009 Selection Sort Code_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..f25b4a0daf39c90f9c6d5dd3ef5a3fe54c6b7b24 --- /dev/null +++ b/28 - DSA (Optional)/009 Selection Sort Code_en.srt @@ -0,0 +1,724 @@ +1 +00:00:00,000 --> 00:00:04,000 +-: Now it's time to implement selection sort in Java. + +2 +00:00:04,000 --> 00:00:05,000 +So in the previous video + +3 +00:00:05,000 --> 00:00:07,000 +we have talked about the bubble sort. + +4 +00:00:07,000 --> 00:00:09,000 +And this is how it works. + +5 +00:00:09,000 --> 00:00:12,000 +Now, one thing to remember, in the inner loop, + +6 +00:00:12,000 --> 00:00:13,000 +and you see this loop? + +7 +00:00:13,000 --> 00:00:15,000 +In this loop, we are basically doing the swapping, + +8 +00:00:15,000 --> 00:00:18,000 +so it increases the time as well. + +9 +00:00:18,000 --> 00:00:21,000 +So in selection, what you do is you basically swap this, + +10 +00:00:21,000 --> 00:00:22,000 +of course you do that swapping, + +11 +00:00:22,000 --> 00:00:25,000 +but outside the inner loop, somewhere here. + +12 +00:00:25,000 --> 00:00:27,000 +So it reduces the number of swap you're doing. + +13 +00:00:27,000 --> 00:00:28,000 +How will you do it? + +14 +00:00:28,000 --> 00:00:30,000 +So what I'll do is I will just remove the entire code + +15 +00:00:30,000 --> 00:00:32,000 +from here because this is the bubble sort. + +16 +00:00:32,000 --> 00:00:34,000 +I don't need this. + +17 +00:00:34,000 --> 00:00:37,000 +And what we have here is we have the array, + +18 +00:00:37,000 --> 00:00:39,000 +which has six elements. + +19 +00:00:39,000 --> 00:00:41,000 +We've got a size variable where we are storing the length + +20 +00:00:41,000 --> 00:00:43,000 +of an array, and then we also have + +21 +00:00:43,000 --> 00:00:44,000 +a temp variable for swapping. + +22 +00:00:44,000 --> 00:00:47,000 +Then we are printing the array before sorting, + +23 +00:00:47,000 --> 00:00:50,000 +and we are printing the array after sorting. + +24 +00:00:50,000 --> 00:00:53,000 +So if you run this, you will get the values + +25 +00:00:53,000 --> 00:00:54,000 +before sorting, after sorting, + +26 +00:00:54,000 --> 00:00:56,000 +but at this point they're same, + +27 +00:00:56,000 --> 00:00:59,000 +is because the main logic of sorting is missing here. + +28 +00:00:59,000 --> 00:01:03,000 +So let's write the logic for selection sort here. + +29 +00:01:03,000 --> 00:01:05,000 +The thing is, in the theory we have said that we'll go + +30 +00:01:05,000 --> 00:01:08,000 +for the maximum value, the biggest value to swap, + +31 +00:01:08,000 --> 00:01:11,000 +or we'll put the biggest value at the end. + +32 +00:01:11,000 --> 00:01:12,000 +Let's say in the practical, + +33 +00:01:12,000 --> 00:01:14,000 +let's try to implement with the smallest value. + +34 +00:01:14,000 --> 00:01:16,000 +And we have seen both the approach then. + +35 +00:01:16,000 --> 00:01:19,000 +So what we are going to do is we'll look at the entire array + +36 +00:01:19,000 --> 00:01:21,000 +and then we'll find the smallest value, which is two. + +37 +00:01:21,000 --> 00:01:24,000 +Of course, you have to iterate between all the values. + +38 +00:01:24,000 --> 00:01:26,000 +And then once you've got the smallest value, + +39 +00:01:26,000 --> 00:01:28,000 +you will swap the six with two, + +40 +00:01:28,000 --> 00:01:30,000 +because two should be the first value. + +41 +00:01:30,000 --> 00:01:32,000 +And that's how we can start. + +42 +00:01:32,000 --> 00:01:34,000 +And for doing that, we have to use two loops, + +43 +00:01:34,000 --> 00:01:35,000 +the outer loop and inner loop. + +44 +00:01:35,000 --> 00:01:37,000 +We'll start outer loop with zero, + +45 +00:01:37,000 --> 00:01:41,000 +and then we'll say i less than size. + +46 +00:01:41,000 --> 00:01:43,000 +So we'll say i plus plus. + +47 +00:01:43,000 --> 00:01:45,000 +But also we can make it more efficient by saying + +48 +00:01:45,000 --> 00:01:47,000 +that size minus one is + +49 +00:01:47,000 --> 00:01:49,000 +because if you remember when we were doing the sorting + +50 +00:01:49,000 --> 00:01:52,000 +and once you got all the values sorted + +51 +00:01:52,000 --> 00:01:54,000 +except the first value or the last value, + +52 +00:01:54,000 --> 00:01:57,000 +you don't basically sort the one value. + +53 +00:01:57,000 --> 00:02:00,000 +So you can reduce the number of iterations you go for. + +54 +00:02:00,000 --> 00:02:01,000 +So we can say size one is one, + +55 +00:02:01,000 --> 00:02:03,000 +and we'll see if that works or not. + +56 +00:02:03,000 --> 00:02:06,000 +And if it works, then we are happy. + +57 +00:02:06,000 --> 00:02:07,000 +We'll also go for the inner loop. + +58 +00:02:07,000 --> 00:02:10,000 +Now for the inner loop, we'll go for j equal to. + +59 +00:02:10,000 --> 00:02:11,000 +Now where you are going to start j? + +60 +00:02:11,000 --> 00:02:14,000 +So basically if you are putting the starting, + +61 +00:02:14,000 --> 00:02:16,000 +the biggest value or the smallest value at the start, + +62 +00:02:16,000 --> 00:02:21,000 +what it means is after for every iteration the inner loop, + +63 +00:02:21,000 --> 00:02:23,000 +remember we can skip the first value. + +64 +00:02:23,000 --> 00:02:26,000 +So for the second iteration, we can skip the first value. + +65 +00:02:26,000 --> 00:02:29,000 +For the third iteration, we can skip the first two values + +66 +00:02:29,000 --> 00:02:30,000 +because they're sorted. + +67 +00:02:30,000 --> 00:02:32,000 +So we have two sections, sorted array and unsorted array. + +68 +00:02:32,000 --> 00:02:36,000 +So here we can start with j equal to i. + +69 +00:02:36,000 --> 00:02:38,000 +Yeah, in fact, when your i value is zero, + +70 +00:02:38,000 --> 00:02:39,000 +j should start from zero. + +71 +00:02:39,000 --> 00:02:42,000 +So we can say j equal to i + +72 +00:02:42,000 --> 00:02:47,000 +and j less than size and j plus plus. + +73 +00:02:47,000 --> 00:02:50,000 +So basically we have to go to the end. + +74 +00:02:50,000 --> 00:02:52,000 +So that's the for loop we have. + +75 +00:02:52,000 --> 00:02:53,000 +Now what you do inside this? + +76 +00:02:53,000 --> 00:02:55,000 +So we have to find the minimum value. + +77 +00:02:55,000 --> 00:02:58,000 +To start with, we can take min value variable here. + +78 +00:02:58,000 --> 00:03:03,000 +So I can int min is equal to zero. + +79 +00:03:03,000 --> 00:03:06,000 +In fact, we want the minIndex, not the min value + +80 +00:03:06,000 --> 00:03:07,000 +because we have to work with the index. + +81 +00:03:07,000 --> 00:03:11,000 +And we'll say the index is by default minus one. + +82 +00:03:11,000 --> 00:03:12,000 +And here, to start with, + +83 +00:03:12,000 --> 00:03:15,000 +we'll start with minIndex equal to i. + +84 +00:03:15,000 --> 00:03:18,000 +And then what you do, you basically check if the array, + +85 +00:03:18,000 --> 00:03:21,000 +which is nums, of minIndex. + +86 +00:03:21,000 --> 00:03:24,000 +Now we want this to be smallest value. + +87 +00:03:24,000 --> 00:03:26,000 +So I assume that this is the smallest, + +88 +00:03:26,000 --> 00:03:29,000 +but what if this is not the smallest compared + +89 +00:03:29,000 --> 00:03:31,000 +to the value which we are considering? + +90 +00:03:31,000 --> 00:03:34,000 +The value which is considering here is nums of j. + +91 +00:03:34,000 --> 00:03:37,000 +So example, if this six is the smallest, + +92 +00:03:37,000 --> 00:03:38,000 +which we are thinking, + +93 +00:03:38,000 --> 00:03:41,000 +in fact, we should be saying i plus one. + +94 +00:03:41,000 --> 00:03:43,000 +The reason being, when you are saying + +95 +00:03:43,000 --> 00:03:46,000 +that six is the minimum value, we are assuming that, + +96 +00:03:46,000 --> 00:03:47,000 +and then we are comparing with five. + +97 +00:03:47,000 --> 00:03:51,000 +Now you will get five when you say j starts with i plus one. + +98 +00:03:51,000 --> 00:03:54,000 +Because in the first iteration, the value of i will be zero. + +99 +00:03:54,000 --> 00:03:55,000 +And this five is one. + +100 +00:03:55,000 --> 00:03:58,000 +So we'll compare these two values. + +101 +00:03:58,000 --> 00:04:01,000 +If six is smaller than five, then everything works. + +102 +00:04:01,000 --> 00:04:03,000 +But what if six is greater than five? + +103 +00:04:03,000 --> 00:04:05,000 +In that case, we have to swap. + +104 +00:04:05,000 --> 00:04:06,000 +Now, we don't have to swap, + +105 +00:04:06,000 --> 00:04:10,000 +we basically have to change the minIndex to j + +106 +00:04:10,000 --> 00:04:13,000 +because the new index we will get is j. + +107 +00:04:13,000 --> 00:04:15,000 +So the new index here is one. + +108 +00:04:16,000 --> 00:04:17,000 +So that's the minIndex. + +109 +00:04:17,000 --> 00:04:19,000 +So what we are doing by doing this is + +110 +00:04:19,000 --> 00:04:23,000 +after the entire iteration, you will get the minimum + +111 +00:04:23,000 --> 00:04:24,000 +value from this array. + +112 +00:04:24,000 --> 00:04:27,000 +So that means after this iteration, + +113 +00:04:27,000 --> 00:04:30,000 +you will know that two is the minimum value. + +114 +00:04:30,000 --> 00:04:31,000 +Now, once you know the minimum value, + +115 +00:04:31,000 --> 00:04:34,000 +what you do is you swap two with six. + +116 +00:04:34,000 --> 00:04:35,000 +It's that simple. + +117 +00:04:35,000 --> 00:04:37,000 +How do you swap two and six? + +118 +00:04:37,000 --> 00:04:39,000 +How do you know we have to swap six? + +119 +00:04:39,000 --> 00:04:41,000 +Because i is referring to six + +120 +00:04:41,000 --> 00:04:43,000 +and the minIndex is referring to two. + +121 +00:04:43,000 --> 00:04:45,000 +So we can simply swap them. + +122 +00:04:45,000 --> 00:04:46,000 +So we can use a temp variable here + +123 +00:04:46,000 --> 00:04:50,000 +and we can put nums of minIndex + +124 +00:04:50,000 --> 00:04:55,000 +and we can say nums of minIndex is equal to nums of i. + +125 +00:04:56,000 --> 00:05:00,000 +And nums of i is equal to temp. + +126 +00:05:00,000 --> 00:05:02,000 +So basically we just have to swap them. + +127 +00:05:02,000 --> 00:05:05,000 +So what we are doing is in the inner loop, + +128 +00:05:05,000 --> 00:05:07,000 +we are just finding the minimum value, + +129 +00:05:07,000 --> 00:05:09,000 +and then once you find it, you simply swap it + +130 +00:05:09,000 --> 00:05:11,000 +with the min value which you're considering, + +131 +00:05:11,000 --> 00:05:12,000 +which is the first value. + +132 +00:05:12,000 --> 00:05:15,000 +And as the i value goes up, + +133 +00:05:15,000 --> 00:05:16,000 +first you will compare with this, + +134 +00:05:16,000 --> 00:05:18,000 +then you compare with the five + +135 +00:05:18,000 --> 00:05:20,000 +and you compare, then you make two as a minimum + +136 +00:05:20,000 --> 00:05:21,000 +after of course, after the swapping. + +137 +00:05:21,000 --> 00:05:24,000 +And what I will do is I will also print this + +138 +00:05:24,000 --> 00:05:28,000 +values after each iteration so I can just copy this + +139 +00:05:28,000 --> 00:05:31,000 +and paste it here. + +140 +00:05:31,000 --> 00:05:35,000 +And we also print a new line to get it on the new line. + +141 +00:05:35,000 --> 00:05:38,000 +So now if you done this, let's see what happens. + +142 +00:05:38,000 --> 00:05:40,000 +So you can see if this is, + +143 +00:05:40,000 --> 00:05:44,000 +I will, after printing this also, I will just say new line + +144 +00:05:44,000 --> 00:05:47,000 +or maybe I will just keep this new line + +145 +00:05:47,000 --> 00:05:51,000 +before the for loop printing. + +146 +00:05:51,000 --> 00:05:52,000 +Okay, let's run. + +147 +00:05:52,000 --> 00:05:55,000 +And you can see this is before swapping + +148 +00:05:55,000 --> 00:05:57,000 +and this is after swapping. + +149 +00:05:57,000 --> 00:05:58,000 +So let's compare the value. + +150 +00:05:58,000 --> 00:06:00,000 +Is it properly sorted? + +151 +00:06:00,000 --> 00:06:04,000 +Yes, two, four, five, six, eight, nine. + +152 +00:06:04,000 --> 00:06:05,000 +Those are the values we have. + +153 +00:06:05,000 --> 00:06:07,000 +And then see the steps. + +154 +00:06:07,000 --> 00:06:09,000 +In the first iteration before swapping, + +155 +00:06:09,000 --> 00:06:11,000 +so this is the first iteration. + +156 +00:06:11,000 --> 00:06:13,000 +What you do is you know that two is a minimum value. + +157 +00:06:13,000 --> 00:06:15,000 +So you swap six with two and that's done. + +158 +00:06:15,000 --> 00:06:19,000 +Then in the second iteration you find the minimum value. + +159 +00:06:19,000 --> 00:06:21,000 +So the minimum value from this is four. + +160 +00:06:21,000 --> 00:06:24,000 +So you swap four with five and that is done here. + +161 +00:06:24,000 --> 00:06:26,000 +Then you consider six. + +162 +00:06:26,000 --> 00:06:29,000 +Then you say, you find the minimum value from this, + +163 +00:06:29,000 --> 00:06:31,000 +which is five, you'll swap five with six. + +164 +00:06:31,000 --> 00:06:33,000 +So in this, you'll say six is the minimum, + +165 +00:06:33,000 --> 00:06:35,000 +so you'll swap it with eight. + +166 +00:06:35,000 --> 00:06:38,000 +Comparing this two, this is eight and nine. + +167 +00:06:38,000 --> 00:06:40,000 +So you'll swap eight with nine. + +168 +00:06:40,000 --> 00:06:41,000 +And in last value, + +169 +00:06:41,000 --> 00:06:43,000 +you don't have to even sort it because it's always sorted. + +170 +00:06:43,000 --> 00:06:44,000 +And that's what we are doing here, + +171 +00:06:44,000 --> 00:06:46,000 +and that's why we say it's size minus one. + +172 +00:06:46,000 --> 00:06:48,000 +We don't even sort here. + +173 +00:06:48,000 --> 00:06:50,000 +In fact, if you don't put this minus one, + +174 +00:06:50,000 --> 00:06:52,000 +what will happen is, if you run this code, + +175 +00:06:52,000 --> 00:06:55,000 +you will get one iteration and there's no change there. + +176 +00:06:55,000 --> 00:06:57,000 +It doesn't matter how many values you have, + +177 +00:06:57,000 --> 00:06:59,000 +you're not going to have any change in the large iteration. + +178 +00:06:59,000 --> 00:07:02,000 +So you simply save it by saying minus one here. + +179 +00:07:02,000 --> 00:07:04,000 +So yeah, that's how we use selection sort. + +180 +00:07:04,000 --> 00:07:05,000 +Basically you find the minimum value + +181 +00:07:05,000 --> 00:07:07,000 +and swap it with the location where it should be. + diff --git a/28 - DSA (Optional)/010 Insertion Sort_en.srt b/28 - DSA (Optional)/010 Insertion Sort_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..7cebfb27f8d4d7d87fd520063ea3cbb90f3bf035 --- /dev/null +++ b/28 - DSA (Optional)/010 Insertion Sort_en.srt @@ -0,0 +1,804 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: So we have talked about different sorting techniques. + +2 +00:00:02,000 --> 00:00:05,000 +We have talked about Bubble Sort and Selection Sort. + +3 +00:00:05,000 --> 00:00:07,000 +In this video, we'll talk about Insertion Sort. + +4 +00:00:07,000 --> 00:00:09,000 +So what you do in this technique, + +5 +00:00:09,000 --> 00:00:10,000 +is basically you take the elements + +6 +00:00:10,000 --> 00:00:14,000 +and then you put the elements at the right position. + +7 +00:00:14,000 --> 00:00:15,000 +That is your insertion. + +8 +00:00:15,000 --> 00:00:17,000 +So you have to put at the right location. + +9 +00:00:17,000 --> 00:00:20,000 +Now, in this, you don't normally do swapping. + +10 +00:00:20,000 --> 00:00:23,000 +Yes, we do swapping, but we don't use word swapping, + +11 +00:00:23,000 --> 00:00:26,000 +what you use is something called shifting. + +12 +00:00:26,000 --> 00:00:27,000 +Okay? + +13 +00:00:27,000 --> 00:00:30,000 +So example, if you're playing cards. + +14 +00:00:30,000 --> 00:00:33,000 +So if you have a random cards on your table. + +15 +00:00:33,000 --> 00:00:34,000 +So basically in this example, + +16 +00:00:34,000 --> 00:00:36,000 +you are not going to use cards. + +17 +00:00:36,000 --> 00:00:38,000 +We are going to use this characters. + +18 +00:00:38,000 --> 00:00:42,000 +I found these characters in my drawer. + +19 +00:00:42,000 --> 00:00:44,000 +I think I got it from my kids' birthday, + +20 +00:00:44,000 --> 00:00:45,000 +but it doesn't matter. + +21 +00:00:45,000 --> 00:00:48,000 +So I got this, and we'll try to sort this values here. + +22 +00:00:48,000 --> 00:00:50,000 +So let's say if you randomly get these values + +23 +00:00:50,000 --> 00:00:51,000 +and if you want to sort them, + +24 +00:00:51,000 --> 00:00:53,000 +what you do is you pick up the value + +25 +00:00:53,000 --> 00:00:55,000 +and then you, you know, + +26 +00:00:55,000 --> 00:00:57,000 +imagine these are cards, playing cards. + +27 +00:00:57,000 --> 00:00:58,000 +So what you do is you take the first value, + +28 +00:00:58,000 --> 00:01:01,000 +you take the second value, and then you keep it like this. + +29 +00:01:01,000 --> 00:01:02,000 +I don't know if how many of you play cards. + +30 +00:01:02,000 --> 00:01:03,000 +I used to love them. + +31 +00:01:03,000 --> 00:01:05,000 +Not for money, but I used to love them. + +32 +00:01:05,000 --> 00:01:08,000 +Then you take a next card, and then you put it before this. + +33 +00:01:08,000 --> 00:01:09,000 +Then you take a card. + +34 +00:01:09,000 --> 00:01:12,000 +Then you know that this card goes between A and O. + +35 +00:01:12,000 --> 00:01:13,000 +You put it here. + +36 +00:01:13,000 --> 00:01:15,000 +So this is called insertion, + +37 +00:01:15,000 --> 00:01:17,000 +but how exactly you're gonna do that, + +38 +00:01:17,000 --> 00:01:20,000 +that will see with example on this table + +39 +00:01:20,000 --> 00:01:22,000 +and with this cards. + +40 +00:01:22,000 --> 00:01:24,000 +So now let's sort this values. + +41 +00:01:24,000 --> 00:01:26,000 +Now these are not values, but characters. + +42 +00:01:26,000 --> 00:01:27,000 +So let's try. + +43 +00:01:27,000 --> 00:01:30,000 +So what we have here is we'll start with the first location. + +44 +00:01:30,000 --> 00:01:32,000 +As you can see on the screen, + +45 +00:01:32,000 --> 00:01:35,000 +we have different values here, and I'll pull this down a bit + +46 +00:01:35,000 --> 00:01:37,000 +so that we can have some place to keep this. + +47 +00:01:37,000 --> 00:01:39,000 +So the first value is O. + +48 +00:01:39,000 --> 00:01:43,000 +Now I'm assuming the O goes here, the first location, + +49 +00:01:43,000 --> 00:01:45,000 +because it's always sorted. + +50 +00:01:45,000 --> 00:01:49,000 +And then you take the next value, which is E, let's say. + +51 +00:01:49,000 --> 00:01:52,000 +Now we know that E comes before O. + +52 +00:01:52,000 --> 00:01:56,000 +So what we to do is, this need to go at the start. + +53 +00:01:56,000 --> 00:02:00,000 +So you have to move this to the next location + +54 +00:02:00,000 --> 00:02:02,000 +so that you'll have some space for E. + +55 +00:02:02,000 --> 00:02:05,000 +Now, the point to remember here is it's all about shifting. + +56 +00:02:05,000 --> 00:02:06,000 +Okay? + +57 +00:02:06,000 --> 00:02:08,000 +So, we don't use a word swapping here. + +58 +00:02:08,000 --> 00:02:09,000 +You'll understand that in some time. + +59 +00:02:09,000 --> 00:02:11,000 +Why don't we say swapping? + +60 +00:02:11,000 --> 00:02:14,000 +Let's go for the next one, which is S. + +61 +00:02:14,000 --> 00:02:16,000 +Now we know that S is greater than, + +62 +00:02:16,000 --> 00:02:18,000 +so we'll compare S with all the values. + +63 +00:02:18,000 --> 00:02:20,000 +So we'll compare S with E, okay? + +64 +00:02:20,000 --> 00:02:22,000 +It goes after that, it goes after O. + +65 +00:02:22,000 --> 00:02:24,000 +So S goes here, okay, + +66 +00:02:24,000 --> 00:02:26,000 +I'm not promoting any camera model here, + +67 +00:02:26,000 --> 00:02:28,000 +but that's the sequence. + +68 +00:02:28,000 --> 00:02:31,000 +Then you go for the next one, which is D. + +69 +00:02:31,000 --> 00:02:34,000 +Now D goes at the start. + +70 +00:02:34,000 --> 00:02:35,000 +So what you do is, + +71 +00:02:35,000 --> 00:02:38,000 +you basically have to move all the elements, right? + +72 +00:02:38,000 --> 00:02:40,000 +So if you compare with S here, + +73 +00:02:40,000 --> 00:02:43,000 +D is less than S. + +74 +00:02:44,000 --> 00:02:48,000 +So you have to shift S, you have to make some space for D. + +75 +00:02:48,000 --> 00:02:52,000 +Then you take O, and you move O here, + +76 +00:02:52,000 --> 00:02:54,000 +so that you can make some space for D. + +77 +00:02:54,000 --> 00:02:56,000 +Then you compare D with E, + +78 +00:02:56,000 --> 00:02:57,000 +and you have to make some space. + +79 +00:02:57,000 --> 00:03:00,000 +Of course, you have to swap, you have to move this. + +80 +00:03:00,000 --> 00:03:04,000 +You'll make some space for D and D goes here, right? + +81 +00:03:04,000 --> 00:03:05,000 +So now it is in sequence. + +82 +00:03:05,000 --> 00:03:07,000 +Then what about the last value, which is A? + +83 +00:03:07,000 --> 00:03:09,000 +Now A, we all know we have to, + +84 +00:03:09,000 --> 00:03:11,000 +I mean, of course it goes at the start, + +85 +00:03:11,000 --> 00:03:13,000 +but we have to compare from start or last. + +86 +00:03:13,000 --> 00:03:16,000 +So this goes here, which is S. + +87 +00:03:16,000 --> 00:03:17,000 +If it is less than that, + +88 +00:03:17,000 --> 00:03:20,000 +we'll shift this to make space for A, + +89 +00:03:20,000 --> 00:03:23,000 +but then we have to also check with O. + +90 +00:03:23,000 --> 00:03:24,000 +This also have to shift. + +91 +00:03:24,000 --> 00:03:25,000 +We check with E. + +92 +00:03:25,000 --> 00:03:27,000 +This also have to shift. + +93 +00:03:27,000 --> 00:03:29,000 +We compare with D. + +94 +00:03:29,000 --> 00:03:30,000 +This also has to shift, + +95 +00:03:30,000 --> 00:03:32,000 +and then A goes at the start. + +96 +00:03:32,000 --> 00:03:33,000 +By doing this, what you're doing + +97 +00:03:33,000 --> 00:03:36,000 +is you're basically inserting value at that location. + +98 +00:03:36,000 --> 00:03:39,000 +So this is how you do the insertion sort. + +99 +00:03:39,000 --> 00:03:41,000 +Now let's say this was, + +100 +00:03:41,000 --> 00:03:42,000 +randomly we were picking values, right? + +101 +00:03:42,000 --> 00:03:46,000 +But what if you have things already in the alley? + +102 +00:03:46,000 --> 00:03:48,000 +So what we'll do is we'll keep it here, + +103 +00:03:48,000 --> 00:03:50,000 +we'll keep this here, + +104 +00:03:50,000 --> 00:03:53,000 +we'll keep this here, this goes here, + +105 +00:03:53,000 --> 00:03:54,000 +and let's say this goes here. + +106 +00:03:54,000 --> 00:03:59,000 +In fact, I will also move this, okay? + +107 +00:03:59,000 --> 00:04:01,000 +I'm not trying to make any city name, + +108 +00:04:01,000 --> 00:04:02,000 +but let's say we have this sequence, + +109 +00:04:02,000 --> 00:04:05,000 +we have these five values, and now you want to sort them. + +110 +00:04:05,000 --> 00:04:07,000 +So what you do is, you don't start from the first value, + +111 +00:04:07,000 --> 00:04:10,000 +you start with the second value. + +112 +00:04:10,000 --> 00:04:11,000 +And then because we are assuming + +113 +00:04:11,000 --> 00:04:13,000 +that the first value is always sorted, okay? + +114 +00:04:13,000 --> 00:04:15,000 +So what you do in this technique is, + +115 +00:04:15,000 --> 00:04:19,000 +basically you divide your list or add A into two segments. + +116 +00:04:19,000 --> 00:04:21,000 +This sorted and unsorted. + +117 +00:04:21,000 --> 00:04:23,000 +At this point all are unsorted. + +118 +00:04:23,000 --> 00:04:24,000 +Oh, so this is unsorted area. + +119 +00:04:24,000 --> 00:04:26,000 +But when you start, you have to assume + +120 +00:04:26,000 --> 00:04:28,000 +that your S is already sorted. + +121 +00:04:28,000 --> 00:04:29,000 +Then you start with E. + +122 +00:04:29,000 --> 00:04:32,000 +Then you see, hey, do we have E at the right position? + +123 +00:04:32,000 --> 00:04:36,000 +The way you do that is you compare E with S, + +124 +00:04:36,000 --> 00:04:39,000 +and you say, Hey, you know, E is not at the right position. + +125 +00:04:39,000 --> 00:04:40,000 +So you will take E out. + +126 +00:04:40,000 --> 00:04:44,000 +In programming what you do is you save the value of E + +127 +00:04:44,000 --> 00:04:46,000 +in the temporary location, okay? + +128 +00:04:46,000 --> 00:04:48,000 +So exactly, you're not moving E, + +129 +00:04:48,000 --> 00:04:50,000 +you're just making a copy of it. + +130 +00:04:50,000 --> 00:04:52,000 +Maybe there will be a copy of E here as well. + +131 +00:04:52,000 --> 00:04:54,000 +But you have extra copy with you + +132 +00:04:54,000 --> 00:04:55,000 +saved in a temporary variable. + +133 +00:04:55,000 --> 00:04:57,000 +So let's say this is a temporary space. + +134 +00:04:57,000 --> 00:05:02,000 +Then you move your S here, so that you can make space for E, + +135 +00:05:02,000 --> 00:05:04,000 +and that's sorted now. + +136 +00:05:04,000 --> 00:05:07,000 +Now if you see this two, it looks sorted. + +137 +00:05:07,000 --> 00:05:11,000 +Now what you do is you check E before, do we have any value? + +138 +00:05:11,000 --> 00:05:14,000 +So of course we don't have any value before E. + +139 +00:05:14,000 --> 00:05:15,000 +Now that is sorted. + +140 +00:05:15,000 --> 00:05:16,000 +Then you go for the next value. + +141 +00:05:16,000 --> 00:05:19,000 +So we started here in the next generation, + +142 +00:05:19,000 --> 00:05:20,000 +you basically start with O. + +143 +00:05:20,000 --> 00:05:24,000 +Now O will compare with the previous value, which is S. + +144 +00:05:24,000 --> 00:05:26,000 +Now is it the right position? + +145 +00:05:26,000 --> 00:05:27,000 +No. + +146 +00:05:27,000 --> 00:05:29,000 +So we have to copy O in the temporary variable + +147 +00:05:29,000 --> 00:05:33,000 +and move your S so that you can make some space for O and O. + +148 +00:05:33,000 --> 00:05:35,000 +Of course you can move O here. + +149 +00:05:35,000 --> 00:05:37,000 +You have to also check if O really goes here. + +150 +00:05:37,000 --> 00:05:41,000 +You have to compare O with the previous value, which is E. + +151 +00:05:41,000 --> 00:05:43,000 +And we know that O is greater than E, + +152 +00:05:43,000 --> 00:05:45,000 +so you can keep O here. + +153 +00:05:45,000 --> 00:05:48,000 +So that's how you basically sorted the third element. + +154 +00:05:48,000 --> 00:05:49,000 +Then you go for the next element, which is D. + +155 +00:05:49,000 --> 00:05:52,000 +So what you do is you compare D with S here, + +156 +00:05:52,000 --> 00:05:54,000 +and then you know that D is smaller than S, + +157 +00:05:54,000 --> 00:05:56,000 +or S is greater than D. + +158 +00:05:56,000 --> 00:05:57,000 +So what you do is you have to move. + +159 +00:05:57,000 --> 00:06:00,000 +So you take into temporary variable, move S here. + +160 +00:06:00,000 --> 00:06:03,000 +Of course, you don't move D directly there. + +161 +00:06:03,000 --> 00:06:04,000 +You have to compare D + +162 +00:06:04,000 --> 00:06:06,000 +with the other other elements, which is O. + +163 +00:06:06,000 --> 00:06:08,000 +Is it smaller than that? + +164 +00:06:08,000 --> 00:06:09,000 +Yes. + +165 +00:06:09,000 --> 00:06:10,000 +We have to move this here. + +166 +00:06:10,000 --> 00:06:12,000 +We have to make space for D, right? + +167 +00:06:12,000 --> 00:06:14,000 +Again, before that also we have one more element, + +168 +00:06:14,000 --> 00:06:17,000 +which is E, so you move E here. + +169 +00:06:17,000 --> 00:06:19,000 +It's all about shifting values. + +170 +00:06:19,000 --> 00:06:21,000 +And then your D goes here. + +171 +00:06:21,000 --> 00:06:24,000 +And if you compare, it looks sorted, right? + +172 +00:06:24,000 --> 00:06:27,000 +Compared to the other values, so it looks sorted, + +173 +00:06:27,000 --> 00:06:29,000 +but then you have to complete one more iteration. + +174 +00:06:29,000 --> 00:06:32,000 +So you take A, you compare A with S. + +175 +00:06:32,000 --> 00:06:34,000 +Now A here is compared with S. + +176 +00:06:34,000 --> 00:06:35,000 +So again, we have to shift. + +177 +00:06:35,000 --> 00:06:38,000 +So what you do is you take A at the temporary location, + +178 +00:06:38,000 --> 00:06:41,000 +then you move your S, + +179 +00:06:41,000 --> 00:06:43,000 +you will check A with O. + +180 +00:06:43,000 --> 00:06:45,000 +Again, you have to move O. + +181 +00:06:45,000 --> 00:06:47,000 +Then you check A with E, + +182 +00:06:47,000 --> 00:06:49,000 +again, you have to move E. + +183 +00:06:49,000 --> 00:06:52,000 +So remember, we have to only move + +184 +00:06:52,000 --> 00:06:55,000 +when you know that A will not be the right position. + +185 +00:06:55,000 --> 00:06:56,000 +And then you compare A with D. + +186 +00:06:56,000 --> 00:06:58,000 +Again, D has to move + +187 +00:06:58,000 --> 00:07:00,000 +so that you can make some space for A. + +188 +00:07:00,000 --> 00:07:03,000 +So this is how basically you do this sorting technique. + +189 +00:07:03,000 --> 00:07:04,000 +Now, question arise. + +190 +00:07:04,000 --> 00:07:04,000 +How will you do in programming? + +191 +00:07:04,000 --> 00:07:06,000 +So what you need is you need two loops. + +192 +00:07:06,000 --> 00:07:10,000 +One for the number of passes you are doing, right? + +193 +00:07:10,000 --> 00:07:11,000 +Moving from different element, + +194 +00:07:11,000 --> 00:07:13,000 +and you need a inner loop, + +195 +00:07:13,000 --> 00:07:18,000 +which will responsible to find the key and shift the values. + +196 +00:07:18,000 --> 00:07:19,000 +And also you need a temporary variable + +197 +00:07:19,000 --> 00:07:20,000 +so that you can store it somewhere. + +198 +00:07:20,000 --> 00:07:22,000 +So you need a key which will go + +199 +00:07:22,000 --> 00:07:24,000 +into a temporary variable on something. + +200 +00:07:24,000 --> 00:07:25,000 +Now, how do we do this? + +201 +00:07:25,000 --> 00:07:26,000 +That implement in the code. + diff --git a/28 - DSA (Optional)/011 Insertion Sort Code_en.srt b/28 - DSA (Optional)/011 Insertion Sort Code_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..c94595048714fc113b1e7b2a88ef7ebd15d78442 --- /dev/null +++ b/28 - DSA (Optional)/011 Insertion Sort Code_en.srt @@ -0,0 +1,1480 @@ +1 +00:00:00,000 --> 00:00:04,000 +-: So let's try to implement insertion sort using Java. + +2 +00:00:04,000 --> 00:00:06,000 +Now basically even before we implement that, + +3 +00:00:06,000 --> 00:00:07,000 +before we write the code, + +4 +00:00:07,000 --> 00:00:09,000 +let's write the algorithm on the board + +5 +00:00:09,000 --> 00:00:12,000 +and then we'll try to convert that into code. + +6 +00:00:12,000 --> 00:00:13,000 +So what we'll do is first of all, + +7 +00:00:13,000 --> 00:00:15,000 +let's get some values, okay? + +8 +00:00:15,000 --> 00:00:17,000 +So we also need an array. + +9 +00:00:17,000 --> 00:00:21,000 +So let's say I will name this as array itself, arr. + +10 +00:00:21,000 --> 00:00:23,000 +And then it'll have some values. + +11 +00:00:23,000 --> 00:00:26,000 +So let me create a array here + +12 +00:00:26,000 --> 00:00:27,000 +and we'll take few values. + +13 +00:00:27,000 --> 00:00:30,000 +Let's say we got five values. + +14 +00:00:30,000 --> 00:00:32,000 +Of course you can go for more values here. + +15 +00:00:32,000 --> 00:00:33,000 +And then let me just say, + +16 +00:00:33,000 --> 00:00:38,000 +these values are three, six, two, one, five, okay? + +17 +00:00:40,000 --> 00:00:42,000 +So we got these five values. + +18 +00:00:42,000 --> 00:00:44,000 +Now I want some variables as well. + +19 +00:00:44,000 --> 00:00:46,000 +So as I mentioned before, we don't swap here, right? + +20 +00:00:46,000 --> 00:00:48,000 +We do shifting of the values. + +21 +00:00:48,000 --> 00:00:49,000 +So of course to achieve that, + +22 +00:00:49,000 --> 00:00:51,000 +we need inner loop and the outer loop. + +23 +00:00:51,000 --> 00:00:53,000 +So let's say we got the outer loop, + +24 +00:00:53,000 --> 00:00:55,000 +which we'll go for for loop. + +25 +00:00:55,000 --> 00:00:57,000 +So let's say we have a for loop here. + +26 +00:00:57,000 --> 00:00:58,000 +And then we have to take a variable, + +27 +00:00:58,000 --> 00:01:01,000 +which is i for the outer loop counter. + +28 +00:01:01,000 --> 00:01:04,000 +And then inside this, we are going to have a while loop, + +29 +00:01:04,000 --> 00:01:06,000 +which will have let's say some condition. + +30 +00:01:06,000 --> 00:01:08,000 +Of course, you can also go for for and for. + +31 +00:01:08,000 --> 00:01:10,000 +But then when you talk about insertion, + +32 +00:01:10,000 --> 00:01:12,000 +using while loop inside makes much more sense. + +33 +00:01:12,000 --> 00:01:16,000 +It's because we want to run this based on the condition, + +34 +00:01:16,000 --> 00:01:18,000 +not on the number of iterations, okay? + +35 +00:01:18,000 --> 00:01:20,000 +So this is your outer loop. + +36 +00:01:20,000 --> 00:01:22,000 +Let's finish it here. + +37 +00:01:22,000 --> 00:01:23,000 +This is your inner loop. + +38 +00:01:23,000 --> 00:01:24,000 +Let's finish it here. + +39 +00:01:24,000 --> 00:01:26,000 +And let's write the code there. + +40 +00:01:26,000 --> 00:01:28,000 +Now we need two variables as I mentioned. + +41 +00:01:28,000 --> 00:01:29,000 +Of course in the while loop, + +42 +00:01:29,000 --> 00:01:31,000 +we are not going to create a variable. + +43 +00:01:31,000 --> 00:01:32,000 +We are going to use one variable, + +44 +00:01:32,000 --> 00:01:35,000 +but let's also go for j. + +45 +00:01:35,000 --> 00:01:36,000 +So we need a i variable. + +46 +00:01:36,000 --> 00:01:39,000 +Let me just write i here. + +47 +00:01:39,000 --> 00:01:40,000 +We need a j variable + +48 +00:01:40,000 --> 00:01:42,000 +and we need a key as well, + +49 +00:01:42,000 --> 00:01:44,000 +so that you can store this value somewhere. + +50 +00:01:44,000 --> 00:01:46,000 +So at one point, you will compare and you will save it. + +51 +00:01:46,000 --> 00:01:49,000 +Now from where we are going to start this? + +52 +00:01:49,000 --> 00:01:52,000 +Now if you want to understand what we're going to do, + +53 +00:01:52,000 --> 00:01:53,000 +it's very simple. + +54 +00:01:53,000 --> 00:01:55,000 +So initially you will take your six as a key. + +55 +00:01:55,000 --> 00:01:57,000 +So when you write this variable, + +56 +00:01:57,000 --> 00:02:00,000 +we'll say key is initially six, okay? + +57 +00:02:00,000 --> 00:02:02,000 +Now how you are going to get six? + +58 +00:02:02,000 --> 00:02:03,000 +So let's say we take a variable i here + +59 +00:02:03,000 --> 00:02:05,000 +and then we'll keep the j here, okay? + +60 +00:02:05,000 --> 00:02:08,000 +So we're not going to start i from the first one, + +61 +00:02:08,000 --> 00:02:10,000 +we're going to start i from the second one + +62 +00:02:10,000 --> 00:02:13,000 +and then j will be the first value. + +63 +00:02:13,000 --> 00:02:15,000 +Now i will represent the outer loop + +64 +00:02:15,000 --> 00:02:18,000 +and j will change in the inner loop as well, okay? + +65 +00:02:18,000 --> 00:02:19,000 +So that's why we are going for i and j. + +66 +00:02:19,000 --> 00:02:21,000 +We could have gone for different variable name, + +67 +00:02:21,000 --> 00:02:23,000 +but we got used to i and j + +68 +00:02:23,000 --> 00:02:24,000 +for the outer loop and inner loop. + +69 +00:02:24,000 --> 00:02:26,000 +Okay, so now what you're gonna do? + +70 +00:02:26,000 --> 00:02:29,000 +So the idea is very simple. + +71 +00:02:29,000 --> 00:02:31,000 +Once you've got a key, which is six in this case, + +72 +00:02:31,000 --> 00:02:32,000 +now how you got six? + +73 +00:02:32,000 --> 00:02:34,000 +Maybe we can do something like this. + +74 +00:02:34,000 --> 00:02:36,000 +We'll say arr[i], + +75 +00:02:36,000 --> 00:02:37,000 +whatever i represents, + +76 +00:02:37,000 --> 00:02:40,000 +we'll save that in the variable k or key. + +77 +00:02:40,000 --> 00:02:42,000 +And then we are going to start j + +78 +00:02:42,000 --> 00:02:45,000 +by saying j is equal to i-1. + +79 +00:02:45,000 --> 00:02:46,000 +So i is starting from one. + +80 +00:02:46,000 --> 00:02:48,000 +So j is i-1. + +81 +00:02:48,000 --> 00:02:51,000 +That means the value for j initially will be zero + +82 +00:02:51,000 --> 00:02:53,000 +and i is one. + +83 +00:02:53,000 --> 00:02:54,000 +Now what you're gonna do with this? + +84 +00:02:54,000 --> 00:02:55,000 +It's very simple. + +85 +00:02:55,000 --> 00:02:58,000 +You will simply compare the value of j, + +86 +00:02:58,000 --> 00:03:00,000 +which is this, the first value. + +87 +00:03:00,000 --> 00:03:03,000 +So you'll compare j with the key. + +88 +00:03:03,000 --> 00:03:06,000 +If this value is greater than the key, + +89 +00:03:06,000 --> 00:03:08,000 +then you will do the shifting part. + +90 +00:03:08,000 --> 00:03:09,000 +At this point, we don't need that, right? + +91 +00:03:09,000 --> 00:03:10,000 +Because the value of j + +92 +00:03:10,000 --> 00:03:13,000 +where the j represents is less than the key. + +93 +00:03:13,000 --> 00:03:16,000 +Then you don't have to do any shifting part. + +94 +00:03:16,000 --> 00:03:18,000 +So this is already sorted, okay? + +95 +00:03:18,000 --> 00:03:20,000 +But let's go for the next situation. + +96 +00:03:20,000 --> 00:03:22,000 +Now when you send in next situation, it's very simple. + +97 +00:03:22,000 --> 00:03:25,000 +What you simply do is so you shift your value of i here + +98 +00:03:25,000 --> 00:03:26,000 +and you shift your value of j here. + +99 +00:03:26,000 --> 00:03:28,000 +Because we know that the three and six, + +100 +00:03:28,000 --> 00:03:30,000 +if you compare this two, they are sorted. + +101 +00:03:30,000 --> 00:03:31,000 +Let's go for the next one. + +102 +00:03:31,000 --> 00:03:33,000 +Now in this, what you will do, is you will check, + +103 +00:03:33,000 --> 00:03:35,000 +again you will change certain things, right? + +104 +00:03:35,000 --> 00:03:36,000 +If you can see, this will change. + +105 +00:03:36,000 --> 00:03:40,000 +Because now arr[i] is not six, + +106 +00:03:40,000 --> 00:03:44,000 +arr[i] is basically two, okay? + +107 +00:03:44,000 --> 00:03:45,000 +So key is two now. + +108 +00:03:45,000 --> 00:03:46,000 +What about the value of i? + +109 +00:03:46,000 --> 00:03:48,000 +Even i is changed, + +110 +00:03:48,000 --> 00:03:50,000 +i is now two. + +111 +00:03:50,000 --> 00:03:51,000 +So index is two. + +112 +00:03:51,000 --> 00:03:52,000 +So let me also write index, + +113 +00:03:52,000 --> 00:03:54,000 +so that it'll make much more sense, right? + +114 +00:03:54,000 --> 00:03:55,000 +So that those are the index value. + +115 +00:03:55,000 --> 00:03:57,000 +So i value is two now. + +116 +00:03:57,000 --> 00:03:58,000 +What about j? + +117 +00:03:58,000 --> 00:03:59,000 +So j is i-1. + +118 +00:03:59,000 --> 00:04:02,000 +So j will start from one, that's done. + +119 +00:04:02,000 --> 00:04:04,000 +And now you will apply a condition. + +120 +00:04:04,000 --> 00:04:05,000 +So what basically condition we are checking for? + +121 +00:04:05,000 --> 00:04:07,000 +So of course if you write the code here, + +122 +00:04:07,000 --> 00:04:09,000 +so i is initially starting with one. + +123 +00:04:09,000 --> 00:04:10,000 +We are not starting with zero. + +124 +00:04:10,000 --> 00:04:13,000 +And then we'll go till i less than n + +125 +00:04:13,000 --> 00:04:16,000 +if the length of this array is n. + +126 +00:04:16,000 --> 00:04:19,000 +And then we'll say i++, okay? + +127 +00:04:19,000 --> 00:04:21,000 +And then we'll assign the value. + +128 +00:04:21,000 --> 00:04:23,000 +Okay, let's not write the actual code. + +129 +00:04:23,000 --> 00:04:24,000 +Let's write some dummy code. + +130 +00:04:24,000 --> 00:04:25,000 +So let's say we got a key variable. + +131 +00:04:25,000 --> 00:04:28,000 +The value for key is basically arr, + +132 +00:04:28,000 --> 00:04:30,000 +which is the arr[i]. + +133 +00:04:30,000 --> 00:04:32,000 +And that's what we are doing here. + +134 +00:04:32,000 --> 00:04:33,000 +That's one thing. + +135 +00:04:33,000 --> 00:04:34,000 +Next, we also need j variable. + +136 +00:04:34,000 --> 00:04:38,000 +So we'll say j is equal to i-1, okay? + +137 +00:04:38,000 --> 00:04:39,000 +That's how we're gonna start. + +138 +00:04:39,000 --> 00:04:41,000 +Now what you're going to compare in the while loop? + +139 +00:04:41,000 --> 00:04:43,000 +So in the while loop, it's very simple. + +140 +00:04:43,000 --> 00:04:46,000 +You will check for the arr, so arr[j], + +141 +00:04:48,000 --> 00:04:51,000 +okay, let me just say (indistinct) this here. + +142 +00:04:51,000 --> 00:04:54,000 +So if this is greater than the key, + +143 +00:04:54,000 --> 00:04:56,000 +because the shifting part will come only + +144 +00:04:56,000 --> 00:04:59,000 +when whatever is represented by j is greater than the key. + +145 +00:04:59,000 --> 00:05:01,000 +So if you can see, the key is two here. + +146 +00:05:01,000 --> 00:05:03,000 +And in this case, yes, it is greater. + +147 +00:05:03,000 --> 00:05:05,000 +So six is greater than two. + +148 +00:05:05,000 --> 00:05:07,000 +So what you do is you basically perform some operation. + +149 +00:05:07,000 --> 00:05:09,000 +Now what operation I'm going to perform? + +150 +00:05:09,000 --> 00:05:12,000 +So basically first of all, I will take this key somewhere. + +151 +00:05:12,000 --> 00:05:15,000 +Of course, we are storing that two in the key variable. + +152 +00:05:15,000 --> 00:05:17,000 +And then you do the shifting. + +153 +00:05:17,000 --> 00:05:18,000 +Shifting of what? + +154 +00:05:18,000 --> 00:05:20,000 +So you will first compare this two with six + +155 +00:05:20,000 --> 00:05:23,000 +and now we know that we have to shift six. + +156 +00:05:23,000 --> 00:05:24,000 +So you will shift six here. + +157 +00:05:24,000 --> 00:05:26,000 +Then that's one done, right? + +158 +00:05:26,000 --> 00:05:28,000 +Can you put two here? + +159 +00:05:28,000 --> 00:05:30,000 +We can actually, we can put two here. + +160 +00:05:30,000 --> 00:05:31,000 +But the problem is we also know + +161 +00:05:31,000 --> 00:05:33,000 +that we have to compare two with the previous value, + +162 +00:05:33,000 --> 00:05:34,000 +which is three in this case. + +163 +00:05:34,000 --> 00:05:35,000 +So we'll not shift two there. + +164 +00:05:35,000 --> 00:05:37,000 +But we'll shift three here. + +165 +00:05:37,000 --> 00:05:39,000 +But then to shift three here, + +166 +00:05:39,000 --> 00:05:42,000 +what we have to also do is we have to first shift our j here + +167 +00:05:42,000 --> 00:05:43,000 +on the first location. + +168 +00:05:43,000 --> 00:05:48,000 +That means we are also going to perform j--, right? + +169 +00:05:48,000 --> 00:05:49,000 +So what I'm saying, + +170 +00:05:49,000 --> 00:05:51,000 +is the first operation you're going to perform, + +171 +00:05:51,000 --> 00:05:52,000 +is shifting the values, right? + +172 +00:05:52,000 --> 00:05:54,000 +So how do you shift? + +173 +00:05:54,000 --> 00:05:55,000 +You simply take the next value. + +174 +00:05:55,000 --> 00:05:57,000 +So initially, j was here, right? + +175 +00:05:57,000 --> 00:05:59,000 +So this value six is going here. + +176 +00:05:59,000 --> 00:06:00,000 +So that means what we can do, + +177 +00:06:00,000 --> 00:06:03,000 +is we can say arr[j+1], + +178 +00:06:03,000 --> 00:06:07,000 +which is in this case two, is arr[j]. + +179 +00:06:07,000 --> 00:06:09,000 +By doing this, what you're doing is you're shifting. + +180 +00:06:09,000 --> 00:06:12,000 +So the j initially was here, which is one. + +181 +00:06:12,000 --> 00:06:16,000 +So index of j was one or the value of j was one. + +182 +00:06:16,000 --> 00:06:19,000 +Then you're saying j of two, I mean j becomes two, + +183 +00:06:19,000 --> 00:06:19,000 +then what you're saying, + +184 +00:06:19,000 --> 00:06:22,000 +is we are saying j+1, which is two, + +185 +00:06:22,000 --> 00:06:24,000 +so the value of six which was here will move here. + +186 +00:06:24,000 --> 00:06:26,000 +That's what we are doing on this line. + +187 +00:06:26,000 --> 00:06:27,000 +That's the shifting part. + +188 +00:06:27,000 --> 00:06:30,000 +And then you will say j--. + +189 +00:06:30,000 --> 00:06:33,000 +So you can say j-- or you can say j-1 even, both works. + +190 +00:06:33,000 --> 00:06:36,000 +So this is the entire logic you have, right? + +191 +00:06:36,000 --> 00:06:37,000 +But this loop will repeat, right? + +192 +00:06:37,000 --> 00:06:38,000 +Now when it'll repeat? + +193 +00:06:38,000 --> 00:06:39,000 +Look at the condition here. + +194 +00:06:39,000 --> 00:06:41,000 +The condition is whatever j, + +195 +00:06:41,000 --> 00:06:43,000 +so we are shifting j backwards, right? + +196 +00:06:43,000 --> 00:06:44,000 +J is now here. + +197 +00:06:44,000 --> 00:06:47,000 +So we'll check if arr[j] is greater than key. + +198 +00:06:47,000 --> 00:06:48,000 +In this case, yes, + +199 +00:06:48,000 --> 00:06:52,000 +you can see the key is two, arr[j] is three. + +200 +00:06:52,000 --> 00:06:53,000 +So it is greater, so we'll shift. + +201 +00:06:53,000 --> 00:06:54,000 +So how do you shift? + +202 +00:06:54,000 --> 00:06:56,000 +Basically you do the same operation, + +203 +00:06:56,000 --> 00:06:57,000 +which is inside the while loop. + +204 +00:06:57,000 --> 00:07:00,000 +So if you compare this operation, + +205 +00:07:00,000 --> 00:07:02,000 +this is what it is doing the shifting part, right? + +206 +00:07:02,000 --> 00:07:05,000 +And then we know that of course if you can see here, + +207 +00:07:05,000 --> 00:07:06,000 +we have one more step. + +208 +00:07:06,000 --> 00:07:07,000 +You will do j-1, + +209 +00:07:07,000 --> 00:07:11,000 +which means your j basically moves one here. + +210 +00:07:11,000 --> 00:07:13,000 +Because when you reduce the value of j, + +211 +00:07:13,000 --> 00:07:15,000 +it became zero, right? + +212 +00:07:15,000 --> 00:07:16,000 +And then again you're doing it, + +213 +00:07:16,000 --> 00:07:19,000 +so j becomes minus-one. + +214 +00:07:19,000 --> 00:07:20,000 +So we have to stop it here. + +215 +00:07:20,000 --> 00:07:22,000 +Once the j becomes less than zero, we have to stop. + +216 +00:07:22,000 --> 00:07:23,000 +But don't you think we have + +217 +00:07:23,000 --> 00:07:25,000 +to also add that condition in the while loop? + +218 +00:07:25,000 --> 00:07:26,000 +So we'll do that. + +219 +00:07:26,000 --> 00:07:29,000 +We'll say and j should be greater than equal to zero, okay? + +220 +00:07:29,000 --> 00:07:30,000 +This is one condition we have to add. + +221 +00:07:30,000 --> 00:07:31,000 +If this is false, + +222 +00:07:31,000 --> 00:07:34,000 +if j is not less than or equal to zero, + +223 +00:07:34,000 --> 00:07:35,000 +then you will do the last step. + +224 +00:07:35,000 --> 00:07:37,000 +The last step is very simple. + +225 +00:07:37,000 --> 00:07:38,000 +You move your two here. + +226 +00:07:38,000 --> 00:07:39,000 +And the way you can do that, + +227 +00:07:39,000 --> 00:07:43,000 +is by saying arr[j+1], + +228 +00:07:43,000 --> 00:07:45,000 +because j became minus-one, we don't want minus-one, + +229 +00:07:45,000 --> 00:07:47,000 +we want zero is equal to k, is equal to key. + +230 +00:07:47,000 --> 00:07:50,000 +So basically this is where the value goes first. + +231 +00:07:50,000 --> 00:07:50,000 +Now, by doing this, + +232 +00:07:50,000 --> 00:07:53,000 +we were able to sort the first three values. + +233 +00:07:53,000 --> 00:07:53,000 +Now what next? + +234 +00:07:53,000 --> 00:07:56,000 +Of course, you will increment the value of i. + +235 +00:07:56,000 --> 00:07:57,000 +So when you say increment, + +236 +00:07:57,000 --> 00:08:00,000 +that means the value of the i variable goes here. + +237 +00:08:00,000 --> 00:08:04,000 +And again for the next situation, j goes i-1, right? + +238 +00:08:04,000 --> 00:08:06,000 +Because of this basically for the outer loop, + +239 +00:08:06,000 --> 00:08:07,000 +the i value is incrementing, + +240 +00:08:07,000 --> 00:08:10,000 +but the j value is just before i. + +241 +00:08:10,000 --> 00:08:12,000 +And again, you do the same things. + +242 +00:08:12,000 --> 00:08:12,000 +What are the same things? + +243 +00:08:12,000 --> 00:08:15,000 +You first check if the key, + +244 +00:08:15,000 --> 00:08:16,000 +okay, we have to update the key as well. + +245 +00:08:16,000 --> 00:08:17,000 +Now how do you update the key? + +246 +00:08:17,000 --> 00:08:20,000 +Now the key value is arr[i], + +247 +00:08:20,000 --> 00:08:23,000 +which is in this case should be one. + +248 +00:08:23,000 --> 00:08:26,000 +So the new key is one and i is what? + +249 +00:08:26,000 --> 00:08:29,000 +I is three and j is two. + +250 +00:08:29,000 --> 00:08:31,000 +Okay, new key value is one. + +251 +00:08:31,000 --> 00:08:31,000 +You will compare, + +252 +00:08:31,000 --> 00:08:35,000 +if the arr[j], this is the condition we're checking now, + +253 +00:08:35,000 --> 00:08:40,000 +if arr[j], which is in this case is six, is greater + +254 +00:08:40,000 --> 00:08:41,000 +than the key, + +255 +00:08:41,000 --> 00:08:42,000 +in this case, yes, + +256 +00:08:42,000 --> 00:08:43,000 +you perform this operation. + +257 +00:08:43,000 --> 00:08:44,000 +What is the operation? + +258 +00:08:44,000 --> 00:08:45,000 +It's very simple. + +259 +00:08:45,000 --> 00:08:46,000 +Let's keep this value somewhere. + +260 +00:08:46,000 --> 00:08:49,000 +You start shifting six here, six goes there + +261 +00:08:49,000 --> 00:08:52,000 +and then your j comes back, + +262 +00:08:52,000 --> 00:08:53,000 +j comes here, right? + +263 +00:08:53,000 --> 00:08:55,000 +Because of this step here, right? + +264 +00:08:55,000 --> 00:09:00,000 +And then you again check if arr[j] is greater than key. + +265 +00:09:00,000 --> 00:09:01,000 +Yes, three is greater than one. + +266 +00:09:01,000 --> 00:09:03,000 +Again, you will shift three here, + +267 +00:09:03,000 --> 00:09:06,000 +then you shift j here, then again you shift two here, + +268 +00:09:06,000 --> 00:09:10,000 +then you shift j here and then now j becomes minus-one. + +269 +00:09:10,000 --> 00:09:13,000 +Let's stop the loop and shift one here. + +270 +00:09:13,000 --> 00:09:15,000 +You can just do the same thing for five. + +271 +00:09:15,000 --> 00:09:16,000 +Let's do that quickly. + +272 +00:09:16,000 --> 00:09:18,000 +So what you will do is you will shift the value of i here, + +273 +00:09:18,000 --> 00:09:21,000 +which means certain things are going to change. + +274 +00:09:21,000 --> 00:09:24,000 +Now this i becomes four, this j becomes three. + +275 +00:09:24,000 --> 00:09:27,000 +The key value now is five, right? + +276 +00:09:27,000 --> 00:09:29,000 +Again, you do the same things. + +277 +00:09:29,000 --> 00:09:32,000 +You keep your j here, because j is just i-1. + +278 +00:09:32,000 --> 00:09:33,000 +Then you compare, + +279 +00:09:33,000 --> 00:09:37,000 +if the arr[j] which is six in this case is greater + +280 +00:09:37,000 --> 00:09:38,000 +than the key. + +281 +00:09:38,000 --> 00:09:41,000 +Yes, let's do the shifting. + +282 +00:09:41,000 --> 00:09:41,000 +Now how do we do that? + +283 +00:09:41,000 --> 00:09:43,000 +Let's move that here + +284 +00:09:43,000 --> 00:09:46,000 +and then we have to shift six here, j goes here. + +285 +00:09:46,000 --> 00:09:48,000 +Now again, you check. + +286 +00:09:48,000 --> 00:09:50,000 +Is the arr[j] is greater than key? + +287 +00:09:50,000 --> 00:09:51,000 +No, it's not that. + +288 +00:09:51,000 --> 00:09:54,000 +So you will just simply come out of the loop + +289 +00:09:54,000 --> 00:09:57,000 +and then you will keep five here. + +290 +00:09:57,000 --> 00:09:59,000 +And if you can see, it's all sorted. + +291 +00:09:59,000 --> 00:10:01,000 +So this is the logic. + +292 +00:10:01,000 --> 00:10:03,000 +Let's implement this logic in the code now. + +293 +00:10:03,000 --> 00:10:04,000 +So you have the logic in front of you. + +294 +00:10:04,000 --> 00:10:07,000 +What you will do is you got the same values. + +295 +00:10:07,000 --> 00:10:09,000 +In fact, we can go with the values which we have. + +296 +00:10:09,000 --> 00:10:12,000 +So let's say, okay, now I've just rewritten the values. + +297 +00:10:12,000 --> 00:10:15,000 +Let's go for these values and let's see if this works. + +298 +00:10:15,000 --> 00:10:17,000 +Instead of array, we are going for nums. + +299 +00:10:17,000 --> 00:10:18,000 +That perfectly works. + +300 +00:10:18,000 --> 00:10:20,000 +I don't want a temp variable. + +301 +00:10:20,000 --> 00:10:23,000 +So we can remove this portion here. + +302 +00:10:23,000 --> 00:10:24,000 +This is basically the older code, + +303 +00:10:24,000 --> 00:10:27,000 +which we had for selection sort if I'm not wrong. + +304 +00:10:27,000 --> 00:10:29,000 +And this is the before-sorting elements. + +305 +00:10:29,000 --> 00:10:31,000 +This logic is going to change for sure. + +306 +00:10:31,000 --> 00:10:32,000 +Okay, so let's write the logic. + +307 +00:10:32,000 --> 00:10:33,000 +It's very simple. + +308 +00:10:33,000 --> 00:10:34,000 +You create a array. + +309 +00:10:34,000 --> 00:10:38,000 +So let's say this array is arr is equal to, + +310 +00:10:38,000 --> 00:10:40,000 +let's have some default values to it, + +311 +00:10:40,000 --> 00:10:43,000 +let's say five, six, two, three, one. + +312 +00:10:43,000 --> 00:10:45,000 +This time, we are going for only five values + +313 +00:10:45,000 --> 00:10:47,000 +and this is an array. + +314 +00:10:47,000 --> 00:10:47,000 +Okay, what next? + +315 +00:10:47,000 --> 00:10:50,000 +Now basically as we have returned in the board, + +316 +00:10:50,000 --> 00:10:51,000 +let's take a for loop. + +317 +00:10:51,000 --> 00:10:53,000 +Now the for loop is going to start with i, + +318 +00:10:53,000 --> 00:10:54,000 +i is equal to one + +319 +00:10:54,000 --> 00:10:55,000 +and we can do that quickly now. + +320 +00:10:55,000 --> 00:10:58,000 +It'll finish at n, but we don't know what n is. + +321 +00:10:58,000 --> 00:11:00,000 +So we can say arr.length. + +322 +00:11:00,000 --> 00:11:02,000 +And we can say i++, right? + +323 +00:11:02,000 --> 00:11:03,000 +That's one done. + +324 +00:11:03,000 --> 00:11:07,000 +And now, okay, let me just remove some space in between. + +325 +00:11:07,000 --> 00:11:09,000 +Okay, now what are the extra variables we need? + +326 +00:11:09,000 --> 00:11:11,000 +Basically, we need two variables, right? + +327 +00:11:11,000 --> 00:11:12,000 +We need key. + +328 +00:11:12,000 --> 00:11:15,000 +The value for key will be arr[i], + +329 +00:11:15,000 --> 00:11:17,000 +which we have done earlier. + +330 +00:11:17,000 --> 00:11:18,000 +And then we also need j + +331 +00:11:18,000 --> 00:11:20,000 +and we know j is j-1. + +332 +00:11:20,000 --> 00:11:21,000 +Now this is what happens + +333 +00:11:21,000 --> 00:11:25,000 +when you know the algorithm on the board, all right? + +334 +00:11:25,000 --> 00:11:27,000 +I have a board with me. + +335 +00:11:27,000 --> 00:11:30,000 +So, okay, those are the things we need. + +336 +00:11:30,000 --> 00:11:32,000 +Okay, not j-1, it should be i-1. + +337 +00:11:32,000 --> 00:11:34,000 +And then you do a while loop. + +338 +00:11:34,000 --> 00:11:36,000 +Because this is where you do the iteration. + +339 +00:11:36,000 --> 00:11:39,000 +We have to make sure that j is greater than zero. + +340 +00:11:39,000 --> 00:11:39,000 +That's one thing. + +341 +00:11:39,000 --> 00:11:44,000 +And we need to also check if arr[j] is greater than key. + +342 +00:11:45,000 --> 00:11:47,000 +In that case, you will execute the while loop. + +343 +00:11:47,000 --> 00:11:49,000 +So you will do certain things here. + +344 +00:11:49,000 --> 00:11:51,000 +Now what you are going to do inside this while loop? + +345 +00:11:51,000 --> 00:11:53,000 +So basically you have to do the shifting, right? + +346 +00:11:53,000 --> 00:11:55,000 +If it goes inside, you will say arr[j+1] is equal + +347 +00:11:57,000 --> 00:12:00,000 +to arr[j], right? + +348 +00:12:00,000 --> 00:12:02,000 +And also you have to reduce the value of j. + +349 +00:12:02,000 --> 00:12:05,000 +So you'll say j, we can even say j--. + +350 +00:12:05,000 --> 00:12:06,000 +Syntactically that works. + +351 +00:12:06,000 --> 00:12:10,000 +And for the outer loop, you will assign the value for key + +352 +00:12:10,000 --> 00:12:12,000 +for that particular empty location. + +353 +00:12:12,000 --> 00:12:14,000 +And the empty location is defined by j itself. + +354 +00:12:14,000 --> 00:12:19,000 +So j+1 is equal to key and I think we are done. + +355 +00:12:19,000 --> 00:12:20,000 +(Instructor laughs) + +356 +00:12:20,000 --> 00:12:22,000 +So the code is so short. + +357 +00:12:22,000 --> 00:12:23,000 +Let me check if that works. + +358 +00:12:23,000 --> 00:12:25,000 +What I will do is I will just print the values here. + +359 +00:12:25,000 --> 00:12:27,000 +So I'll take a for loop + +360 +00:12:27,000 --> 00:12:30,000 +and I will say int num from array + +361 +00:12:30,000 --> 00:12:31,000 +and let's print the values. + +362 +00:12:31,000 --> 00:12:33,000 +Let's print num. + +363 +00:12:33,000 --> 00:12:34,000 +I don't want the new line. + +364 +00:12:34,000 --> 00:12:36,000 +I would just want a space here and let's see if that works. + +365 +00:12:36,000 --> 00:12:39,000 +Right-click, run and boom! + +366 +00:12:39,000 --> 00:12:43,000 +You can see we got the output and the values are sorted. + +367 +00:12:43,000 --> 00:12:45,000 +It doesn't matter what type of values you add here. + +368 +00:12:45,000 --> 00:12:48,000 +Let's say eight, four and run. + +369 +00:12:48,000 --> 00:12:51,000 +You can see, this is sorted, yeah? + +370 +00:12:51,000 --> 00:12:53,000 +So that's your insertion sort. + diff --git a/28 - DSA (Optional)/012 Quick Sort Theory_en.srt b/28 - DSA (Optional)/012 Quick Sort Theory_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..562db22d6678de1abc4e7957d371b03eecd2350d --- /dev/null +++ b/28 - DSA (Optional)/012 Quick Sort Theory_en.srt @@ -0,0 +1,2268 @@ +1 +00:00:00,000 --> 00:00:03,000 +-: In this video, let's talk about another sorting algorithm + +2 +00:00:03,000 --> 00:00:04,000 +which is called a quick sort. + +3 +00:00:04,000 --> 00:00:07,000 +So the thing is, when you talk about sorting algorithms, + +4 +00:00:07,000 --> 00:00:10,000 +the idea here is to explore different algorithm + +5 +00:00:10,000 --> 00:00:13,000 +and we'll see how efficient it is. + +6 +00:00:13,000 --> 00:00:15,000 +Now, when we talked about the other algorithms, + +7 +00:00:15,000 --> 00:00:16,000 +which we talked about till now, + +8 +00:00:16,000 --> 00:00:18,000 +bubble sort, selection sort, + +9 +00:00:18,000 --> 00:00:21,000 +they have a Big O notation of n squared, + +10 +00:00:21,000 --> 00:00:22,000 +which we don't want, right? + +11 +00:00:22,000 --> 00:00:24,000 +We want to make it faster or efficient. + +12 +00:00:24,000 --> 00:00:27,000 +Now in that case we have to explore some other algorithms, + +13 +00:00:27,000 --> 00:00:29,000 +and one of them is quick sort. + +14 +00:00:29,000 --> 00:00:33,000 +Now in the best case, the quick sort goes for n log(n), + +15 +00:00:33,000 --> 00:00:37,000 +which is much better compared to the n squared, + +16 +00:00:37,000 --> 00:00:40,000 +but in the worst case also it goes for n squared. + +17 +00:00:40,000 --> 00:00:43,000 +So in the worst case, yes, it is almost there, + +18 +00:00:43,000 --> 00:00:47,000 +but on on average it is n log(n), + +19 +00:00:47,000 --> 00:00:48,000 +or you can say the best case. + +20 +00:00:48,000 --> 00:00:50,000 +So at least we are happy in the best case + +21 +00:00:50,000 --> 00:00:52,000 +and in the average case. + +22 +00:00:52,000 --> 00:00:54,000 +Now let's say what is quick sort? + +23 +00:00:54,000 --> 00:00:56,000 +So actually it uses multiple things, + +24 +00:00:56,000 --> 00:00:57,000 +so let's talk about that first. + +25 +00:00:57,000 --> 00:00:59,000 +The first thing is divide and conquer. + +26 +00:00:59,000 --> 00:01:00,000 +So basically till this point + +27 +00:01:00,000 --> 00:01:03,000 +when we talked about bubble sort or selection sort, + +28 +00:01:03,000 --> 00:01:06,000 +we were doing the algorithm on the entire collection. + +29 +00:01:06,000 --> 00:01:08,000 +So let's say if you have a a list of values, + +30 +00:01:08,000 --> 00:01:10,000 +which is six values, + +31 +00:01:10,000 --> 00:01:12,000 +so what you do is you perform the operation of bubble sort + +32 +00:01:12,000 --> 00:01:14,000 +and all the sorting techniques, + +33 +00:01:14,000 --> 00:01:18,000 +which we have done before, applied on the entire list. + +34 +00:01:18,000 --> 00:01:21,000 +But what quick sort says, let's do divide and conquer, + +35 +00:01:21,000 --> 00:01:23,000 +which simply means divide your problem + +36 +00:01:23,000 --> 00:01:26,000 +into multiple things and solve the problem, + +37 +00:01:26,000 --> 00:01:28,000 +or sub-problems individually. + +38 +00:01:28,000 --> 00:01:29,000 +So what you're doing is you're dividing it, + +39 +00:01:29,000 --> 00:01:32,000 +you're solving it, and at the end you will combine it. + +40 +00:01:32,000 --> 00:01:33,000 +So that's your divide and conquer. + +41 +00:01:33,000 --> 00:01:35,000 +So in this case, if you have six values, + +42 +00:01:35,000 --> 00:01:37,000 +let's divide the values into multiple sub-problems + +43 +00:01:37,000 --> 00:01:39,000 +and then sort it and then combine it. + +44 +00:01:39,000 --> 00:01:41,000 +So that is your divide and conquer. + +45 +00:01:41,000 --> 00:01:44,000 +Next it uses something called a recursion. + +46 +00:01:44,000 --> 00:01:45,000 +Of course you can also use recursion + +47 +00:01:45,000 --> 00:01:47,000 +in the other algorithms, + +48 +00:01:47,000 --> 00:01:50,000 +but here it works with recursion. + +49 +00:01:50,000 --> 00:01:51,000 +Now basically what is recursion? + +50 +00:01:51,000 --> 00:01:54,000 +So when you call the same function by itself, + +51 +00:01:54,000 --> 00:01:56,000 +so example, let's say the function name or the meta name + +52 +00:01:56,000 --> 00:01:59,000 +is show, and if you're calling show inside show, + +53 +00:01:59,000 --> 00:02:00,000 +that's your recursion, right? + +54 +00:02:00,000 --> 00:02:03,000 +And next we have to understand the concept of pivot. + +55 +00:02:03,000 --> 00:02:06,000 +Now basically pivot is something + +56 +00:02:06,000 --> 00:02:08,000 +called a central point of a problem, okay? + +57 +00:02:08,000 --> 00:02:10,000 +So let's say you have this entire list here, + +58 +00:02:10,000 --> 00:02:13,000 +or the array, now you have to find a pivot + +59 +00:02:13,000 --> 00:02:16,000 +and then based on that you will perform the operation. + +60 +00:02:16,000 --> 00:02:18,000 +Of course, it will make much more sense when we start, + +61 +00:02:18,000 --> 00:02:21,000 +but remember we have to work with the pivot. + +62 +00:02:21,000 --> 00:02:22,000 +So those are the things you have to remember, + +63 +00:02:22,000 --> 00:02:24,000 +divide, conquer, recursion, and pivot. + +64 +00:02:24,000 --> 00:02:26,000 +There's one more thing, which is the tree. + +65 +00:02:26,000 --> 00:02:28,000 +Now basically when you say you are dividing + +66 +00:02:28,000 --> 00:02:32,000 +your entire list into subsections, or sub problems, + +67 +00:02:32,000 --> 00:02:33,000 +what you're doing is you are creating + +68 +00:02:33,000 --> 00:02:35,000 +a tree structure, okay? + +69 +00:02:35,000 --> 00:02:36,000 +So once we start with the diagrams, + +70 +00:02:36,000 --> 00:02:38,000 +it'll make much more sense, + +71 +00:02:38,000 --> 00:02:40,000 +but these are the things we have to remember. + +72 +00:02:40,000 --> 00:02:42,000 +So let's see how your quick sort works + +73 +00:02:42,000 --> 00:02:45,000 +and then we'll try to convert that into algorithm, + +74 +00:02:45,000 --> 00:02:47,000 +and then we'll see how to write a code for it. + +75 +00:02:47,000 --> 00:02:49,000 +Now the basic idea here + +76 +00:02:49,000 --> 00:02:50,000 +is let's say you have a list of values, + +77 +00:02:50,000 --> 00:02:53,000 +so we are discussing about quick sort. + +78 +00:02:53,000 --> 00:02:55,000 +So let's say we have a list of values. + +79 +00:02:55,000 --> 00:02:57,000 +So let's go for six values here, + +80 +00:02:57,000 --> 00:03:02,000 +and I will say this is 5, this is 3, 6, 1, 4, and 2. + +81 +00:03:03,000 --> 00:03:04,000 +So let's say we have the six values. + +82 +00:03:04,000 --> 00:03:07,000 +I'm not sure will it really make sense + +83 +00:03:07,000 --> 00:03:09,000 +to use this example, but let's get started. + +84 +00:03:09,000 --> 00:03:11,000 +Now with these six values, + +85 +00:03:11,000 --> 00:03:14,000 +what you normally do is you do divide and conquer here. + +86 +00:03:14,000 --> 00:03:17,000 +So the basic idea is break the entire list + +87 +00:03:17,000 --> 00:03:19,000 +into sub-problems, okay? + +88 +00:03:19,000 --> 00:03:21,000 +Something like this, maybe we can create + +89 +00:03:21,000 --> 00:03:23,000 +two different arrays here. + +90 +00:03:23,000 --> 00:03:26,000 +We'll be having three, I mean five, three, six. + +91 +00:03:26,000 --> 00:03:29,000 +Here you can have one, four, two. + +92 +00:03:29,000 --> 00:03:31,000 +And then you can, again, divide this. + +93 +00:03:31,000 --> 00:03:33,000 +Maybe you can have one and four here + +94 +00:03:33,000 --> 00:03:35,000 +and divide this into two. + +95 +00:03:35,000 --> 00:03:37,000 +Here also we can have five, + +96 +00:03:37,000 --> 00:03:40,000 +and then we can have another area which is three and six. + +97 +00:03:40,000 --> 00:03:43,000 +So we got these two areas from the big area, + +98 +00:03:43,000 --> 00:03:46,000 +and then you create the shorter format of it. + +99 +00:03:46,000 --> 00:03:47,000 +So at the end, what you're doing is you're creating + +100 +00:03:47,000 --> 00:03:49,000 +the small chunks. + +101 +00:03:49,000 --> 00:03:51,000 +Now this is what your tree structure is. + +102 +00:03:51,000 --> 00:03:54,000 +So we talked about divide, so we are dividing it, + +103 +00:03:54,000 --> 00:03:57,000 +we'll sort the array, and then we'll combine it. + +104 +00:03:57,000 --> 00:03:59,000 +And then since we are performing this sorting + +105 +00:03:59,000 --> 00:04:03,000 +on each section in sub-problem, that is your recursion. + +106 +00:04:03,000 --> 00:04:05,000 +But is it the real thing? No. + +107 +00:04:05,000 --> 00:04:07,000 +So basically we talked about the idea, + +108 +00:04:07,000 --> 00:04:09,000 +but how do you divide the array? + +109 +00:04:09,000 --> 00:04:11,000 +Randomly I just divided the array, + +110 +00:04:11,000 --> 00:04:12,000 +but that's not the main idea. + +111 +00:04:12,000 --> 00:04:14,000 +But we have to divide, but not this way. + +112 +00:04:14,000 --> 00:04:18,000 +So how we are going to divide this? That's the question. + +113 +00:04:18,000 --> 00:04:20,000 +Let's remove this and let's go with the values here. + +114 +00:04:20,000 --> 00:04:22,000 +The idea is you want to divide for sure. + +115 +00:04:22,000 --> 00:04:24,000 +You will divide this into two parts, + +116 +00:04:24,000 --> 00:04:26,000 +but you need to find a point + +117 +00:04:26,000 --> 00:04:28,000 +from where you will divide this, + +118 +00:04:28,000 --> 00:04:30,000 +and that point is basically your pivot. + +119 +00:04:30,000 --> 00:04:31,000 +Now, how do you find a pivot? + +120 +00:04:31,000 --> 00:04:35,000 +Now basically the reason we get this pivot, + +121 +00:04:35,000 --> 00:04:37,000 +to do the partitions of the array, right? + +122 +00:04:37,000 --> 00:04:39,000 +Now, should we pick a one? + +123 +00:04:39,000 --> 00:04:40,000 +Now, that depends. + +124 +00:04:40,000 --> 00:04:43,000 +So even before you divide, you have to do one more thing. + +125 +00:04:43,000 --> 00:04:44,000 +So I'm saying pivot, + +126 +00:04:44,000 --> 00:04:46,000 +but then that pivot should be a point + +127 +00:04:46,000 --> 00:04:49,000 +which is at the right position. + +128 +00:04:49,000 --> 00:04:50,000 +Let me repeat. + +129 +00:04:50,000 --> 00:04:52,000 +The pivot is a point in this array + +130 +00:04:52,000 --> 00:04:55,000 +which is at the right location. + +131 +00:04:55,000 --> 00:04:56,000 +Now if you see this, + +132 +00:04:56,000 --> 00:04:59,000 +after this sorting, what the values you will get? + +133 +00:04:59,000 --> 00:05:00,000 +So you will get values, + +134 +00:05:00,000 --> 00:05:04,000 +like let's say, I will just write it in shorter format now. + +135 +00:05:04,000 --> 00:05:08,000 +So you will get 1, 2, 3, 4, 5, 6. + +136 +00:05:08,000 --> 00:05:10,000 +This is the output I want, okay? + +137 +00:05:10,000 --> 00:05:12,000 +Just went for the sequence of numbers here, + +138 +00:05:12,000 --> 00:05:14,000 +but this is the output. + +139 +00:05:14,000 --> 00:05:16,000 +So this is the output which we want from this input, + +140 +00:05:16,000 --> 00:05:18,000 +which we have the bigger array here, + +141 +00:05:18,000 --> 00:05:21,000 +or the big size, which I mentioned. + +142 +00:05:21,000 --> 00:05:24,000 +Okay, so if you talk about the value one here, + +143 +00:05:24,000 --> 00:05:26,000 +so one should be at the first position, + +144 +00:05:26,000 --> 00:05:27,000 +two should be at the second position, + +145 +00:05:27,000 --> 00:05:29,000 +three on the third position, four, fourth position, + +146 +00:05:29,000 --> 00:05:31,000 +fifth and sixth, right? + +147 +00:05:31,000 --> 00:05:33,000 +So they should be at that position. + +148 +00:05:33,000 --> 00:05:34,000 +Now, since we have taken those values, + +149 +00:05:34,000 --> 00:05:36,000 +and that's why it might be confusing, + +150 +00:05:36,000 --> 00:05:38,000 +but you got the point, right? + +151 +00:05:38,000 --> 00:05:39,000 +It doesn't matter what your values you have, + +152 +00:05:39,000 --> 00:05:41,000 +it should be in the ascending order. + +153 +00:05:41,000 --> 00:05:43,000 +So the first value, the smallest value should be first, + +154 +00:05:43,000 --> 00:05:45,000 +and then a list goes on. + +155 +00:05:45,000 --> 00:05:48,000 +Okay, now we have to make sure that at least you find + +156 +00:05:48,000 --> 00:05:51,000 +one point from here, or one value + +157 +00:05:51,000 --> 00:05:52,000 +which is at the right position. + +158 +00:05:52,000 --> 00:05:56,000 +Example, let me take four. + +159 +00:05:56,000 --> 00:05:58,000 +Now four, is it at the right position? + +160 +00:05:58,000 --> 00:05:59,000 +The answer is no. + +161 +00:05:59,000 --> 00:06:01,000 +Four should be here, right? + +162 +00:06:01,000 --> 00:06:03,000 +So instead of one, it should be four. + +163 +00:06:03,000 --> 00:06:06,000 +Then we can say four is at the right position. + +164 +00:06:06,000 --> 00:06:08,000 +Now, if you want to understand this with an example, + +165 +00:06:08,000 --> 00:06:09,000 +so let's say you're in a classroom + +166 +00:06:09,000 --> 00:06:11,000 +and then you have multiple students there, okay? + +167 +00:06:11,000 --> 00:06:14,000 +And then you want to sort them based on their height. + +168 +00:06:14,000 --> 00:06:16,000 +There's one way as a trainer, I can be there + +169 +00:06:16,000 --> 00:06:18,000 +and I can say, "Okay, you go there, you go there." + +170 +00:06:18,000 --> 00:06:20,000 +And that's what we have done in the bubble sort, right? + +171 +00:06:20,000 --> 00:06:21,000 +I was controlling it. + +172 +00:06:21,000 --> 00:06:24,000 +But what if student can sort themself. + +173 +00:06:24,000 --> 00:06:25,000 +So they can compare each other + +174 +00:06:25,000 --> 00:06:26,000 +and they say, "Okay, I should be here," + +175 +00:06:26,000 --> 00:06:29,000 +or "I should be here," and they will move. + +176 +00:06:29,000 --> 00:06:32,000 +Now, one way is one person can sort them, + +177 +00:06:32,000 --> 00:06:34,000 +but then we don't want it. + +178 +00:06:34,000 --> 00:06:39,000 +So what we want is what if the students themself + +179 +00:06:39,000 --> 00:06:40,000 +can get into the right position, + +180 +00:06:40,000 --> 00:06:42,000 +that will solve the problem, right? + +181 +00:06:42,000 --> 00:06:44,000 +Okay, but how we are going to do here, + +182 +00:06:44,000 --> 00:06:46,000 +how they will reach to a right position. + +183 +00:06:46,000 --> 00:06:47,000 +Now, what you do is, + +184 +00:06:47,000 --> 00:06:49,000 +initially we don't know the position, right? + +185 +00:06:49,000 --> 00:06:50,000 +So you can take any value. + +186 +00:06:50,000 --> 00:06:52,000 +Normally we can find the pivot. + +187 +00:06:52,000 --> 00:06:54,000 +So for the first iteration, what we are trying to do + +188 +00:06:54,000 --> 00:06:56,000 +is we want to make sure at least one value + +189 +00:06:56,000 --> 00:06:59,000 +is at the right position so that we can divide the array. + +190 +00:06:59,000 --> 00:07:01,000 +Now, which value should we take? + +191 +00:07:01,000 --> 00:07:03,000 +Technically, if you take the average value, + +192 +00:07:03,000 --> 00:07:05,000 +or if you take the middle value, + +193 +00:07:05,000 --> 00:07:08,000 +example, if you go from one to six, + +194 +00:07:08,000 --> 00:07:11,000 +the mid value is three or four, right? + +195 +00:07:11,000 --> 00:07:14,000 +So if I can take three as my pivot, + +196 +00:07:14,000 --> 00:07:17,000 +it'll be much easier for me to divide the array. + +197 +00:07:17,000 --> 00:07:19,000 +So what will happen is if I take three as my pivot, + +198 +00:07:19,000 --> 00:07:22,000 +then three should be coming here, right? + +199 +00:07:22,000 --> 00:07:25,000 +So this is the right position for three, right? + +200 +00:07:25,000 --> 00:07:28,000 +But it's not, if you start with the array, + +201 +00:07:28,000 --> 00:07:29,000 +this is not at the right position. + +202 +00:07:29,000 --> 00:07:30,000 +But if you get in the right position, + +203 +00:07:30,000 --> 00:07:32,000 +we can divide into two parts, right? + +204 +00:07:32,000 --> 00:07:33,000 +Okay, so that's the main idea. + +205 +00:07:33,000 --> 00:07:34,000 +How will you get that there? + +206 +00:07:34,000 --> 00:07:38,000 +So normally what you do is if you can find a good pivot, + +207 +00:07:38,000 --> 00:07:39,000 +and that's very difficult to find + +208 +00:07:39,000 --> 00:07:41,000 +because how do you know the mid value? + +209 +00:07:41,000 --> 00:07:43,000 +Since we know the values here, I can find the mid value, + +210 +00:07:43,000 --> 00:07:45,000 +but as a program or the software, + +211 +00:07:45,000 --> 00:07:48,000 +they don't have any idea what those values are. + +212 +00:07:48,000 --> 00:07:51,000 +So what we do is we randomly pick the pivot. + +213 +00:07:51,000 --> 00:07:53,000 +Maybe you can pick up the pivot, let's say one + +214 +00:07:53,000 --> 00:07:55,000 +in between somewhere, anywhere if you want. + +215 +00:07:55,000 --> 00:07:57,000 +So you can pick up this one, you can pick up the six, + +216 +00:07:57,000 --> 00:08:00,000 +or you can pick up the first value, + +217 +00:08:00,000 --> 00:08:01,000 +or you can pick up the last value. + +218 +00:08:01,000 --> 00:08:05,000 +Or what you can do is you can just add all these values + +219 +00:08:05,000 --> 00:08:07,000 +and you divide by six, whatever value you will get. + +220 +00:08:07,000 --> 00:08:10,000 +So try to find the average of it. + +221 +00:08:10,000 --> 00:08:11,000 +That's what we can do. + +222 +00:08:11,000 --> 00:08:13,000 +But again, that's a lengthy process. + +223 +00:08:13,000 --> 00:08:14,000 +Normally, which I have seen, + +224 +00:08:14,000 --> 00:08:16,000 +is they go for the last value, + +225 +00:08:16,000 --> 00:08:18,000 +not the biggest value, last value, + +226 +00:08:18,000 --> 00:08:20,000 +because we don't have any idea what's the biggest value is. + +227 +00:08:20,000 --> 00:08:21,000 +So it will go for the last value. + +228 +00:08:21,000 --> 00:08:23,000 +Okay, so this becomes your pivot. + +229 +00:08:23,000 --> 00:08:25,000 +That means we need multiple variables here. + +230 +00:08:25,000 --> 00:08:26,000 +Now, what variables we need? + +231 +00:08:26,000 --> 00:08:28,000 +So I will just write variables here. + +232 +00:08:28,000 --> 00:08:30,000 +So the first variable we need is a pivot variable. + +233 +00:08:30,000 --> 00:08:32,000 +So we'll say pi, okay? + +234 +00:08:32,000 --> 00:08:34,000 +And then you need few more variables. + +235 +00:08:34,000 --> 00:08:37,000 +You need a variable i for the positioning, + +236 +00:08:37,000 --> 00:08:39,000 +again, we'll discuss what this positioning is. + +237 +00:08:39,000 --> 00:08:42,000 +Then we'll need a j, which will track, + +238 +00:08:42,000 --> 00:08:43,000 +which will go through each element. + +239 +00:08:43,000 --> 00:08:44,000 +And we have done that in the sorting, right? + +240 +00:08:44,000 --> 00:08:48,000 +So when you jump from each value, that will be done by j. + +241 +00:08:48,000 --> 00:08:50,000 +And then of course we also need array here, + +242 +00:08:50,000 --> 00:08:53,000 +so let's say this is A-R-R, this is your array, okay? + +243 +00:08:53,000 --> 00:08:54,000 +And let's have the index value, + +244 +00:08:54,000 --> 00:08:58,000 +so this 0, 1, 2, 3, 4, 5, + +245 +00:08:58,000 --> 00:09:00,000 +those are the index values we have. + +246 +00:09:00,000 --> 00:09:01,000 +Apart from this, we also need + +247 +00:09:01,000 --> 00:09:04,000 +a low and a high variable, okay? + +248 +00:09:04,000 --> 00:09:06,000 +So we will say l and h. + +249 +00:09:06,000 --> 00:09:09,000 +So let's say this is l and this is h. + +250 +00:09:09,000 --> 00:09:13,000 +Now, normally we can say h is the last value, + +251 +00:09:13,000 --> 00:09:15,000 +or we can say it's a length minus one. + +252 +00:09:15,000 --> 00:09:16,000 +So we can say that. + +253 +00:09:16,000 --> 00:09:18,000 +So we have a low and we have a high. + +254 +00:09:18,000 --> 00:09:21,000 +So low starts from the first and h is the last value. + +255 +00:09:21,000 --> 00:09:23,000 +Now, how do you perform the operation? It's very simple. + +256 +00:09:23,000 --> 00:09:24,000 +First you find the pivot. + +257 +00:09:24,000 --> 00:09:26,000 +Now we know that we are going for pivot, + +258 +00:09:26,000 --> 00:09:27,000 +which is two in this case. + +259 +00:09:27,000 --> 00:09:31,000 +So what you will do is let's take this two as the pivot. + +260 +00:09:31,000 --> 00:09:32,000 +So I will say this is pi, + +261 +00:09:32,000 --> 00:09:35,000 +so this is referring this two. + +262 +00:09:35,000 --> 00:09:38,000 +So pi variable at this point is referring two. + +263 +00:09:38,000 --> 00:09:40,000 +So I will say this is two in this case. + +264 +00:09:40,000 --> 00:09:42,000 +And then we need two variables. + +265 +00:09:42,000 --> 00:09:44,000 +So let's take variable i. + +266 +00:09:44,000 --> 00:09:47,000 +Now by default, the value for i + +267 +00:09:47,000 --> 00:09:50,000 +will be the lowest value minus one. + +268 +00:09:50,000 --> 00:09:53,000 +So let's say the i value is here at this point. + +269 +00:09:53,000 --> 00:09:54,000 +You know what I will do? + +270 +00:09:54,000 --> 00:09:57,000 +Let me just remove this portion at this point + +271 +00:09:57,000 --> 00:09:59,000 +because let's keep the slate clean. + +272 +00:09:59,000 --> 00:10:02,000 +So we need a i variable and then we need a j variable. + +273 +00:10:02,000 --> 00:10:04,000 +So j will be here to start with. + +274 +00:10:04,000 --> 00:10:07,000 +So i will be here at the minus one position, + +275 +00:10:07,000 --> 00:10:09,000 +j will be here at the zeroth position, + +276 +00:10:09,000 --> 00:10:13,000 +and then what you do is you basically run a loop + +277 +00:10:13,000 --> 00:10:15,000 +where of course the i will also increment, + +278 +00:10:15,000 --> 00:10:19,000 +but the main loop here we want is for j, okay? + +279 +00:10:19,000 --> 00:10:21,000 +So we'll take a for loop for j. + +280 +00:10:21,000 --> 00:10:23,000 +Okay, so let me write the code here. + +281 +00:10:23,000 --> 00:10:25,000 +So what we want is, of course the rough code. + +282 +00:10:25,000 --> 00:10:28,000 +So I will say for, j will be starting with low, okay? + +283 +00:10:28,000 --> 00:10:30,000 +That's what we are doing here. + +284 +00:10:30,000 --> 00:10:32,000 +And then j should go till n, + +285 +00:10:32,000 --> 00:10:34,000 +but then we are not using n variable, right? + +286 +00:10:34,000 --> 00:10:35,000 +We are using variable high, + +287 +00:10:35,000 --> 00:10:39,000 +so let's say j less than high and j++. + +288 +00:10:39,000 --> 00:10:42,000 +Now, what you are going to do inside this loop? + +289 +00:10:42,000 --> 00:10:44,000 +So basically you will first make sure + +290 +00:10:44,000 --> 00:10:46,000 +that you get the pivot value, right? + +291 +00:10:46,000 --> 00:10:47,000 +So I mean, of course you have a pivot value here, + +292 +00:10:47,000 --> 00:10:51,000 +which is two, and then you also got i, i is still minus one. + +293 +00:10:51,000 --> 00:10:52,000 +Now inside this loop, + +294 +00:10:52,000 --> 00:10:56,000 +what you will do is you will check if the arr, + +295 +00:10:56,000 --> 00:10:59,000 +which is in this case we have an array, if arr of j, + +296 +00:10:59,000 --> 00:11:01,000 +if it is less than the pivot. + +297 +00:11:01,000 --> 00:11:02,000 +So pivot, we have a variable here. + +298 +00:11:02,000 --> 00:11:05,000 +If it is less than pivot, you will perform some operation. + +299 +00:11:05,000 --> 00:11:08,000 +Now if you compare the values here, do you think + +300 +00:11:08,000 --> 00:11:12,000 +the arr of j, which is five in this case, + +301 +00:11:12,000 --> 00:11:15,000 +is it less than the pivot because pivot value is two? + +302 +00:11:15,000 --> 00:11:17,000 +No, it is not less than pivot, + +303 +00:11:17,000 --> 00:11:19,000 +so we'll not do anything. + +304 +00:11:19,000 --> 00:11:21,000 +But we have to find the value which is less than two, right? + +305 +00:11:21,000 --> 00:11:24,000 +So if the value five is less than two, + +306 +00:11:24,000 --> 00:11:25,000 +you will perform some operation. + +307 +00:11:25,000 --> 00:11:26,000 +Since it is not true, + +308 +00:11:26,000 --> 00:11:28,000 +we are not going to perform any operation here. + +309 +00:11:28,000 --> 00:11:31,000 +So basically you are going to shift your j here, okay? + +310 +00:11:31,000 --> 00:11:33,000 +Now why we are doing it? + +311 +00:11:33,000 --> 00:11:34,000 +Of course we are running a loop, right? + +312 +00:11:34,000 --> 00:11:37,000 +So j starts with low and then we have to increment. + +313 +00:11:37,000 --> 00:11:40,000 +So j goes there, it will compare itself with the pivot. + +314 +00:11:40,000 --> 00:11:41,000 +Is it small? No. + +315 +00:11:41,000 --> 00:11:43,000 +So again, you will shift your j. + +316 +00:11:43,000 --> 00:11:45,000 +Then you compare six with two. + +317 +00:11:45,000 --> 00:11:47,000 +Then is it smaller? No. + +318 +00:11:47,000 --> 00:11:49,000 +Then you shift your j here. + +319 +00:11:49,000 --> 00:11:50,000 +Then you check. + +320 +00:11:50,000 --> 00:11:52,000 +Is it smaller? Yes. + +321 +00:11:52,000 --> 00:11:53,000 +Want if it is smaller? + +322 +00:11:53,000 --> 00:11:54,000 +Then what you will do? + +323 +00:11:54,000 --> 00:11:57,000 +In this case, if it is smaller, you perform some operation. + +324 +00:11:57,000 --> 00:11:59,000 +First thing you do is you increment the value of i. + +325 +00:11:59,000 --> 00:12:04,000 +Basically what you're doing is you are shifting your i here. + +326 +00:12:05,000 --> 00:12:05,000 +Okay, that's done. + +327 +00:12:05,000 --> 00:12:08,000 +Next, we have to make sure that you swap + +328 +00:12:08,000 --> 00:12:10,000 +the value of i and j, okay? + +329 +00:12:10,000 --> 00:12:14,000 +So whatever value you have with i and j, + +330 +00:12:14,000 --> 00:12:15,000 +you basically swap them. + +331 +00:12:15,000 --> 00:12:19,000 +So what I'm saying is take this value here, + +332 +00:12:19,000 --> 00:12:24,000 +move this value five here and move this one here, okay? + +333 +00:12:24,000 --> 00:12:25,000 +Now how do we do that? + +334 +00:12:25,000 --> 00:12:27,000 +Of course, we have used it before. + +335 +00:12:27,000 --> 00:12:28,000 +So we can use a temp variable, + +336 +00:12:28,000 --> 00:12:30,000 +or maybe at this point I will simply say swap + +337 +00:12:30,000 --> 00:12:32,000 +and then we'll write the logic later. + +338 +00:12:32,000 --> 00:12:37,000 +So I want to swap basically the value of arr of i + +339 +00:12:37,000 --> 00:12:40,000 +with arr of j, okay? + +340 +00:12:40,000 --> 00:12:42,000 +So we need to do the swapping, that's it. + +341 +00:12:42,000 --> 00:12:45,000 +These are the thing we are doing inside the if condition. + +342 +00:12:45,000 --> 00:12:46,000 +Now, once that is done, + +343 +00:12:46,000 --> 00:12:47,000 +of course you have to move your j again + +344 +00:12:47,000 --> 00:12:49,000 +because we are in a loop. + +345 +00:12:49,000 --> 00:12:51,000 +So j will go here. + +346 +00:12:51,000 --> 00:12:52,000 +Now again, you will compare, + +347 +00:12:52,000 --> 00:12:54,000 +if the four is less than two. + +348 +00:12:54,000 --> 00:12:56,000 +No, and that's it. + +349 +00:12:56,000 --> 00:12:58,000 +j will move here, because if it is not less, + +350 +00:12:58,000 --> 00:13:00,000 +then we are not doing any operation. + +351 +00:13:00,000 --> 00:13:02,000 +Then your j goes to the last value + +352 +00:13:02,000 --> 00:13:05,000 +because your j ends at high, right? + +353 +00:13:05,000 --> 00:13:09,000 +So j reaches here and of course the pi and j is same. + +354 +00:13:11,000 --> 00:13:13,000 +I mean of course it is false. + +355 +00:13:13,000 --> 00:13:15,000 +This condition will be false, which is arr of j + +356 +00:13:15,000 --> 00:13:16,000 +is not less than pivot. + +357 +00:13:16,000 --> 00:13:18,000 +So in this case it will not swap. + +358 +00:13:18,000 --> 00:13:20,000 +But what happens after that? + +359 +00:13:20,000 --> 00:13:22,000 +Even if you complete the entire loop, + +360 +00:13:22,000 --> 00:13:24,000 +do you think two, is it the right position? + +361 +00:13:24,000 --> 00:13:26,000 +No, two should be here, right? + +362 +00:13:26,000 --> 00:13:29,000 +So this three need to go there. + +363 +00:13:29,000 --> 00:13:31,000 +So what you do is you perform one more operation + +364 +00:13:31,000 --> 00:13:32,000 +after the loop. + +365 +00:13:32,000 --> 00:13:33,000 +So after the loop what you will do + +366 +00:13:33,000 --> 00:13:38,000 +is you will basically swap the arr of i+1 + +367 +00:13:38,000 --> 00:13:41,000 +with the arr of high. + +368 +00:13:41,000 --> 00:13:43,000 +Okay, now this is the operation you have to perform. + +369 +00:13:43,000 --> 00:13:45,000 +Now what will be happening here? + +370 +00:13:45,000 --> 00:13:46,000 +So what is the value of i? + +371 +00:13:46,000 --> 00:13:48,000 +Value of i is zero, which is index? + +372 +00:13:48,000 --> 00:13:50,000 +You are picking the next value, which is three. + +373 +00:13:50,000 --> 00:13:52,000 +So you have to swap this three with this two + +374 +00:13:52,000 --> 00:13:55,000 +because that's what your arr of high is. + +375 +00:13:55,000 --> 00:13:58,000 +So you will move here and you will move this side. + +376 +00:13:58,000 --> 00:14:01,000 +Now, by doing this, we made sure that the pivot value + +377 +00:14:01,000 --> 00:14:05,000 +which you choose is at the right position. + +378 +00:14:05,000 --> 00:14:06,000 +Can you confirm, is it the right position? + +379 +00:14:06,000 --> 00:14:08,000 +Yes, it's at the right position. + +380 +00:14:08,000 --> 00:14:10,000 +Now what you do is you divide, + +381 +00:14:10,000 --> 00:14:12,000 +I mean if you observe the array, + +382 +00:14:12,000 --> 00:14:14,000 +if you look at the pi, which is pivot here, + +383 +00:14:14,000 --> 00:14:16,000 +now if you see the left hand side of it, + +384 +00:14:16,000 --> 00:14:19,000 +this is always lower than two + +385 +00:14:19,000 --> 00:14:21,000 +and this value is always greater than the two. + +386 +00:14:21,000 --> 00:14:23,000 +So yes, two is the right position. + +387 +00:14:23,000 --> 00:14:24,000 +Now what's the next step? + +388 +00:14:24,000 --> 00:14:26,000 +The next step is very simple. + +389 +00:14:26,000 --> 00:14:30,000 +You divide this array based on the pivot value. + +390 +00:14:30,000 --> 00:14:33,000 +So what you do is you create another array here, + +391 +00:14:33,000 --> 00:14:35,000 +and in this you'll be having only one value, + +392 +00:14:35,000 --> 00:14:38,000 +which is coming from here, right? + +393 +00:14:38,000 --> 00:14:40,000 +In fact, we can also draw an arrow there. + +394 +00:14:40,000 --> 00:14:41,000 +So this is coming here. + +395 +00:14:41,000 --> 00:14:43,000 +Two will remain there itself. + +396 +00:14:43,000 --> 00:14:44,000 +I can write two here. + +397 +00:14:44,000 --> 00:14:47,000 +We don't have to work with two because it's already sorted. + +398 +00:14:47,000 --> 00:14:49,000 +And then you take this particular section + +399 +00:14:49,000 --> 00:14:51,000 +into another array, + +400 +00:14:51,000 --> 00:14:53,000 +not another array, but let's say logical partition + +401 +00:14:53,000 --> 00:14:54,000 +we are doing here. + +402 +00:14:54,000 --> 00:14:56,000 +So we'll get another array here. + +403 +00:14:56,000 --> 00:14:58,000 +So we are doing divide and conquer. + +404 +00:14:58,000 --> 00:15:02,000 +So here you're going to have 6, 5, 4, 3. + +405 +00:15:02,000 --> 00:15:04,000 +If you observe they're not sorted, + +406 +00:15:04,000 --> 00:15:06,000 +but at least we got the partition, right? + +407 +00:15:06,000 --> 00:15:09,000 +Now what you do is you again perform the same operation + +408 +00:15:09,000 --> 00:15:11,000 +which we have done here in the first example. + +409 +00:15:11,000 --> 00:15:14,000 +Now you help me, should we start with this? + +410 +00:15:14,000 --> 00:15:15,000 +Yes, we can start, + +411 +00:15:15,000 --> 00:15:17,000 +but don't you think we only have one value? + +412 +00:15:17,000 --> 00:15:19,000 +And whenever you have one value, it is always sorted. + +413 +00:15:19,000 --> 00:15:20,000 +Let's work with this. + +414 +00:15:20,000 --> 00:15:23,000 +So what you do is you again take the same thing. + +415 +00:15:23,000 --> 00:15:26,000 +So you take the low, you take the high, + +416 +00:15:26,000 --> 00:15:30,000 +and then of course you again have all these values here. + +417 +00:15:30,000 --> 00:15:31,000 +So all these variables stay there + +418 +00:15:31,000 --> 00:15:35,000 +because you are going to apply quick sort again + +419 +00:15:35,000 --> 00:15:37,000 +on these particular values. + +420 +00:15:37,000 --> 00:15:38,000 +So let's get started. + +421 +00:15:38,000 --> 00:15:40,000 +Let's take pivot, which pivot we are choosing? + +422 +00:15:40,000 --> 00:15:42,000 +Of course we're choosing always the last value. + +423 +00:15:42,000 --> 00:15:44,000 +So this is your pi, pivot. + +424 +00:15:44,000 --> 00:15:46,000 +Then you got i here, you got j here. + +425 +00:15:46,000 --> 00:15:49,000 +Then what you do is you start running your algorithm, + +426 +00:15:49,000 --> 00:15:53,000 +which is first you compare the value of j, + +427 +00:15:53,000 --> 00:15:54,000 +which is six in this case. + +428 +00:15:54,000 --> 00:15:57,000 +Is six smaller than three? + +429 +00:15:57,000 --> 00:15:59,000 +Look at the algorithm which we have done. + +430 +00:15:59,000 --> 00:16:03,000 +Is six smaller than the pivot? + +431 +00:16:03,000 --> 00:16:04,000 +The answer is no, + +432 +00:16:04,000 --> 00:16:08,000 +so you will simply move your j here. + +433 +00:16:08,000 --> 00:16:11,000 +Again, check, is it small? No. + +434 +00:16:11,000 --> 00:16:13,000 +Shift, is it small? No. + +435 +00:16:13,000 --> 00:16:14,000 +Is it? Then again goes here. + +436 +00:16:14,000 --> 00:16:16,000 +Is it small? No. + +437 +00:16:16,000 --> 00:16:17,000 +Then what you do + +438 +00:16:17,000 --> 00:16:19,000 +is you have not done any swapping, + +439 +00:16:19,000 --> 00:16:21,000 +but at the end we got one more logic, right? + +440 +00:16:21,000 --> 00:16:23,000 +So once you complete this for loop, + +441 +00:16:23,000 --> 00:16:25,000 +you will do something with this, + +442 +00:16:25,000 --> 00:16:28,000 +which is swapping the i+1. + +443 +00:16:28,000 --> 00:16:30,000 +So i is at minus one. + +444 +00:16:30,000 --> 00:16:33,000 +So you have to basically swap the six + +445 +00:16:33,000 --> 00:16:35,000 +with the aray of high, which is three. + +446 +00:16:35,000 --> 00:16:38,000 +So this goes here and this goes here. + +447 +00:16:38,000 --> 00:16:42,000 +Now if you look at the pivot, this is sorted, right? + +448 +00:16:42,000 --> 00:16:45,000 +So now what you do is you write three here, + +449 +00:16:45,000 --> 00:16:46,000 +which is your pivot value, + +450 +00:16:46,000 --> 00:16:49,000 +and then you divide your array into two parts. + +451 +00:16:49,000 --> 00:16:51,000 +But since the pivot was the first value, + +452 +00:16:51,000 --> 00:16:54,000 +what you will do is you will get the new array now + +453 +00:16:54,000 --> 00:16:59,000 +from this, which is five, four, six, + +454 +00:16:59,000 --> 00:17:01,000 +and then you start performing operation here. + +455 +00:17:01,000 --> 00:17:02,000 +Now what operation? + +456 +00:17:02,000 --> 00:17:03,000 +The same operation which we have done. + +457 +00:17:03,000 --> 00:17:06,000 +Again, you'll apply quick sort. + +458 +00:17:06,000 --> 00:17:08,000 +So this is l, this is h. + +459 +00:17:08,000 --> 00:17:11,000 +So pi is referring six here, and then you start. + +460 +00:17:11,000 --> 00:17:13,000 +So you've got i here, you've got j, + +461 +00:17:13,000 --> 00:17:15,000 +then you start comparing. + +462 +00:17:15,000 --> 00:17:16,000 +Is the first value, the value of j, + +463 +00:17:16,000 --> 00:17:18,000 +is it smaller then the pivot? + +464 +00:17:18,000 --> 00:17:19,000 +Yes. + +465 +00:17:19,000 --> 00:17:20,000 +Now, when you say yes, + +466 +00:17:20,000 --> 00:17:22,000 +what you will simply do is you will increment + +467 +00:17:22,000 --> 00:17:23,000 +the value of i first. + +468 +00:17:23,000 --> 00:17:24,000 +If you look at the code, + +469 +00:17:24,000 --> 00:17:27,000 +that's what the logic we have. + +470 +00:17:27,000 --> 00:17:32,000 +And then you swap the value of i and j. + +471 +00:17:32,000 --> 00:17:34,000 +Now in this case, i and j are referring to the same value. + +472 +00:17:34,000 --> 00:17:36,000 +So swapping doesn't make sense. + +473 +00:17:36,000 --> 00:17:37,000 +Of course they will get the same value. + +474 +00:17:37,000 --> 00:17:39,000 +Then you increment the value of j, j goes here. + +475 +00:17:39,000 --> 00:17:42,000 +Check, is four less than six? Yes. + +476 +00:17:42,000 --> 00:17:44,000 +In that case you'll move i here + +477 +00:17:44,000 --> 00:17:46,000 +and then you will swap the value of i and j. + +478 +00:17:46,000 --> 00:17:48,000 +Again, it remains same, so nothing to do here. + +479 +00:17:48,000 --> 00:17:51,000 +Then you shift your j here. + +480 +00:17:51,000 --> 00:17:55,000 +Of course, the array of j is not less than pivot + +481 +00:17:55,000 --> 00:17:56,000 +because they're the same value. + +482 +00:17:56,000 --> 00:17:59,000 +Then this loop end, the for loop ends. + +483 +00:17:59,000 --> 00:18:00,000 +Then you perform this operation, + +484 +00:18:00,000 --> 00:18:03,000 +which is arr, which is plus one, + +485 +00:18:03,000 --> 00:18:07,000 +this will be replaced by high, and that remains same, right? + +486 +00:18:07,000 --> 00:18:08,000 +So there's no swapping. + +487 +00:18:08,000 --> 00:18:10,000 +I know, it all depend upon + +488 +00:18:10,000 --> 00:18:11,000 +the type of values you're getting, + +489 +00:18:11,000 --> 00:18:13,000 +so at this point there's no swapping happened. + +490 +00:18:13,000 --> 00:18:15,000 +So this remain as it is, six, + +491 +00:18:15,000 --> 00:18:18,000 +but then you will get a new array here, + +492 +00:18:18,000 --> 00:18:21,000 +which only has five and four. + +493 +00:18:21,000 --> 00:18:22,000 +And then you start applying the same operation. + +494 +00:18:22,000 --> 00:18:26,000 +Let's get started, in fact I will just write this bit up + +495 +00:18:26,000 --> 00:18:28,000 +so that I will have some space, + +496 +00:18:28,000 --> 00:18:31,000 +and this is coming from this particular section. + +497 +00:18:31,000 --> 00:18:33,000 +Then what you do is you'll take the same value, + +498 +00:18:33,000 --> 00:18:35,000 +and if you observe, you are doing recursion, right? + +499 +00:18:35,000 --> 00:18:37,000 +The same steps we are repeating multiple times. + +500 +00:18:37,000 --> 00:18:41,000 +So l, h, we got pi here, + +501 +00:18:41,000 --> 00:18:44,000 +we got the variable i referring to minus one position, + +502 +00:18:44,000 --> 00:18:45,000 +or in fact not minus one position, + +503 +00:18:45,000 --> 00:18:48,000 +it is one less than the low. + +504 +00:18:48,000 --> 00:18:51,000 +So in this case, the low is this position, right? + +505 +00:18:51,000 --> 00:18:52,000 +Now what is this position? + +506 +00:18:53,000 --> 00:18:55,000 +In fact we should have maintained the index value, right? + +507 +00:18:55,000 --> 00:18:58,000 +So this is zero, this is one, + +508 +00:18:58,000 --> 00:19:01,000 +this is two, three, four, five. + +509 +00:19:01,000 --> 00:19:04,000 +Then we got three here, which is this one. + +510 +00:19:04,000 --> 00:19:07,000 +This is three, four, five. + +511 +00:19:07,000 --> 00:19:08,000 +What we got here is three, four, + +512 +00:19:08,000 --> 00:19:10,000 +so these are the index values for them. + +513 +00:19:10,000 --> 00:19:12,000 +Okay, so we got i referring to the index value two + +514 +00:19:12,000 --> 00:19:14,000 +and j is here. + +515 +00:19:14,000 --> 00:19:16,000 +Then what you do is you simply compare, + +516 +00:19:16,000 --> 00:19:17,000 +the same steps which we have done. + +517 +00:19:17,000 --> 00:19:20,000 +So the array of j, is it less than four? No. + +518 +00:19:20,000 --> 00:19:24,000 +So all this operation, this swapping is not needed. + +519 +00:19:24,000 --> 00:19:26,000 +You will simply move your j here. + +520 +00:19:26,000 --> 00:19:27,000 +Again, do we need to swap? + +521 +00:19:27,000 --> 00:19:29,000 +Of course not, because the array of j + +522 +00:19:29,000 --> 00:19:32,000 +is not less than pivot, so it'll remain same. + +523 +00:19:32,000 --> 00:19:34,000 +But then you will perform the last operation, + +524 +00:19:34,000 --> 00:19:35,000 +which is this one. + +525 +00:19:35,000 --> 00:19:40,000 +What it says, it says swap this value with the high value. + +526 +00:19:40,000 --> 00:19:42,000 +So this will be swapped. + +527 +00:19:42,000 --> 00:19:45,000 +So five goes here, this goes here, and this goes here. + +528 +00:19:45,000 --> 00:19:49,000 +Now if you observe, we divided and we conquered. + +529 +00:19:49,000 --> 00:19:52,000 +So if you merge this, in fact we are not merging physically, + +530 +00:19:52,000 --> 00:19:54,000 +we are merging logically, right? + +531 +00:19:54,000 --> 00:19:57,000 +Because no way we are we were breaking the array. + +532 +00:19:57,000 --> 00:19:59,000 +But if you see, if you try to merge logically, + +533 +00:19:59,000 --> 00:20:00,000 +what you will get? + +534 +00:20:00,000 --> 00:20:01,000 +So you will get this one first, + +535 +00:20:01,000 --> 00:20:04,000 +which is one goes here, then two goes here. + +536 +00:20:04,000 --> 00:20:06,000 +Then by sequence three goes here. + +537 +00:20:06,000 --> 00:20:10,000 +Then by sequence, this four, five goes here, + +538 +00:20:10,000 --> 00:20:13,000 +and then this six goes at the end. + +539 +00:20:13,000 --> 00:20:15,000 +So if I'm to write this, this will go here, + +540 +00:20:15,000 --> 00:20:17,000 +so let me write one. + +541 +00:20:17,000 --> 00:20:20,000 +Then this goes here, that's two. + +542 +00:20:20,000 --> 00:20:23,000 +Then three goes here, that's three. + +543 +00:20:23,000 --> 00:20:25,000 +Then if you can see, in the binary tree, + +544 +00:20:25,000 --> 00:20:27,000 +we always goes from the left to right. + +545 +00:20:27,000 --> 00:20:28,000 +So if you see the left, + +546 +00:20:28,000 --> 00:20:30,000 +because six is there on the right hand side, + +547 +00:20:30,000 --> 00:20:35,000 +you will say four and five, and at the end you will get six. + +548 +00:20:35,000 --> 00:20:36,000 +Now this is sorted. + +549 +00:20:36,000 --> 00:20:37,000 +So after all this explanation, + +550 +00:20:37,000 --> 00:20:40,000 +this is how basically you sort them. + +551 +00:20:40,000 --> 00:20:42,000 +Now this logic which we have written here + +552 +00:20:42,000 --> 00:20:45,000 +is actually a part of a partition method. + +553 +00:20:45,000 --> 00:20:47,000 +So this is a partition method, + +554 +00:20:47,000 --> 00:20:49,000 +so the entire thing goes there. + +555 +00:20:49,000 --> 00:20:52,000 +But we also need to return the pivot value, right? + +556 +00:20:52,000 --> 00:20:53,000 +So which is the pivot value? + +557 +00:20:53,000 --> 00:20:55,000 +So we'll say return i+1 + +558 +00:20:55,000 --> 00:20:57,000 +because that's where the pivot is, + +559 +00:20:57,000 --> 00:21:00,000 +but this particular function will be called by whom? + +560 +00:21:00,000 --> 00:21:02,000 +So we have a concept of quick sort here. + +561 +00:21:02,000 --> 00:21:04,000 +So quick sort is going to call this partition. + +562 +00:21:04,000 --> 00:21:06,000 +How? That we are going to see in the code itself. + +563 +00:21:06,000 --> 00:21:08,000 +So you got the idea, + +564 +00:21:08,000 --> 00:21:10,000 +basically you divide, sort each values, + +565 +00:21:10,000 --> 00:21:12,000 +and you will get the output. + +566 +00:21:12,000 --> 00:21:15,000 +Now this will make much more sense once we start coding, + +567 +00:21:15,000 --> 00:21:17,000 +and I hope it will get clear. + diff --git a/28 - DSA (Optional)/013 Quick Sort Code_en.srt b/28 - DSA (Optional)/013 Quick Sort Code_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..900214259b8c46a388e19dd05862b981e13023d2 --- /dev/null +++ b/28 - DSA (Optional)/013 Quick Sort Code_en.srt @@ -0,0 +1,744 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: So let's try to implement the quickSort logic. + +2 +00:00:02,000 --> 00:00:05,000 +So what you're gonna do here is we got these values here. + +3 +00:00:05,000 --> 00:00:07,000 +So this is the logic for the insertion. + +4 +00:00:07,000 --> 00:00:10,000 +So what I'll do is I will just remove the entire part here. + +5 +00:00:10,000 --> 00:00:14,000 +We got this added and we are going to perform the operation. + +6 +00:00:14,000 --> 00:00:16,000 +Now of course you can write the entire logic here, + +7 +00:00:16,000 --> 00:00:19,000 +but then remember quickSort goes for recursion? + +8 +00:00:19,000 --> 00:00:21,000 +So when you say a question, you have to create a function + +9 +00:00:21,000 --> 00:00:23,000 +or the method which will get called. + +10 +00:00:23,000 --> 00:00:25,000 +So what I will do is I will create a function here + +11 +00:00:25,000 --> 00:00:26,000 +and let's call them. + +12 +00:00:26,000 --> 00:00:29,000 +So we'll say a quickSort as a function name + +13 +00:00:29,000 --> 00:00:32,000 +or the method name, which will take three parameters. + +14 +00:00:32,000 --> 00:00:33,000 +Now what are the three parameters? + +15 +00:00:33,000 --> 00:00:36,000 +The first one is the array itself on which you want + +16 +00:00:36,000 --> 00:00:37,000 +to perform the operation. + +17 +00:00:37,000 --> 00:00:39,000 +Next is the starting point + +18 +00:00:39,000 --> 00:00:42,000 +and the ending point of each section. + +19 +00:00:42,000 --> 00:00:44,000 +Example, starting to the entire array, + +20 +00:00:44,000 --> 00:00:46,000 +but remember when we have to break it down? + +21 +00:00:46,000 --> 00:00:48,000 +So that is your each section, okay? + +22 +00:00:48,000 --> 00:00:51,000 +So we have to specify the low, + +23 +00:00:51,000 --> 00:00:53,000 +and then you have to specify the high. + +24 +00:00:53,000 --> 00:00:55,000 +Now in this case, what is low? + +25 +00:00:55,000 --> 00:00:57,000 +So low is zero, + +26 +00:00:57,000 --> 00:00:59,000 +and the high is basically + +27 +00:00:59,000 --> 00:01:02,000 +the array dot length minus one, right? + +28 +00:01:02,000 --> 00:01:04,000 +That's the value you have to send. Now basically we don't + +29 +00:01:04,000 --> 00:01:06,000 +have this function, so we can just right click here + +30 +00:01:06,000 --> 00:01:11,000 +and say, okay, I want the IE to create this method for me. + +31 +00:01:11,000 --> 00:01:12,000 +And we're just using some inbuilt methods. + +32 +00:01:12,000 --> 00:01:14,000 +Okay, let's create our own. + +33 +00:01:14,000 --> 00:01:15,000 +So what I will do here is I will get + +34 +00:01:15,000 --> 00:01:16,000 +a method called "public". + +35 +00:01:16,000 --> 00:01:17,000 +I don't want to get the objects + +36 +00:01:17,000 --> 00:01:19,000 +of the static void quickSort, + +37 +00:01:20,000 --> 00:01:22,000 +and it'll take these three values. + +38 +00:01:22,000 --> 00:01:25,000 +So first it'll take int array, in fact, one + +39 +00:01:25,000 --> 00:01:28,000 +of the syntax of array you can write it on this side + +40 +00:01:28,000 --> 00:01:29,000 +because it will make much more sense. + +41 +00:01:29,000 --> 00:01:32,000 +You got array and then you got int low, + +42 +00:01:32,000 --> 00:01:35,000 +then you got int high, so these are the variables you need + +43 +00:01:35,000 --> 00:01:37,000 +and then you can start performing the operation. + +44 +00:01:37,000 --> 00:01:39,000 +So basically from the main you're calling quickSort only + +45 +00:01:39,000 --> 00:01:41,000 +once, but quickSort will be calling itself. + +46 +00:01:41,000 --> 00:01:42,000 +Okay? But based on what? + +47 +00:01:42,000 --> 00:01:43,000 +Now basically I want quickSort + +48 +00:01:43,000 --> 00:01:47,000 +to call itself only till when you're low is less than high. + +49 +00:01:47,000 --> 00:01:48,000 +We don't want it, right? + +50 +00:01:48,000 --> 00:01:49,000 +So that's what it makes sense, right? + +51 +00:01:49,000 --> 00:01:51,000 +Low should be low, high should be high. + +52 +00:01:51,000 --> 00:01:53,000 +And if it is true, then keep calling the quickSort. + +53 +00:01:53,000 --> 00:01:56,000 +So I, I will keep calling the quickSort, + +54 +00:01:56,000 --> 00:01:59,000 +but the question is, "What values have to pass?" + +55 +00:01:59,000 --> 00:02:00,000 +Now think about this: + +56 +00:02:00,000 --> 00:02:02,000 +If you have one big array, + +57 +00:02:02,000 --> 00:02:03,000 +and then you are saying that we'll be + +58 +00:02:03,000 --> 00:02:05,000 +breaking this into two parts, + +59 +00:02:05,000 --> 00:02:08,000 +so don't you think you have to call quickSort two times + +60 +00:02:08,000 --> 00:02:09,000 +for two different arrays? + +61 +00:02:09,000 --> 00:02:11,000 +That's right, so you have to call it two times. + +62 +00:02:11,000 --> 00:02:13,000 +But the question is "What are the values?" + +63 +00:02:13,000 --> 00:02:15,000 +For sure, the first variable will be array + +64 +00:02:15,000 --> 00:02:17,000 +because that's what you are sending, right? + +65 +00:02:17,000 --> 00:02:19,000 +We have to sort the array. The question is, + +66 +00:02:19,000 --> 00:02:21,000 +"What will be low? What will be the high?" + +67 +00:02:21,000 --> 00:02:23,000 +Because when you divide your array into + +68 +00:02:23,000 --> 00:02:24,000 +two parts, what should be the starting point + +69 +00:02:24,000 --> 00:02:26,000 +of the second array and the ending point + +70 +00:02:26,000 --> 00:02:27,000 +of the second array? + +71 +00:02:27,000 --> 00:02:28,000 +That's what you have to mention. + +72 +00:02:28,000 --> 00:02:29,000 +But how will you know the starting point? + +73 +00:02:29,000 --> 00:02:32,000 +It is easy actually. Here, the starting is always low + +74 +00:02:32,000 --> 00:02:34,000 +for the first array and the ending is always + +75 +00:02:34,000 --> 00:02:36,000 +high for the second array. + +76 +00:02:36,000 --> 00:02:38,000 +Okay, well that is solved. The problem is with the ending + +77 +00:02:38,000 --> 00:02:40,000 +of the first array and the starting of the second array, + +78 +00:02:40,000 --> 00:02:43,000 +that's what we need to find. Now how do you find this? + +79 +00:02:43,000 --> 00:02:45,000 +For this, we have to run a partition. Okay? + +80 +00:02:45,000 --> 00:02:47,000 +So this is the logic which we have to refer to. + +81 +00:02:47,000 --> 00:02:51,000 +So what I will do is I will just reduce the IE size so + +82 +00:02:51,000 --> 00:02:52,000 +that I can see those values. + +83 +00:02:52,000 --> 00:02:53,000 +I mean this logic. So this is what you have + +84 +00:02:53,000 --> 00:02:55,000 +to implement in the partition. + +85 +00:02:55,000 --> 00:02:57,000 +So basically to get this value, + +86 +00:02:57,000 --> 00:02:58,000 +the pivot value, what you will do is + +87 +00:02:58,000 --> 00:03:00,000 +you will say int pi equal to, + +88 +00:03:00,000 --> 00:03:03,000 +and you will create a method called "partition" in which you + +89 +00:03:03,000 --> 00:03:06,000 +will pass three values, the same three values + +90 +00:03:06,000 --> 00:03:08,000 +array low and high. + +91 +00:03:08,000 --> 00:03:10,000 +So basically the logic have not mentioned it, + +92 +00:03:10,000 --> 00:03:11,000 +but we want this value as well. + +93 +00:03:11,000 --> 00:03:14,000 +Okay, so we need to create this method. + +94 +00:03:14,000 --> 00:03:16,000 +So what I will do is I will just go back here, + +95 +00:03:16,000 --> 00:03:20,000 +more actions, create a method. Okay, so we got the method, + +96 +00:03:20,000 --> 00:03:22,000 +let's perform operation on this method. + +97 +00:03:22,000 --> 00:03:24,000 +We can make it private because we're not going + +98 +00:03:24,000 --> 00:03:26,000 +to use partition outside this class. + +99 +00:03:26,000 --> 00:03:29,000 +So private works, but what should be the logic here? + +100 +00:03:29,000 --> 00:03:30,000 +And we have to return something. + +101 +00:03:30,000 --> 00:03:32,000 +Remember we have to return the pi value. Okay, + +102 +00:03:32,000 --> 00:03:34,000 +so we can write the same logic which we have done here, + +103 +00:03:34,000 --> 00:03:38,000 +and once you get this pi, you can basically pass the pi + +104 +00:03:38,000 --> 00:03:40,000 +and we don't want to include the pi. + +105 +00:03:40,000 --> 00:03:42,000 +So we can set pi minus one. + +106 +00:03:42,000 --> 00:03:45,000 +And here, we have to pass pi plus one + +107 +00:03:45,000 --> 00:03:47,000 +because every partition, when you create, + +108 +00:03:47,000 --> 00:03:47,000 +you don't consider the pi, right? + +109 +00:03:47,000 --> 00:03:50,000 +Pi stays there. You create two parts + +110 +00:03:50,000 --> 00:03:53,000 +and then those are your two partitions. + +111 +00:03:53,000 --> 00:03:55,000 +But we have to find this pi. That's the main task. + +112 +00:03:55,000 --> 00:03:57,000 +And for that we have to write this + +113 +00:03:57,000 --> 00:03:58,000 +logic, which is mentioned here. + +114 +00:03:58,000 --> 00:04:01,000 +So we need some variables. The first one is temporary pivot + +115 +00:04:01,000 --> 00:04:03,000 +variable, and we have to assume pivot for the first time. + +116 +00:04:03,000 --> 00:04:07,000 +So the pivot here is always high. + +117 +00:04:07,000 --> 00:04:08,000 +So that's the algorithm we are using, + +118 +00:04:08,000 --> 00:04:11,000 +but we can pick up the starting value + +119 +00:04:11,000 --> 00:04:13,000 +or randomly pick any value that works. + +120 +00:04:13,000 --> 00:04:15,000 +So let's say we got pivot high, + +121 +00:04:15,000 --> 00:04:17,000 +and then we also need one more variable, which is, + +122 +00:04:17,000 --> 00:04:20,000 +"I", remember? We have to use this "I", which will return. + +123 +00:04:20,000 --> 00:04:24,000 +So "I" is basically your low minus one. + +124 +00:04:24,000 --> 00:04:26,000 +Remember, if the, if the array starts with zero, + +125 +00:04:26,000 --> 00:04:28,000 +the "I" will be at minus one. + +126 +00:04:28,000 --> 00:04:30,000 +Next, we need a loop. + +127 +00:04:30,000 --> 00:04:32,000 +So we can get the entire loop from here, + +128 +00:04:32,000 --> 00:04:35,000 +or maybe you can type. So we can say four into to J is equal + +129 +00:04:35,000 --> 00:04:38,000 +to zero, in fact, not zero, right? + +130 +00:04:38,000 --> 00:04:39,000 +It should be always low + +131 +00:04:39,000 --> 00:04:42,000 +because when you create different partitions, + +132 +00:04:42,000 --> 00:04:43,000 +not every partition will start + +133 +00:04:43,000 --> 00:04:44,000 +with zero, but they will start with low. + +134 +00:04:44,000 --> 00:04:49,000 +So J less than equal to high and J plus plus. + +135 +00:04:49,000 --> 00:04:52,000 +I'm just referring to my algorithm, which I mentioned here. + +136 +00:04:52,000 --> 00:04:56,000 +And then the logic we have to write is if arr of J, + +137 +00:04:56,000 --> 00:04:58,000 +if it is less than pivot, + +138 +00:04:58,000 --> 00:05:01,000 +then, you start doing some operation. + +139 +00:05:01,000 --> 00:05:03,000 +What operation? First you increment the value of "I" + +140 +00:05:03,000 --> 00:05:04,000 +and then you swap. + +141 +00:05:04,000 --> 00:05:06,000 +But how will you swap? We don't have a swap function. + +142 +00:05:06,000 --> 00:05:08,000 +Actually we can create one, + +143 +00:05:08,000 --> 00:05:10,000 +but instead of that, what we can also do + +144 +00:05:10,000 --> 00:05:11,000 +is we can write the logic. + +145 +00:05:11,000 --> 00:05:15,000 +So I can say int temp is equal to arr of "I". + +146 +00:05:16,000 --> 00:05:21,000 +Then arr of "I" will be replaced by arr of J, + +147 +00:05:22,000 --> 00:05:26,000 +and then arr of J will be replaced by the temp. + +148 +00:05:26,000 --> 00:05:29,000 +Okay? That's the values we have to swap. And that's it. + +149 +00:05:29,000 --> 00:05:33,000 +This will run and you will do the partitioning part. + +150 +00:05:33,000 --> 00:05:35,000 +But once you complete the for loop, again, we have + +151 +00:05:35,000 --> 00:05:38,000 +to swap the value of "I" plus one with high. + +152 +00:05:38,000 --> 00:05:41,000 +So you can actually use the same logic here + +153 +00:05:41,000 --> 00:05:42,000 +or same statements. + +154 +00:05:42,000 --> 00:05:45,000 +So I'm not copy pasting, I'm just reusing the code. + +155 +00:05:45,000 --> 00:05:47,000 +Sounds better, right? + +156 +00:05:47,000 --> 00:05:51,000 +So here we can say this will be replaced by "I" plus one + +157 +00:05:51,000 --> 00:05:54,000 +and I plus one will be replaced by high, + +158 +00:05:54,000 --> 00:05:59,000 +and this high is going to be replaced by temp. Swiping done. + +159 +00:05:59,000 --> 00:06:02,000 +And at the end, we need to return "I" plus one + +160 +00:06:02,000 --> 00:06:04,000 +because that's what is referring to the pivot. + +161 +00:06:04,000 --> 00:06:06,000 +Okay? Looks like a right logic, + +162 +00:06:06,000 --> 00:06:08,000 +we just converted that into here. + +163 +00:06:08,000 --> 00:06:12,000 +So by doing the partition, we are saying replace one. + +164 +00:06:12,000 --> 00:06:14,000 +In fact, once you get this algorithm, + +165 +00:06:14,000 --> 00:06:15,000 +also try not the same thing, + +166 +00:06:15,000 --> 00:06:17,000 +which I've done on the board, okay? + +167 +00:06:17,000 --> 00:06:18,000 +Then it'll make much more sense. + +168 +00:06:18,000 --> 00:06:21,000 +Okay, I hope this will work. What do you think? + +169 +00:06:21,000 --> 00:06:26,000 +Let's run and let's see, run and yes, looks like sorted. + +170 +00:06:26,000 --> 00:06:30,000 +1, 2, 3, 4, 5, 6, 8. Let's change some values. + +171 +00:06:30,000 --> 00:06:33,000 +Let's say this is 81. + +172 +00:06:33,000 --> 00:06:35,000 +This is 62, + +173 +00:06:35,000 --> 00:06:38,000 +and this is 1, 1, 1. + +174 +00:06:39,000 --> 00:06:42,000 +Let's try with this values and let's see if they're sorted. + +175 +00:06:42,000 --> 00:06:45,000 +So 2, 3, 4, 5, 62, 81, 1, 1, 1. So it works. + +176 +00:06:45,000 --> 00:06:48,000 +So those, this is your quickSort. + +177 +00:06:48,000 --> 00:06:51,000 +Now why we are saying this is n log of n is + +178 +00:06:51,000 --> 00:06:55,000 +because we only have one loop here, which is for loop. + +179 +00:06:55,000 --> 00:06:57,000 +Plus, we are doing the partitions of it, right? + +180 +00:06:57,000 --> 00:06:58,000 +So they can run parallelly. + +181 +00:06:58,000 --> 00:07:00,000 +Yeah, so this is n log n, + +182 +00:07:00,000 --> 00:07:03,000 +but in the worst case it can go for n squared, + +183 +00:07:03,000 --> 00:07:06,000 +but better than the other sorting techniques. + +184 +00:07:06,000 --> 00:07:07,000 +So yeah, that's it from quickSort, + +185 +00:07:07,000 --> 00:07:08,000 +we have written the code in Java. + +186 +00:07:08,000 --> 00:07:11,000 +And see you in the next video. + diff --git a/28 - DSA (Optional)/014 Divide and Conquer_en.srt b/28 - DSA (Optional)/014 Divide and Conquer_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..089ebc73172c966605dc822fce247e8e71aafa01 --- /dev/null +++ b/28 - DSA (Optional)/014 Divide and Conquer_en.srt @@ -0,0 +1,432 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: In this video, we'll talk about divide + +2 +00:00:02,000 --> 00:00:03,000 +and conquer technique. + +3 +00:00:03,000 --> 00:00:05,000 +The thing is, in the upcoming algorithms + +4 +00:00:05,000 --> 00:00:06,000 +which you're going to learn, + +5 +00:00:06,000 --> 00:00:08,000 +we are going to use this technique. + +6 +00:00:08,000 --> 00:00:11,000 +So it makes sense to talk about this technique separately. + +7 +00:00:11,000 --> 00:00:14,000 +The thing is, when you talk about any algorithm, + +8 +00:00:14,000 --> 00:00:15,000 +what we are trying to do is we are trying + +9 +00:00:15,000 --> 00:00:17,000 +to solve a particular problem. + +10 +00:00:17,000 --> 00:00:19,000 +Now, of course, when you learn a particular algorithm, + +11 +00:00:19,000 --> 00:00:21,000 +we normally work with limited set of values, right? + +12 +00:00:21,000 --> 00:00:25,000 +Let's say five values, six values, or maybe 10 values. + +13 +00:00:25,000 --> 00:00:27,000 +But still, these are small numbers. + +14 +00:00:27,000 --> 00:00:29,000 +What if you are working with huge amount of data? + +15 +00:00:29,000 --> 00:00:31,000 +Now, that's where one of the issue which comes, + +16 +00:00:31,000 --> 00:00:33,000 +which is your time complexity, right? + +17 +00:00:33,000 --> 00:00:35,000 +So basically whatever algorithm we are learning, + +18 +00:00:35,000 --> 00:00:38,000 +we are trying to understand which algorithm goes + +19 +00:00:38,000 --> 00:00:40,000 +for what time complexity? + +20 +00:00:40,000 --> 00:00:41,000 +So basically when we talk about bubble sort, + +21 +00:00:41,000 --> 00:00:43,000 +when you talk about selection sort, + +22 +00:00:43,000 --> 00:00:46,000 +they have a different time complexity which is much higher, + +23 +00:00:46,000 --> 00:00:48,000 +and on the other hand, when you talk about quick sort, + +24 +00:00:48,000 --> 00:00:50,000 +it is much more time efficient. + +25 +00:00:50,000 --> 00:00:51,000 +So how does that really work? + +26 +00:00:51,000 --> 00:00:53,000 +So what they do is they basically take a big task, + +27 +00:00:53,000 --> 00:00:56,000 +now, it can be for small numbers as well, + +28 +00:00:56,000 --> 00:00:57,000 +you take a task, you take a problem, + +29 +00:00:57,000 --> 00:00:59,000 +and you try to find a solution of it. + +30 +00:00:59,000 --> 00:01:01,000 +Of course, every algorithm does that. + +31 +00:01:01,000 --> 00:01:05,000 +So we have a problem, and we find a solution for it. + +32 +00:01:05,000 --> 00:01:07,000 +Now, in divide and conquer, as a name suggests, + +33 +00:01:07,000 --> 00:01:11,000 +what you do is you divide first, and then you conquer, + +34 +00:01:11,000 --> 00:01:12,000 +and then you combine. + +35 +00:01:12,000 --> 00:01:16,000 +Now what it means, let's say we have a big problem called P. + +36 +00:01:16,000 --> 00:01:17,000 +Now what you will do is you will break it down + +37 +00:01:17,000 --> 00:01:19,000 +into subsections. + +38 +00:01:19,000 --> 00:01:21,000 +So you will say P1, P2, and list goes on. + +39 +00:01:21,000 --> 00:01:22,000 +And then what you will do is, + +40 +00:01:22,000 --> 00:01:26,000 +you will apply the algorithm on each subsection, + +41 +00:01:26,000 --> 00:01:29,000 +so on P1, P2, P3, + +42 +00:01:29,000 --> 00:01:31,000 +and then what you will get is a solution + +43 +00:01:31,000 --> 00:01:32,000 +for that particular subproblem. + +44 +00:01:32,000 --> 00:01:35,000 +So by doing this, you are trying to solve a big problem + +45 +00:01:35,000 --> 00:01:37,000 +by breaking it down into small parts. + +46 +00:01:37,000 --> 00:01:40,000 +And then, what you will do is you will simply solve it, + +47 +00:01:40,000 --> 00:01:42,000 +each subproblem, and then you will merge, + +48 +00:01:42,000 --> 00:01:43,000 +you will combine them, + +49 +00:01:43,000 --> 00:01:45,000 +and that's where you will get a solution + +50 +00:01:45,000 --> 00:01:48,000 +or a big solution for the big problem, right? + +51 +00:01:48,000 --> 00:01:51,000 +Now, what if the subsection itself is huge? + +52 +00:01:51,000 --> 00:01:53,000 +So let's say when you say P, that's a big problem. + +53 +00:01:53,000 --> 00:01:56,000 +P1, P2, what if even they are huge? + +54 +00:01:56,000 --> 00:01:57,000 +Now, in that case, + +55 +00:01:57,000 --> 00:02:00,000 +what you will do is you will again divide them to sub parts. + +56 +00:02:00,000 --> 00:02:02,000 +Something like P11 and P21, + +57 +00:02:02,000 --> 00:02:04,000 +or maybe you can have P1 goes + +58 +00:02:04,000 --> 00:02:07,000 +for P11, P12, so those are the subproblems, + +59 +00:02:07,000 --> 00:02:09,000 +and then you try to solve them + +60 +00:02:09,000 --> 00:02:11,000 +so that it will be easier to solve. + +61 +00:02:11,000 --> 00:02:13,000 +Now that's what we call divide. + +62 +00:02:13,000 --> 00:02:15,000 +And of course, when you divide and conquer, + +63 +00:02:15,000 --> 00:02:16,000 +don't forget to combine. + +64 +00:02:16,000 --> 00:02:18,000 +And we have to also make sure + +65 +00:02:18,000 --> 00:02:19,000 +that after dividing this problem, + +66 +00:02:19,000 --> 00:02:21,000 +after solving them, when you combine, + +67 +00:02:21,000 --> 00:02:23,000 +you get a desired result. + +68 +00:02:23,000 --> 00:02:26,000 +Maybe when you solve a particular problem, the subsection, + +69 +00:02:26,000 --> 00:02:26,000 +they are working. + +70 +00:02:26,000 --> 00:02:29,000 +You got the output, which is desired for subproblem, + +71 +00:02:29,000 --> 00:02:31,000 +but what about when you combine it, + +72 +00:02:31,000 --> 00:02:32,000 +you got some different answer? + +73 +00:02:32,000 --> 00:02:33,000 +Even that will not work. + +74 +00:02:33,000 --> 00:02:36,000 +So you have to make sure that when you combine the work, + +75 +00:02:36,000 --> 00:02:38,000 +you will get your desired results. + +76 +00:02:38,000 --> 00:02:40,000 +Now, most of the people have this confusion + +77 +00:02:40,000 --> 00:02:41,000 +when you talk about divide and conquer, + +78 +00:02:41,000 --> 00:02:44,000 +that we can divide the task the way we want, right? + +79 +00:02:44,000 --> 00:02:45,000 +No. + +80 +00:02:45,000 --> 00:02:46,000 +So basically, whatever algorithm you're going to apply + +81 +00:02:46,000 --> 00:02:48,000 +on the big problem, + +82 +00:02:48,000 --> 00:02:51,000 +the same algorithm should work on the each subsection. + +83 +00:02:51,000 --> 00:02:52,000 +So let's say an example, let's say if I want + +84 +00:02:52,000 --> 00:02:54,000 +to paint the entire house, + +85 +00:02:54,000 --> 00:02:55,000 +let's say we have four rooms there + +86 +00:02:55,000 --> 00:02:58,000 +and that I want to paint each room. + +87 +00:02:58,000 --> 00:03:00,000 +So basically, painting the entire house is a big problem. + +88 +00:03:00,000 --> 00:03:03,000 +So what you will do is you will divide the task. + +89 +00:03:03,000 --> 00:03:05,000 +Now, if you're thinking, dividing tasks simply means + +90 +00:03:05,000 --> 00:03:08,000 +that someone will buy the paint, + +91 +00:03:08,000 --> 00:03:12,000 +someone will apply the paint, someone will clean the floors, + +92 +00:03:12,000 --> 00:03:14,000 +no, that's not the task. + +93 +00:03:14,000 --> 00:03:15,000 +What we can do is, + +94 +00:03:15,000 --> 00:03:18,000 +instead of painting the entire house, instead of, + +95 +00:03:18,000 --> 00:03:20,000 +I mean of course you have to paint the entire house, + +96 +00:03:20,000 --> 00:03:21,000 +but you can divide the problem, + +97 +00:03:21,000 --> 00:03:23,000 +you can say, okay, we have full house, + +98 +00:03:23,000 --> 00:03:26,000 +let's divide it, the four rooms into four separate parts, + +99 +00:03:26,000 --> 00:03:27,000 +paint each room. + +100 +00:03:27,000 --> 00:03:30,000 +Of course, in each room you will buy the paint, + +101 +00:03:30,000 --> 00:03:32,000 +you will apply the paint, you will clean the floor. + +102 +00:03:32,000 --> 00:03:34,000 +I only remember those three steps. + +103 +00:03:34,000 --> 00:03:36,000 +So basically, you will do it for each room. + +104 +00:03:36,000 --> 00:03:39,000 +So what you have done is you have divided the big problem, + +105 +00:03:39,000 --> 00:03:41,000 +solved it, and then you combine. + +106 +00:03:41,000 --> 00:03:43,000 +Of course, you have to also combine those four rooms + +107 +00:03:43,000 --> 00:03:45,000 +to get a house, right? + +108 +00:03:45,000 --> 00:03:47,000 +That's what divide and conquer is. + diff --git a/28 - DSA (Optional)/015 Tree Introduction_en.srt b/28 - DSA (Optional)/015 Tree Introduction_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..7d873e276bef74985788ea69a30214d271c9d174 --- /dev/null +++ b/28 - DSA (Optional)/015 Tree Introduction_en.srt @@ -0,0 +1,204 @@ +1 +00:00:00,000 --> 00:00:03,000 +-: Now let's talk about tree structure. + +2 +00:00:03,000 --> 00:00:05,000 +Of course, we'll be having a separate section + +3 +00:00:05,000 --> 00:00:08,000 +for trees because it's a huge topic, + +4 +00:00:08,000 --> 00:00:10,000 +but just want to introduce it. + +5 +00:00:10,000 --> 00:00:12,000 +Now, basically when you talk about list or arrays, + +6 +00:00:12,000 --> 00:00:14,000 +those are your linear structure + +7 +00:00:14,000 --> 00:00:16,000 +because you store data one-by-one. + +8 +00:00:16,000 --> 00:00:18,000 +Now, in the upcoming algorithm, + +9 +00:00:18,000 --> 00:00:20,000 +somewhere we are going to use something + +10 +00:00:20,000 --> 00:00:21,000 +like a tree structure. + +11 +00:00:21,000 --> 00:00:23,000 +Now, what exactly a tree is? + +12 +00:00:23,000 --> 00:00:24,000 +In English term, what is tree? + +13 +00:00:24,000 --> 00:00:27,000 +Basically, you have a root and then you have branches. + +14 +00:00:27,000 --> 00:00:29,000 +In the same way, what we are going to have here + +15 +00:00:29,000 --> 00:00:31,000 +is we'll be having a root element. + +16 +00:00:31,000 --> 00:00:34,000 +So we can say, let's say we have a list of values. + +17 +00:00:34,000 --> 00:00:36,000 +So these values are your root + +18 +00:00:36,000 --> 00:00:38,000 +and then what you do is you do certain things + +19 +00:00:38,000 --> 00:00:40,000 +with these values and you get some new branches. + +20 +00:00:40,000 --> 00:00:43,000 +Okay, so maybe you get some subsections + +21 +00:00:43,000 --> 00:00:44,000 +or maybe you clone it. + +22 +00:00:44,000 --> 00:00:46,000 +It doesn't matter. The logic doesn't matter here. + +23 +00:00:46,000 --> 00:00:47,000 +Understand the structure. + +24 +00:00:47,000 --> 00:00:49,000 +So what you do is you got a root element + +25 +00:00:49,000 --> 00:00:51,000 +and then from that you create a branch. + +26 +00:00:51,000 --> 00:00:53,000 +Maybe you can create one branch, two branch, + +27 +00:00:53,000 --> 00:00:55,000 +three branch, 10 branch, don't matter. + +28 +00:00:55,000 --> 00:00:56,000 +You create the branches. + +29 +00:00:56,000 --> 00:00:57,000 +So from that particular branch, + +30 +00:00:57,000 --> 00:00:59,000 +maybe you can create more branches + +31 +00:00:59,000 --> 00:01:02,000 +and when you do that, you get a tree structure. + +32 +00:01:02,000 --> 00:01:03,000 +Of course, we have different type of tree structure. + +33 +00:01:03,000 --> 00:01:05,000 +We also have something called a binary tree, + +34 +00:01:05,000 --> 00:01:08,000 +where the number of branches for each node + +35 +00:01:08,000 --> 00:01:10,000 +of the element will be only two, okay? + +36 +00:01:10,000 --> 00:01:13,000 +So when you have multiple branches, that's normal tree, + +37 +00:01:13,000 --> 00:01:15,000 +but when you have two branches, that's your binary tree. + +38 +00:01:15,000 --> 00:01:18,000 +So don't get surprised when we see difficult structure + +39 +00:01:18,000 --> 00:01:19,000 +in the upcoming algorithms + +40 +00:01:19,000 --> 00:01:21,000 +and don't even worry if you are not understood + +41 +00:01:21,000 --> 00:01:22,000 +the entire tree concept + +42 +00:01:22,000 --> 00:01:25,000 +is because we'll be having a separate section on it, + +43 +00:01:25,000 --> 00:01:26,000 +so here I just wanted to introduce tree, + +44 +00:01:26,000 --> 00:01:28,000 +so when you see a structure where you see branches + +45 +00:01:28,000 --> 00:01:32,000 +and different nodes, that's your tree structure + +46 +00:01:32,000 --> 00:01:34,000 +and then we are going to use binary tree somewhere, + +47 +00:01:34,000 --> 00:01:37,000 +where each node, so the part, + +48 +00:01:37,000 --> 00:01:40,000 +the branches end, that's what your node is + +49 +00:01:40,000 --> 00:01:41,000 +or you can say leaf, + +50 +00:01:41,000 --> 00:01:43,000 +but that will be having only two branches, + +51 +00:01:43,000 --> 00:01:45,000 +so that's the binary tree. + diff --git a/28 - DSA (Optional)/016 Recursion_en.srt b/28 - DSA (Optional)/016 Recursion_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..f8270a24c8a0d2fc42dfbd9715648041269aec72 --- /dev/null +++ b/28 - DSA (Optional)/016 Recursion_en.srt @@ -0,0 +1,1152 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: In this video, let's talk about recursion. + +2 +00:00:02,000 --> 00:00:04,000 +Now we talk about different programming language, + +3 +00:00:04,000 --> 00:00:06,000 +we use methods or functions, right? + +4 +00:00:06,000 --> 00:00:08,000 +Irrespective of the programming language. + +5 +00:00:08,000 --> 00:00:09,000 +We do have this concept. + +6 +00:00:09,000 --> 00:00:11,000 +Of course, in some languages we do call them methods, + +7 +00:00:11,000 --> 00:00:14,000 +but calling them functions is not a crime. + +8 +00:00:14,000 --> 00:00:15,000 +So let's talk about functions here. + +9 +00:00:15,000 --> 00:00:17,000 +Now, why do we write functions? + +10 +00:00:17,000 --> 00:00:19,000 +If you want to achieve a particular task, + +11 +00:00:19,000 --> 00:00:20,000 +you write a function. + +12 +00:00:20,000 --> 00:00:22,000 +Maybe you want to print something, + +13 +00:00:22,000 --> 00:00:24,000 +maybe you want to add two numbers. + +14 +00:00:24,000 --> 00:00:25,000 +Maybe you want to find a factorial. + +15 +00:00:25,000 --> 00:00:28,000 +Maybe you want to process the payment. + +16 +00:00:28,000 --> 00:00:30,000 +Maybe you want to search something on Google. + +17 +00:00:30,000 --> 00:00:33,000 +Everything happens with the help of functions. + +18 +00:00:33,000 --> 00:00:36,000 +Most of the time we call a function from a function. + +19 +00:00:36,000 --> 00:00:38,000 +Let's say we got two functions here. + +20 +00:00:38,000 --> 00:00:40,000 +Let's call them f1 and f2. + +21 +00:00:40,000 --> 00:00:42,000 +So f1 is calling f2. + +22 +00:00:42,000 --> 00:00:43,000 +Yes, we can call the functions. + +23 +00:00:43,000 --> 00:00:46,000 +Even f2 can call some other function, let's say f3. + +24 +00:00:46,000 --> 00:00:48,000 +f3 is calling some other function. + +25 +00:00:48,000 --> 00:00:51,000 +Now let's say you're calling f2 from f1. + +26 +00:00:51,000 --> 00:00:54,000 +Now what happens is f1 cannot complete its execution + +27 +00:00:54,000 --> 00:00:56,000 +before you complete the execution of f2. + +28 +00:00:56,000 --> 00:00:58,000 +Because if f1 is not calling anyone, + +29 +00:00:58,000 --> 00:01:01,000 +f1 will complete its work and then that's done. + +30 +00:01:01,000 --> 00:01:03,000 +But then what if f1 is calling f2? + +31 +00:01:03,000 --> 00:01:06,000 +In that case, f1 cannot be completed + +32 +00:01:06,000 --> 00:01:08,000 +till you complete f2. + +33 +00:01:08,000 --> 00:01:09,000 +So f2 will complete + +34 +00:01:09,000 --> 00:01:11,000 +and then you can complete f1. + +35 +00:01:11,000 --> 00:01:13,000 +But what if f2 is calling f3? + +36 +00:01:13,000 --> 00:01:16,000 +In that case, f2 has to wait for f3. + +37 +00:01:16,000 --> 00:01:18,000 +And mind you, f1 is still waiting for f2 to complete. + +38 +00:01:18,000 --> 00:01:20,000 +What if f3 is calling f4? + +39 +00:01:20,000 --> 00:01:21,000 +Now of course everything goes into stack. + +40 +00:01:21,000 --> 00:01:23,000 +So f1 is waiting for f2, + +41 +00:01:23,000 --> 00:01:24,000 +f2 is waiting for f3, + +42 +00:01:24,000 --> 00:01:25,000 +f3 is waiting for f4. + +43 +00:01:25,000 --> 00:01:27,000 +Once you complete f4, + +44 +00:01:27,000 --> 00:01:28,000 +the pointer goes back to f3, + +45 +00:01:28,000 --> 00:01:30,000 +f3 says "Okay, I can complete my work" now. + +46 +00:01:30,000 --> 00:01:33,000 +Once f3 is complete, you can complete the f2. + +47 +00:01:33,000 --> 00:01:36,000 +Once f2 is complete, you can complete the f1 + +48 +00:01:36,000 --> 00:01:38,000 +and if everything is done, + +49 +00:01:38,000 --> 00:01:39,000 +you can stop the execution. + +50 +00:01:39,000 --> 00:01:41,000 +But mind you, when f1 is calling f2, + +51 +00:01:41,000 --> 00:01:42,000 +f2 is calling f3, + +52 +00:01:42,000 --> 00:01:44,000 +f3 is calling f4, + +53 +00:01:44,000 --> 00:01:45,000 +there is a dependency. + +54 +00:01:45,000 --> 00:01:47,000 +So have to wait for f4 to complete + +55 +00:01:47,000 --> 00:01:49,000 +so that f3 can complete, so that f2 can complete + +56 +00:01:49,000 --> 00:01:50,000 +and then f1 can complete. + +57 +00:01:50,000 --> 00:01:52,000 +Now here we are calling some other functions, right? + +58 +00:01:52,000 --> 00:01:55,000 +What if a function is calling itself? + +59 +00:01:55,000 --> 00:01:57,000 +Example, let's say we have function f1 + +60 +00:01:57,000 --> 00:01:59,000 +and f1 is not calling f2. + +61 +00:01:59,000 --> 00:02:01,000 +What is f1 is calling itself? + +62 +00:02:01,000 --> 00:02:02,000 +F1 is calling f1 now. + +63 +00:02:02,000 --> 00:02:05,000 +Now in that case, f1 is calling f1 + +64 +00:02:05,000 --> 00:02:06,000 +and it can go multiple times. + +65 +00:02:06,000 --> 00:02:08,000 +So we don't have a different name, + +66 +00:02:08,000 --> 00:02:09,000 +but you can imagine a stack, + +67 +00:02:09,000 --> 00:02:10,000 +so f1 is calling f1, + +68 +00:02:10,000 --> 00:02:11,000 +f1 is calling f1, + +69 +00:02:11,000 --> 00:02:12,000 +f1 is calling f1, + +70 +00:02:12,000 --> 00:02:13,000 +f1 is calling f1. + +71 +00:02:13,000 --> 00:02:15,000 +The last f1 need to complete first + +72 +00:02:15,000 --> 00:02:17,000 +for the second last f1 to complete, + +73 +00:02:17,000 --> 00:02:19,000 +the second last f1 need to complete, + +74 +00:02:19,000 --> 00:02:21,000 +then only we can complete the third last f1. + +75 +00:02:21,000 --> 00:02:24,000 +In fact, if you want to complete the f1, the first f1, + +76 +00:02:24,000 --> 00:02:26,000 +you have to make sure that you complete the entire stack. + +77 +00:02:26,000 --> 00:02:29,000 +So when you say a function is calling itself, + +78 +00:02:29,000 --> 00:02:31,000 +that is called recursion. + +79 +00:02:31,000 --> 00:02:32,000 +But why you will do that? + +80 +00:02:32,000 --> 00:02:34,000 +Because if you don't apply a condition, + +81 +00:02:34,000 --> 00:02:35,000 +it is never ending process. + +82 +00:02:35,000 --> 00:02:37,000 +It's like is calling itself + +83 +00:02:37,000 --> 00:02:39,000 +and then ultimately when your stack is full, + +84 +00:02:39,000 --> 00:02:42,000 +your computer will give you a error. + +85 +00:02:42,000 --> 00:02:44,000 +Most of the time they're called stack overflow error. + +86 +00:02:44,000 --> 00:02:45,000 +And that's why you call + +87 +00:02:45,000 --> 00:02:46,000 +this famous website, Stack overflow. + +88 +00:02:46,000 --> 00:02:48,000 +So if you have to apply some condition as well. + +89 +00:02:48,000 --> 00:02:50,000 +Now where it'll be useful, + +90 +00:02:50,000 --> 00:02:51,000 +so let me show you that with an example. + +91 +00:02:51,000 --> 00:02:54,000 +So let's say this is a very simple Java code + +92 +00:02:54,000 --> 00:02:57,000 +where you got a main function, we got demo. + +93 +00:02:57,000 --> 00:02:59,000 +And from this main, let me call a function, + +94 +00:02:59,000 --> 00:03:00,000 +but before you call a function, + +95 +00:03:00,000 --> 00:03:02,000 +you have to first define a function. + +96 +00:03:02,000 --> 00:03:03,000 +So I'll say public static. + +97 +00:03:03,000 --> 00:03:04,000 +I don't want to get function, + +98 +00:03:04,000 --> 00:03:05,000 +so I will go for static. + +99 +00:03:05,000 --> 00:03:08,000 +And this function is basically calling, let's say f1. + +100 +00:03:08,000 --> 00:03:09,000 +Let's call this function f1 + +101 +00:03:09,000 --> 00:03:10,000 +because we are going with that now. + +102 +00:03:10,000 --> 00:03:13,000 +And then here I want to print something. + +103 +00:03:13,000 --> 00:03:14,000 +So I will what I will say, + +104 +00:03:14,000 --> 00:03:16,000 +I will print the value of I, + +105 +00:03:16,000 --> 00:03:17,000 +but what is I? + +106 +00:03:17,000 --> 00:03:20,000 +So what we can do is we can create a variable I here + +107 +00:03:20,000 --> 00:03:22,000 +and initially let's say the value of I is zero, + +108 +00:03:22,000 --> 00:03:24,000 +and then we are printing I here. + +109 +00:03:24,000 --> 00:03:25,000 +But of course you have to call this function. + +110 +00:03:25,000 --> 00:03:27,000 +So let me call f1 here. + +111 +00:03:27,000 --> 00:03:29,000 +Okay, simple task, we're just calling a function f1. + +112 +00:03:29,000 --> 00:03:30,000 +Nothing fancy. + +113 +00:03:30,000 --> 00:03:32,000 +In f1 we are saying int i=0 + +114 +00:03:32,000 --> 00:03:34,000 +and then we are printing it. + +115 +00:03:34,000 --> 00:03:35,000 +Let's run this code to see what happens. + +116 +00:03:35,000 --> 00:03:37,000 +When you're run this code, you will get zero. + +117 +00:03:37,000 --> 00:03:39,000 +Of course that is what we are expecting. + +118 +00:03:39,000 --> 00:03:41,000 +But what happens after printing I? + +119 +00:03:41,000 --> 00:03:45,000 +Let me call f1 once again. + +120 +00:03:45,000 --> 00:03:47,000 +Now what do you think what will happen? + +121 +00:03:47,000 --> 00:03:48,000 +I'm not sure if my system will crash, + +122 +00:03:48,000 --> 00:03:49,000 +but let's try. + +123 +00:03:49,000 --> 00:03:50,000 +The moment I run this code, + +124 +00:03:50,000 --> 00:03:51,000 +okay, we got an error. + +125 +00:03:51,000 --> 00:03:55,000 +So if you can go up and up and up, okay, where? + +126 +00:03:55,000 --> 00:03:58,000 +Where, okay, so we got somewhere here. + +127 +00:03:58,000 --> 00:04:00,000 +So I got a huge stack here. + +128 +00:04:00,000 --> 00:04:01,000 +So you can see we got another + +129 +00:04:01,000 --> 00:04:02,000 +which says Stack overflow error. + +130 +00:04:02,000 --> 00:04:03,000 +So basically there's a stack + +131 +00:04:03,000 --> 00:04:06,000 +and it has a limited memory. + +132 +00:04:06,000 --> 00:04:07,000 +And that's why you can see + +133 +00:04:07,000 --> 00:04:08,000 +we got all this printing of zero + +134 +00:04:08,000 --> 00:04:10,000 +because that's what they're doing. + +135 +00:04:10,000 --> 00:04:12,000 +And then once they complete their work, they're coming back. + +136 +00:04:12,000 --> 00:04:14,000 +So that's what is happening here. + +137 +00:04:14,000 --> 00:04:16,000 +But then there should be a condition as well. + +138 +00:04:16,000 --> 00:04:19,000 +So what I will do is I will simply pass this I here + +139 +00:04:19,000 --> 00:04:22,000 +and let's say the value of I is one + +140 +00:04:22,000 --> 00:04:23,000 +and then I'm also passing I here. + +141 +00:04:23,000 --> 00:04:25,000 +So you have to accept I. + +142 +00:04:25,000 --> 00:04:29,000 +And what if you don't declare the variable inside? + +143 +00:04:29,000 --> 00:04:30,000 +Okay, let's not declare that inside. + +144 +00:04:30,000 --> 00:04:33,000 +Let's say I'm passing the the value from here + +145 +00:04:33,000 --> 00:04:35,000 +or maybe I can say 10 even, that makes sense. + +146 +00:04:35,000 --> 00:04:38,000 +So let's say we are passing 10 which goes here + +147 +00:04:38,000 --> 00:04:39,000 +and then if I run this code now + +148 +00:04:39,000 --> 00:04:41,000 +it will not be printing zero, + +149 +00:04:41,000 --> 00:04:43,000 +because we're passing a value, it is printing 10. + +150 +00:04:43,000 --> 00:04:45,000 +But of course you have to apply some condition as well. + +151 +00:04:45,000 --> 00:04:49,000 +So what I can do is before calling I, + +152 +00:04:49,000 --> 00:04:51,000 +what I want to do is I want to check + +153 +00:04:51,000 --> 00:04:54,000 +if the value of I is greater than zero, + +154 +00:04:54,000 --> 00:04:56,000 +then only I will call f1. + +155 +00:04:56,000 --> 00:04:59,000 +So that means the value of I should decrease somewhere, + +156 +00:04:59,000 --> 00:05:00,000 +but we are not decreasing it. + +157 +00:05:00,000 --> 00:05:01,000 +But what if you do this, + +158 +00:05:01,000 --> 00:05:04,000 +instead of passing I when you're calling for the next time, + +159 +00:05:04,000 --> 00:05:07,000 +let's pass I minus one? + +160 +00:05:07,000 --> 00:05:09,000 +Now by doing this, what we'll be doing is + +161 +00:05:09,000 --> 00:05:10,000 +we got all these values. + +162 +00:05:10,000 --> 00:05:13,000 +Now if you're thinking what we have done + +163 +00:05:13,000 --> 00:05:14,000 +is basically when you're calling f1 + +164 +00:05:14,000 --> 00:05:16,000 +for the first time, you are passing 10. + +165 +00:05:16,000 --> 00:05:19,000 +The f1, the first F which got called here, + +166 +00:05:19,000 --> 00:05:20,000 +it is calling f1 itself + +167 +00:05:20,000 --> 00:05:22,000 +but not with the same value 10, + +168 +00:05:22,000 --> 00:05:24,000 +it is passing with the value nine. + +169 +00:05:24,000 --> 00:05:26,000 +So next time it is executed, + +170 +00:05:26,000 --> 00:05:28,000 +it is passing the value, which is eight, then seven, + +171 +00:05:28,000 --> 00:05:30,000 +then six, till where we are going? + +172 +00:05:30,000 --> 00:05:32,000 +The moment you say I is not greater than zero, + +173 +00:05:32,000 --> 00:05:33,000 +then you stop. + +174 +00:05:33,000 --> 00:05:34,000 +So basically there will be a point + +175 +00:05:34,000 --> 00:05:37,000 +where it goes into minus or negative values. + +176 +00:05:37,000 --> 00:05:38,000 +Now that's why you have to stop + +177 +00:05:38,000 --> 00:05:40,000 +and that's why you're stopping at zero. + +178 +00:05:40,000 --> 00:05:41,000 +If you change this condition, + +179 +00:05:41,000 --> 00:05:43,000 +it will change, of course. + +180 +00:05:43,000 --> 00:05:45,000 +Example, let's say if I say it is one, + +181 +00:05:45,000 --> 00:05:47,000 +now in this case it'll stop at one. + +182 +00:05:47,000 --> 00:05:49,000 +This is what is happening. + +183 +00:05:49,000 --> 00:05:51,000 +You can do the same thing with a for loop. + +184 +00:05:51,000 --> 00:05:53,000 +So you can start the for loop by 10, + +185 +00:05:53,000 --> 00:05:54,000 +with the same condition, + +186 +00:05:54,000 --> 00:05:57,000 +and then you can say I--, even that works. + +187 +00:05:57,000 --> 00:05:58,000 +So instead of using a loop, + +188 +00:05:58,000 --> 00:06:00,000 +we are using recursion here. + +189 +00:06:00,000 --> 00:06:02,000 +That means there are certain things + +190 +00:06:02,000 --> 00:06:03,000 +you can do with for loop, + +191 +00:06:03,000 --> 00:06:04,000 +which which you can also do with recursion, + +192 +00:06:04,000 --> 00:06:06,000 +example, finding a factorial. + +193 +00:06:06,000 --> 00:06:07,000 +Normally what you do is if you want + +194 +00:06:07,000 --> 00:06:09,000 +to find a factorial with recursion, + +195 +00:06:09,000 --> 00:06:10,000 +let's, I want to change this, + +196 +00:06:10,000 --> 00:06:13,000 +I want to find the fact of five, + +197 +00:06:13,000 --> 00:06:14,000 +which is 120 if I'm not wrong, + +198 +00:06:14,000 --> 00:06:16,000 +or (indistinct) it doesn't matter. + +199 +00:06:16,000 --> 00:06:19,000 +So let's say I'm finding finding the factorial of five. + +200 +00:06:19,000 --> 00:06:20,000 +So what will be the logic of factorial? + +201 +00:06:20,000 --> 00:06:23,000 +Basically, let's say it gives you a result, + +202 +00:06:23,000 --> 00:06:24,000 +we'll store it in result, + +203 +00:06:24,000 --> 00:06:27,000 +and then I will simply print the result. + +204 +00:06:27,000 --> 00:06:28,000 +So let me print result. + +205 +00:06:28,000 --> 00:06:30,000 +Now since we are expecting a value, + +206 +00:06:30,000 --> 00:06:33,000 +it should also return something, so it will return int. + +207 +00:06:33,000 --> 00:06:36,000 +But what will be the actual logic here? + +208 +00:06:36,000 --> 00:06:38,000 +Now what if I'm finding a factorial of zero? + +209 +00:06:38,000 --> 00:06:39,000 +So for zero and one, + +210 +00:06:39,000 --> 00:06:43,000 +the factorial value is one. + +211 +00:06:43,000 --> 00:06:44,000 +So factorial of zero is one. + +212 +00:06:44,000 --> 00:06:48,000 +So I can simply turn here one. + +213 +00:06:48,000 --> 00:06:49,000 +At least we can get this code running. + +214 +00:06:49,000 --> 00:06:50,000 +Let's run this code. + +215 +00:06:50,000 --> 00:06:52,000 +Oh, we got an errors because + +216 +00:06:52,000 --> 00:06:54,000 +we have not changed the function name. + +217 +00:06:54,000 --> 00:06:56,000 +It should be fact, + +218 +00:06:56,000 --> 00:06:58,000 +let's run this and you can see we got one. + +219 +00:06:58,000 --> 00:06:59,000 +So this is working. + +220 +00:06:59,000 --> 00:07:01,000 +Lemme minimize this. + +221 +00:07:01,000 --> 00:07:06,000 +But what if I want to find the factorial of one, + +222 +00:07:06,000 --> 00:07:08,000 +even for one, it will return one, + +223 +00:07:08,000 --> 00:07:09,000 +that actually makes sense. + +224 +00:07:09,000 --> 00:07:11,000 +But what if you say this is two? + +225 +00:07:11,000 --> 00:07:13,000 +Now in this case it should print two, + +226 +00:07:13,000 --> 00:07:15,000 +which is two into one. + +227 +00:07:15,000 --> 00:07:16,000 +We all know what a factorial is. + +228 +00:07:16,000 --> 00:07:20,000 +So example, if I say factorial of five + +229 +00:07:20,000 --> 00:07:24,000 +or five factorial, is actually equal to five, into four, + +230 +00:07:24,000 --> 00:07:28,000 +into three, into two into one. + +231 +00:07:28,000 --> 00:07:31,000 +This is actually the logic of factorial. + +232 +00:07:31,000 --> 00:07:33,000 +So it should give you five into first 20, + +233 +00:07:33,000 --> 00:07:34,000 +20 into three is 60, + +234 +00:07:34,000 --> 00:07:36,000 +16 to two is 120. + +235 +00:07:36,000 --> 00:07:38,000 +So the output I'm expecting is 120. + +236 +00:07:38,000 --> 00:07:39,000 +How will I do this? + +237 +00:07:39,000 --> 00:07:40,000 +So don't you think this is matching + +238 +00:07:40,000 --> 00:07:42,000 +with the previous example which we have done? + +239 +00:07:42,000 --> 00:07:44,000 +We are just lowering the number. + +240 +00:07:44,000 --> 00:07:47,000 +So first if you can find the factorial of, + +241 +00:07:47,000 --> 00:07:48,000 +can we do this? + +242 +00:07:48,000 --> 00:07:51,000 +When somebody is asking for factorial of five, + +243 +00:07:51,000 --> 00:07:54,000 +can I say five into four factorial? + +244 +00:07:54,000 --> 00:07:57,000 +Because if I can get four factorial, + +245 +00:07:57,000 --> 00:07:58,000 +and you know what is four factorial? + +246 +00:07:58,000 --> 00:08:00,000 +Four factorial is four into three into two into one. + +247 +00:08:00,000 --> 00:08:03,000 +So if I can get this, + +248 +00:08:03,000 --> 00:08:04,000 +I can simply multiply it for five + +249 +00:08:04,000 --> 00:08:05,000 +and our job is done. + +250 +00:08:05,000 --> 00:08:09,000 +So here what we can do is we can call the same function + +251 +00:08:09,000 --> 00:08:13,000 +by passing the value one less than the actual value. + +252 +00:08:13,000 --> 00:08:16,000 +So what I'm saying is if I got the factorial of five, + +253 +00:08:16,000 --> 00:08:21,000 +I can simply return I into fact of I minus one. + +254 +00:08:21,000 --> 00:08:22,000 +Don't you think that's what we are doing here? + +255 +00:08:22,000 --> 00:08:24,000 +So when I say I want five factorial, + +256 +00:08:24,000 --> 00:08:25,000 +we are saying five, + +257 +00:08:25,000 --> 00:08:28,000 +which is I in this case into four factorial, + +258 +00:08:28,000 --> 00:08:30,000 +which is fact of I minus one. + +259 +00:08:30,000 --> 00:08:32,000 +But the question is it'll call itself + +260 +00:08:32,000 --> 00:08:33,000 +but tell how many time it'll call. + +261 +00:08:33,000 --> 00:08:35,000 +And that's why we have to put a condition. + +262 +00:08:35,000 --> 00:08:38,000 +So we have to reach till I is equal to zero. + +263 +00:08:38,000 --> 00:08:42,000 +So if I is not equal to zero, + +264 +00:08:42,000 --> 00:08:44,000 +you do this. + +265 +00:08:44,000 --> 00:08:46,000 +Otherwise I would return one, yeah? + +266 +00:08:46,000 --> 00:08:48,000 +So by doing this, you are returning the factorial. + +267 +00:08:48,000 --> 00:08:50,000 +Let's try if this works, + +268 +00:08:50,000 --> 00:08:51,000 +and you can see we got two + +269 +00:08:51,000 --> 00:08:53,000 +because the value is two, + +270 +00:08:53,000 --> 00:08:56,000 +let's say I'm finding a factorial of five, run, + +271 +00:08:56,000 --> 00:08:58,000 +and you can see we got 120. + +272 +00:08:58,000 --> 00:09:01,000 +When you say six, we got 720. + +273 +00:09:01,000 --> 00:09:03,000 +And if you can go for 10, + +274 +00:09:03,000 --> 00:09:04,000 +there's a limit for integer as well. + +275 +00:09:04,000 --> 00:09:05,000 +We got some value. + +276 +00:09:05,000 --> 00:09:07,000 +But what if you go for a bigger value, + +277 +00:09:07,000 --> 00:09:08,000 +this is where it will fail, I think. + +278 +00:09:08,000 --> 00:09:09,000 +You can say we got negative value + +279 +00:09:09,000 --> 00:09:11,000 +because integer has its own range, + +280 +00:09:11,000 --> 00:09:12,000 +but you got the idea. + +281 +00:09:12,000 --> 00:09:13,000 +What is recursion? + +282 +00:09:13,000 --> 00:09:16,000 +So the the idea is not about factorial here. + +283 +00:09:16,000 --> 00:09:18,000 +The idea is about recursion. + +284 +00:09:18,000 --> 00:09:19,000 +So when a function is calling itself, + +285 +00:09:19,000 --> 00:09:20,000 +that is repercussion. + +286 +00:09:20,000 --> 00:09:21,000 +So yeah, that's it from this video + +287 +00:09:21,000 --> 00:09:23,000 +where we talked about recursion + +288 +00:09:23,000 --> 00:09:24,000 +and see you in the next video. + diff --git a/28 - DSA (Optional)/017 Merge Sort Theory_en.srt b/28 - DSA (Optional)/017 Merge Sort Theory_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..dab298ec02b160a0ecba7ccc59d62693fb2da594 --- /dev/null +++ b/28 - DSA (Optional)/017 Merge Sort Theory_en.srt @@ -0,0 +1,1680 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: In this video, we'll talk about merge sort. + +2 +00:00:02,000 --> 00:00:04,000 +Now basically let's talk about the theory first + +3 +00:00:04,000 --> 00:00:07,000 +and then we'll focus on the implementation. + +4 +00:00:07,000 --> 00:00:08,000 +Now when we talk about sorting techniques, + +5 +00:00:08,000 --> 00:00:10,000 +we have worked with multiple sorting technique. + +6 +00:00:10,000 --> 00:00:12,000 +And when we talked about the quick sort, + +7 +00:00:12,000 --> 00:00:16,000 +which was going for the timed complexity of (n log (n)), + +8 +00:00:16,000 --> 00:00:18,000 +now we have one more there, which is called a merge sort, + +9 +00:00:18,000 --> 00:00:21,000 +which also has a time complexity of (n log (n)). + +10 +00:00:22,000 --> 00:00:24,000 +Now, both quick sort and merge sort + +11 +00:00:24,000 --> 00:00:28,000 +follows a technique called divide and conquer. + +12 +00:00:28,000 --> 00:00:31,000 +So basically what you do is you break down the problem + +13 +00:00:31,000 --> 00:00:33,000 +into subproblems and apply the algorithm + +14 +00:00:33,000 --> 00:00:35,000 +on those particular subproblems. + +15 +00:00:35,000 --> 00:00:38,000 +And once you get the solution for each subproblem, + +16 +00:00:38,000 --> 00:00:40,000 +then you combine the solutions or sub solutions + +17 +00:00:40,000 --> 00:00:44,000 +to get a final solution. + +18 +00:00:44,000 --> 00:00:45,000 +That's what we are going to do here, + +19 +00:00:45,000 --> 00:00:46,000 +which is divide and conquer. + +20 +00:00:46,000 --> 00:00:49,000 +Okay, so how we are going to do this? + +21 +00:00:49,000 --> 00:00:51,000 +Now basically, it is, actually easier to understand. + +22 +00:00:51,000 --> 00:00:54,000 +So if you can be with me, it'll make much more sense. + +23 +00:00:54,000 --> 00:00:56,000 +So what we're going to do is we'll take a array initially, + +24 +00:00:56,000 --> 00:00:57,000 +okay? + +25 +00:00:57,000 --> 00:01:00,000 +So let's say I'm taking a list of values here. + +26 +00:01:00,000 --> 00:01:02,000 +So let's say we are going for six values here + +27 +00:01:02,000 --> 00:01:04,000 +and what are these six values? + +28 +00:01:04,000 --> 00:01:05,000 +So let's say I have a value which is + +29 +00:01:05,000 --> 00:01:10,000 +eight, five, nine, one, six, seven, let's say. + +30 +00:01:11,000 --> 00:01:13,000 +So we got this values here and then I want to sort them. + +31 +00:01:13,000 --> 00:01:17,000 +So what you do is you, you will apply a merge sort. + +32 +00:01:17,000 --> 00:01:19,000 +Now basically here, as I mentioned before, + +33 +00:01:19,000 --> 00:01:21,000 +you will do two things, you will, in fact three things. + +34 +00:01:21,000 --> 00:01:23,000 +First, you will divide this problem into subproblems + +35 +00:01:23,000 --> 00:01:24,000 +and then you will conquer them + +36 +00:01:24,000 --> 00:01:27,000 +and then you will simply merge them. + +37 +00:01:27,000 --> 00:01:29,000 +Now since it is called a merge sort, + +38 +00:01:29,000 --> 00:01:32,000 +the important step here is merging. + +39 +00:01:32,000 --> 00:01:36,000 +So when I say important, I'm also saying it is complex part. + +40 +00:01:36,000 --> 00:01:39,000 +The easier part is to break down this into small parts. + +41 +00:01:39,000 --> 00:01:42,000 +Merging will be time consuming, so let's understand this. + +42 +00:01:42,000 --> 00:01:45,000 +So when you have this values here, what you will do is, + +43 +00:01:45,000 --> 00:01:47,000 +first of all, you will break down it into two parts. + +44 +00:01:47,000 --> 00:01:50,000 +Now the question is how will you break it down? + +45 +00:01:50,000 --> 00:01:51,000 +In different sorting algorithm, + +46 +00:01:51,000 --> 00:01:54,000 +we have a different way of breaking the entire list. + +47 +00:01:54,000 --> 00:01:57,000 +In fact, in quick sort we use a pivot. + +48 +00:01:57,000 --> 00:01:58,000 +But here, let's make it very simple. + +49 +00:01:58,000 --> 00:02:00,000 +We use something called a median. + +50 +00:02:00,000 --> 00:02:03,000 +Now what is a median or maybe a midpoint? + +51 +00:02:03,000 --> 00:02:05,000 +So here what we can do is, as we know that in, + +52 +00:02:05,000 --> 00:02:07,000 +if you talk about this particular array here, + +53 +00:02:07,000 --> 00:02:09,000 +the mid is here, right? + +54 +00:02:09,000 --> 00:02:11,000 +We can see and we can tell that, + +55 +00:02:11,000 --> 00:02:14,000 +but how an algorithm will know that this is a mid? + +56 +00:02:14,000 --> 00:02:17,000 +And that's why we will do some calculation. + +57 +00:02:17,000 --> 00:02:19,000 +Now what kind of calculation, it's very easy. + +58 +00:02:19,000 --> 00:02:20,000 +So what we can do is let's say + +59 +00:02:20,000 --> 00:02:22,000 +this is your left and this is your right. + +60 +00:02:22,000 --> 00:02:25,000 +So what we will do is we'll simply use index value. + +61 +00:02:25,000 --> 00:02:28,000 +So this, the index values for this is + +62 +00:02:28,000 --> 00:02:32,000 +zero, one, two, three, four, five, + +63 +00:02:32,000 --> 00:02:34,000 +so that depends upon the values you have. + +64 +00:02:34,000 --> 00:02:36,000 +And then, if you want to find a median, + +65 +00:02:36,000 --> 00:02:39,000 +so, basically, L represents the first index value, + +66 +00:02:39,000 --> 00:02:41,000 +and R represents the last index value. + +67 +00:02:41,000 --> 00:02:43,000 +So you have to find a median + +68 +00:02:43,000 --> 00:02:45,000 +and the formula for median is very simple. + +69 +00:02:45,000 --> 00:02:46,000 +So what it you will simply do + +70 +00:02:46,000 --> 00:02:50,000 +is you will say median is equal to left plus right + +71 +00:02:50,000 --> 00:02:51,000 +divide by two. + +72 +00:02:51,000 --> 00:02:52,000 +That's how you get the median right? + +73 +00:02:52,000 --> 00:02:55,000 +Now in this case, the median is what is l? + +74 +00:02:55,000 --> 00:02:59,000 +l is zero, r is five, and this divided by two + +75 +00:02:59,000 --> 00:03:01,000 +is equal to five by two. + +76 +00:03:01,000 --> 00:03:03,000 +Now since in different languages we get different outputs, + +77 +00:03:03,000 --> 00:03:05,000 +so let's say this is two in this case, + +78 +00:03:05,000 --> 00:03:08,000 +because in most of the languages we go for the floor value. + +79 +00:03:08,000 --> 00:03:10,000 +So we got two here. + +80 +00:03:10,000 --> 00:03:12,000 +Now two is your median, so this is your median. + +81 +00:03:12,000 --> 00:03:14,000 +So whatever is there on this side + +82 +00:03:14,000 --> 00:03:17,000 +will go into a different array, + +83 +00:03:17,000 --> 00:03:18,000 +and what was there in this side, + +84 +00:03:18,000 --> 00:03:19,000 +will go into different array. + +85 +00:03:19,000 --> 00:03:21,000 +So when I say array, I'm talking about the list. + +86 +00:03:21,000 --> 00:03:24,000 +So what we can do is we can divide into two parts + +87 +00:03:24,000 --> 00:03:27,000 +and then we can create a different section. + +88 +00:03:27,000 --> 00:03:29,000 +So we got a section here which has three values, + +89 +00:03:29,000 --> 00:03:33,000 +we got a section here which get three values, + +90 +00:03:33,000 --> 00:03:37,000 +and then the values will be eight, five, nine here, + +91 +00:03:37,000 --> 00:03:40,000 +and one, six, seven here, okay? + +92 +00:03:40,000 --> 00:03:42,000 +And again, the job is not done. + +93 +00:03:42,000 --> 00:03:44,000 +Again, you have to divide into two parts. + +94 +00:03:44,000 --> 00:03:46,000 +Now how we are gonna do that? It's very simple. + +95 +00:03:46,000 --> 00:03:47,000 +The same steps which you have followed, + +96 +00:03:47,000 --> 00:03:52,000 +again, you will take l and r here, l and r here. + +97 +00:03:52,000 --> 00:03:54,000 +Let's find the median of the first point here + +98 +00:03:54,000 --> 00:03:55,000 +or the first array. + +99 +00:03:55,000 --> 00:03:56,000 +Now how do you get the median? + +100 +00:03:56,000 --> 00:04:01,000 +Again, you will say m is equal to l plus r divide by two. + +101 +00:04:01,000 --> 00:04:04,000 +So now we can do it quickly. So what is L in this case? + +102 +00:04:04,000 --> 00:04:06,000 +So if you go with index value, + +103 +00:04:06,000 --> 00:04:09,000 +index value is zero, one, two in this case, + +104 +00:04:09,000 --> 00:04:11,000 +and three, four, five. + +105 +00:04:11,000 --> 00:04:13,000 +Now if we talk about the first array, + +106 +00:04:13,000 --> 00:04:15,000 +the l is zero, r is two. + +107 +00:04:15,000 --> 00:04:17,000 +So you will say zero plus two is two + +108 +00:04:17,000 --> 00:04:18,000 +and two divided by two is one. + +109 +00:04:18,000 --> 00:04:21,000 +So m for this is one, so m is here. + +110 +00:04:21,000 --> 00:04:22,000 +Now if you want to calculate for the next array + +111 +00:04:22,000 --> 00:04:25,000 +or the next values, so again, the same value. + +112 +00:04:25,000 --> 00:04:28,000 +So what is l? l is three, r is five, + +113 +00:04:28,000 --> 00:04:32,000 +so three plus five is eight, eight divided by two is four. + +114 +00:04:32,000 --> 00:04:33,000 +So four is here, so median is here. + +115 +00:04:33,000 --> 00:04:35,000 +Of course when you look at the values, + +116 +00:04:35,000 --> 00:04:37,000 +you'll find the median just by looking at it. + +117 +00:04:37,000 --> 00:04:39,000 +But let's talk about the algorithm + +118 +00:04:39,000 --> 00:04:41,000 +and we don't have a limited set of values. + +119 +00:04:41,000 --> 00:04:43,000 +What if you have 500 values? + +120 +00:04:43,000 --> 00:04:44,000 +You will not simply search for the median, right? + +121 +00:04:44,000 --> 00:04:46,000 +So that's where we can get the median. + +122 +00:04:46,000 --> 00:04:47,000 +Again, you will divide into two parts. + +123 +00:04:47,000 --> 00:04:49,000 +So we can divide something like this. + +124 +00:04:49,000 --> 00:04:51,000 +You will get two sections here for this + +125 +00:04:51,000 --> 00:04:53,000 +and two sections for this. + +126 +00:04:53,000 --> 00:04:56,000 +We can say the first section will have two values, + +127 +00:04:56,000 --> 00:04:58,000 +which is eight and five. + +128 +00:04:58,000 --> 00:05:00,000 +The second section will have a value, which is nine. + +129 +00:05:00,000 --> 00:05:01,000 +Here the first section, + +130 +00:05:01,000 --> 00:05:03,000 +since we only have one value here, nine, + +131 +00:05:03,000 --> 00:05:04,000 +we got nine, + +132 +00:05:04,000 --> 00:05:08,000 +but let's say here we got one and six and here we got seven. + +133 +00:05:08,000 --> 00:05:10,000 +So basically that's how you divide. + +134 +00:05:10,000 --> 00:05:12,000 +Again, the job is not done so you have to go till the end. + +135 +00:05:12,000 --> 00:05:13,000 +So what we're gonna do is, + +136 +00:05:13,000 --> 00:05:15,000 +again, you will divide the first one. + +137 +00:05:15,000 --> 00:05:16,000 +So can we divide the second one, the nine? + +138 +00:05:16,000 --> 00:05:18,000 +We don't need to divide this part, right? + +139 +00:05:18,000 --> 00:05:22,000 +Because when you get one value you cannot divide that. + +140 +00:05:22,000 --> 00:05:23,000 +So let's divide the first part here. + +141 +00:05:23,000 --> 00:05:26,000 +So if you divide this, you will get, again, two values, + +142 +00:05:26,000 --> 00:05:29,000 +which is eight and five. + +143 +00:05:29,000 --> 00:05:32,000 +And again, you will divide this, you will get two values, + +144 +00:05:32,000 --> 00:05:34,000 +which is one and six. + +145 +00:05:34,000 --> 00:05:35,000 +So that's how you can divide + +146 +00:05:35,000 --> 00:05:38,000 +and at the end you will get the individual values. + +147 +00:05:38,000 --> 00:05:39,000 +Now still we can persist the index value + +148 +00:05:39,000 --> 00:05:41,000 +because we are not physically dividing them, + +149 +00:05:41,000 --> 00:05:43,000 +we are just virtually dividing it, right? + +150 +00:05:43,000 --> 00:05:45,000 +So index value for this + +151 +00:05:45,000 --> 00:05:50,000 +is zero, one, two, three, four, five, + +152 +00:05:50,000 --> 00:05:51,000 +the index number remain same. + +153 +00:05:51,000 --> 00:05:54,000 +And if you compare with the actual list we got, + +154 +00:05:54,000 --> 00:05:56,000 +the index value and the values are same, right? + +155 +00:05:56,000 --> 00:05:59,000 +So to this point we have not done any sorting part, + +156 +00:05:59,000 --> 00:06:01,000 +we just did the divide part. + +157 +00:06:01,000 --> 00:06:03,000 +And now, it's time for the conquer. + +158 +00:06:03,000 --> 00:06:05,000 +Now, when you say conquer, it simply means + +159 +00:06:05,000 --> 00:06:07,000 +that you need to sort this value. + +160 +00:06:07,000 --> 00:06:10,000 +So whatever value you got at the end, sort them. + +161 +00:06:10,000 --> 00:06:13,000 +But if you observe, they are always sorted + +162 +00:06:13,000 --> 00:06:16,000 +because each element, if you compare, if you say eight, + +163 +00:06:16,000 --> 00:06:17,000 +this list is sorted. + +164 +00:06:17,000 --> 00:06:21,000 +When you have only one value, it is by default sorted, okay? + +165 +00:06:21,000 --> 00:06:24,000 +So sorting is done, so conquering is done basically. + +166 +00:06:24,000 --> 00:06:26,000 +The next step is actually is to merge them + +167 +00:06:26,000 --> 00:06:28,000 +and the actual fun starts here. + +168 +00:06:28,000 --> 00:06:30,000 +Now when you say merge, what it means? + +169 +00:06:30,000 --> 00:06:31,000 +So basically what you do is + +170 +00:06:31,000 --> 00:06:35,000 +you start with the first two values and then you merge them. + +171 +00:06:35,000 --> 00:06:36,000 +Now merging is important. + +172 +00:06:36,000 --> 00:06:40,000 +So when you say we are merging eight and five, + +173 +00:06:40,000 --> 00:06:41,000 +of course, you will get a list + +174 +00:06:41,000 --> 00:06:42,000 +and you can again say eight and five, + +175 +00:06:42,000 --> 00:06:45,000 +but no, this is not how you'll merge it. + +176 +00:06:45,000 --> 00:06:48,000 +So the way you will merge it is this. + +177 +00:06:48,000 --> 00:06:49,000 +So you will take these two values + +178 +00:06:49,000 --> 00:06:53,000 +and merge them into another block, you will get two values. + +179 +00:06:53,000 --> 00:06:54,000 +But when you are merging it, + +180 +00:06:54,000 --> 00:06:56,000 +you will compare these two values, so which is less here? + +181 +00:06:56,000 --> 00:06:58,000 +So if five is less, + +182 +00:06:58,000 --> 00:07:00,000 +you will put five here and then you'll put eight here + +183 +00:07:00,000 --> 00:07:02,000 +because that's the only remaining element. + +184 +00:07:02,000 --> 00:07:07,000 +Then you think about merging, so you will merge this two. + +185 +00:07:07,000 --> 00:07:08,000 +Now, when you're merging this two, + +186 +00:07:08,000 --> 00:07:09,000 +of course, you will get three values, right? + +187 +00:07:09,000 --> 00:07:12,000 +So you will take it, take three values here, + +188 +00:07:12,000 --> 00:07:13,000 +and this is coming from here. + +189 +00:07:13,000 --> 00:07:16,000 +So these two values and this one value. + +190 +00:07:16,000 --> 00:07:17,000 +Now how will you merge? + +191 +00:07:17,000 --> 00:07:19,000 +Now since we have two different lists + +192 +00:07:19,000 --> 00:07:21,000 +or two different values here + +193 +00:07:21,000 --> 00:07:23,000 +or two different arrays we can say. + +194 +00:07:23,000 --> 00:07:24,000 +When you want to merge, + +195 +00:07:24,000 --> 00:07:26,000 +that's where the actual algorithm starts. + +196 +00:07:26,000 --> 00:07:28,000 +Because when you are merging two values, + +197 +00:07:28,000 --> 00:07:30,000 +you can simply compare smaller or get greater. + +198 +00:07:30,000 --> 00:07:32,000 +But now, we have three values, + +199 +00:07:32,000 --> 00:07:35,000 +in first we have two, in second we have only one. + +200 +00:07:35,000 --> 00:07:39,000 +So what you do here is you basically compare these values + +201 +00:07:39,000 --> 00:07:40,000 +before adding them. + +202 +00:07:40,000 --> 00:07:43,000 +So what should be the first value? + +203 +00:07:43,000 --> 00:07:48,000 +Now, we know that the previous array is sorted, right? + +204 +00:07:48,000 --> 00:07:50,000 +And then, in fact both the values are sorted, + +205 +00:07:50,000 --> 00:07:51,000 +both the arrays are sorted. + +206 +00:07:51,000 --> 00:07:52,000 +When you combine them, + +207 +00:07:52,000 --> 00:07:55,000 +what you can do is you can compare the first two values, + +208 +00:07:55,000 --> 00:07:59,000 +only the first two values, not eight, only five and nine. + +209 +00:07:59,000 --> 00:08:01,000 +So which is smaller? Five. So you will put five here. + +210 +00:08:01,000 --> 00:08:03,000 +Now since five is done, you can simply say, + +211 +00:08:03,000 --> 00:08:04,000 +okay, I'm done with five. + +212 +00:08:04,000 --> 00:08:07,000 +Now let me focus on eight and nine. + +213 +00:08:07,000 --> 00:08:09,000 +So which is smaller? Eight. + +214 +00:08:09,000 --> 00:08:10,000 +And then the only thing remaining here is nine, + +215 +00:08:10,000 --> 00:08:12,000 +so we'll put nine here. + +216 +00:08:12,000 --> 00:08:13,000 +Now that's done. + +217 +00:08:13,000 --> 00:08:14,000 +Now let's combine this three, + +218 +00:08:14,000 --> 00:08:17,000 +which is a one, six, and seven. + +219 +00:08:17,000 --> 00:08:20,000 +Now first you have to combine this two. + +220 +00:08:20,000 --> 00:08:23,000 +So you will get another box here, which is one and six, + +221 +00:08:23,000 --> 00:08:24,000 +so we will compare. + +222 +00:08:24,000 --> 00:08:26,000 +So it is one and six because they're sorted. + +223 +00:08:26,000 --> 00:08:28,000 +So now you'll combine the next part, + +224 +00:08:28,000 --> 00:08:30,000 +you will get three values here. + +225 +00:08:30,000 --> 00:08:31,000 +Now, when you get three values, what we are doing + +226 +00:08:31,000 --> 00:08:35,000 +is we are basically combining this section and this section, + +227 +00:08:35,000 --> 00:08:36,000 +same steps. + +228 +00:08:36,000 --> 00:08:37,000 +You will compare the first two values. + +229 +00:08:37,000 --> 00:08:40,000 +So one compared with seven. + +230 +00:08:40,000 --> 00:08:42,000 +So one will come here, this is done. + +231 +00:08:42,000 --> 00:08:43,000 +Then compare six and seven, + +232 +00:08:43,000 --> 00:08:45,000 +six will come here, this is done. + +233 +00:08:45,000 --> 00:08:48,000 +Then seven will come here, okay? + +234 +00:08:48,000 --> 00:08:50,000 +And then now the major work starts, + +235 +00:08:50,000 --> 00:08:52,000 +we have to combine this two. + +236 +00:08:52,000 --> 00:08:53,000 +Now when you're combining this two, + +237 +00:08:53,000 --> 00:08:56,000 +you will get a bigger array of six elements. + +238 +00:08:56,000 --> 00:09:01,000 +So one, two, three, four, five, six, so we got six blocks. + +239 +00:09:01,000 --> 00:09:04,000 +Now what you will do is you got two arrays + +240 +00:09:04,000 --> 00:09:06,000 +and we know that those two areas are actually sorted, + +241 +00:09:06,000 --> 00:09:07,000 +when you combine them, + +242 +00:09:07,000 --> 00:09:09,000 +again, you will start with the first two values. + +243 +00:09:09,000 --> 00:09:13,000 +So when I say first two values, let's compare five and one. + +244 +00:09:13,000 --> 00:09:14,000 +So what you will do is you'll compare, + +245 +00:09:14,000 --> 00:09:16,000 +okay, five and one which will come here? + +246 +00:09:16,000 --> 00:09:19,000 +So it is one, so this is done. + +247 +00:09:19,000 --> 00:09:21,000 +Then you compare six and five + +248 +00:09:21,000 --> 00:09:22,000 +because five is still remaining, right? + +249 +00:09:22,000 --> 00:09:24,000 +So five and six, it is five, + +250 +00:09:24,000 --> 00:09:29,000 +then eight and six, which is six. So five done, six done. + +251 +00:09:29,000 --> 00:09:33,000 +Then eight and seven, so which is seven? So seven done. + +252 +00:09:33,000 --> 00:09:35,000 +Then eight again, + +253 +00:09:35,000 --> 00:09:38,000 +now we don't have anything to compare from the next array. + +254 +00:09:38,000 --> 00:09:41,000 +So you can simply take these two values as it is here. + +255 +00:09:41,000 --> 00:09:43,000 +This is merge sort, okay? + +256 +00:09:43,000 --> 00:09:45,000 +So you get this sorted value, I hope. + +257 +00:09:45,000 --> 00:09:46,000 +Yeah, you've got sort of value. + +258 +00:09:46,000 --> 00:09:49,000 +So this is how basically the merge sort works. + +259 +00:09:49,000 --> 00:09:51,000 +That's how you basically divide and you combine. + +260 +00:09:51,000 --> 00:09:53,000 +But if you want to do this in programming, + +261 +00:09:53,000 --> 00:09:54,000 +how will you do it? + +262 +00:09:54,000 --> 00:09:55,000 +And we'll keep this as a reference. + +263 +00:09:55,000 --> 00:09:59,000 +So we can divide, we can write this section here. + +264 +00:09:59,000 --> 00:10:00,000 +So we can compare with this. + +265 +00:10:00,000 --> 00:10:02,000 +Now if you want to write an algorithm for this, + +266 +00:10:02,000 --> 00:10:05,000 +first of all, you will get an array, of course. + +267 +00:10:05,000 --> 00:10:08,000 +Now in this array, you'll have some values, + +268 +00:10:08,000 --> 00:10:10,000 +and we can take the same values which we have here, right? + +269 +00:10:10,000 --> 00:10:12,000 +So let's say we have an array with some values here, + +270 +00:10:12,000 --> 00:10:14,000 +the same values which we have. + +271 +00:10:14,000 --> 00:10:16,000 +And then the next step is + +272 +00:10:16,000 --> 00:10:18,000 +you basically need to create a function, + +273 +00:10:18,000 --> 00:10:20,000 +let's say merge sort. + +274 +00:10:20,000 --> 00:10:22,000 +And this is where you will do these sorting part. + +275 +00:10:22,000 --> 00:10:23,000 +So when I say merge sort, the first thing you will do + +276 +00:10:23,000 --> 00:10:26,000 +is you will say divide. + +277 +00:10:26,000 --> 00:10:26,000 +Now, when you say divide, + +278 +00:10:26,000 --> 00:10:29,000 +you have to pass two, three things. + +279 +00:10:29,000 --> 00:10:31,000 +First you have to pass the array, + +280 +00:10:31,000 --> 00:10:34,000 +then you have to pass the l value, r value, right? + +281 +00:10:34,000 --> 00:10:37,000 +And then if you observe, we are dividing, + +282 +00:10:37,000 --> 00:10:39,000 +again you're dividing, again you're dividing, + +283 +00:10:39,000 --> 00:10:41,000 +so don't you think this is a concept of recursion + +284 +00:10:41,000 --> 00:10:43,000 +when you do the same thing multiple times? + +285 +00:10:43,000 --> 00:10:44,000 +So we have to do recursion here. + +286 +00:10:44,000 --> 00:10:47,000 +So basically here we have to pass three values, right? + +287 +00:10:47,000 --> 00:10:49,000 +So you will pass an array, + +288 +00:10:49,000 --> 00:10:53,000 +you will pass the left value and right value for each array. + +289 +00:10:53,000 --> 00:10:55,000 +Now as this will be called multiple times, + +290 +00:10:55,000 --> 00:10:57,000 +in recursion every time you have to pass the array, + +291 +00:10:57,000 --> 00:10:58,000 +the left and the right. + +292 +00:10:58,000 --> 00:10:59,000 +Remember one thing, + +293 +00:10:59,000 --> 00:11:03,000 +we are not dividing the array physically, + +294 +00:11:03,000 --> 00:11:04,000 +we are dividing it logically. + +295 +00:11:04,000 --> 00:11:06,000 +So even if you divide this array, + +296 +00:11:06,000 --> 00:11:08,000 +ultimately, the values will remain same. Okay. + +297 +00:11:08,000 --> 00:11:10,000 +Now when I say divide what it means? + +298 +00:11:10,000 --> 00:11:11,000 +The first thing you will do + +299 +00:11:11,000 --> 00:11:13,000 +is you have to find the median or the mid value. + +300 +00:11:13,000 --> 00:11:15,000 +So I can say mid, but then since this, + +301 +00:11:15,000 --> 00:11:17,000 +okay, so let's find mid here. + +302 +00:11:17,000 --> 00:11:19,000 +So we got mid is equal to, + +303 +00:11:19,000 --> 00:11:22,000 +we know the formula, l plus r divided by two. + +304 +00:11:22,000 --> 00:11:25,000 +Now once we got the median, what we're gonna do is, + +305 +00:11:25,000 --> 00:11:28,000 +once we got the median, you will break down into two parts + +306 +00:11:28,000 --> 00:11:31,000 +and you will apply the merge sort in each section. + +307 +00:11:31,000 --> 00:11:32,000 +So what I'm saying is when I'm pass, + +308 +00:11:32,000 --> 00:11:34,000 +when I'm calling this for the first time, + +309 +00:11:34,000 --> 00:11:36,000 +I'm passing the entire array + +310 +00:11:36,000 --> 00:11:39,000 +with the l value as zero and r value as five, + +311 +00:11:39,000 --> 00:11:40,000 +which is the last index. + +312 +00:11:40,000 --> 00:11:43,000 +But again, I'm going to call the merge sort + +313 +00:11:43,000 --> 00:11:45,000 +by passing different values. + +314 +00:11:45,000 --> 00:11:47,000 +So I'm going to pass arr, + +315 +00:11:47,000 --> 00:11:51,000 +next I need to pass the first array index value. + +316 +00:11:51,000 --> 00:11:55,000 +So the first array here starts from zero to two, + +317 +00:11:55,000 --> 00:11:56,000 +which is the median. + +318 +00:11:56,000 --> 00:12:00,000 +So starting is l, ending is median or middle value + +319 +00:12:00,000 --> 00:12:02,000 +or mid value we can say. + +320 +00:12:02,000 --> 00:12:06,000 +Again, I will call merge sort for this second array. + +321 +00:12:06,000 --> 00:12:08,000 +Again, I have to pass arr, + +322 +00:12:08,000 --> 00:12:09,000 +but this time you have to pass, + +323 +00:12:09,000 --> 00:12:13,000 +if you look at the values, we have to pass three and five. + +324 +00:12:14,000 --> 00:12:16,000 +Now, where do we have three? We don't have three anywhere. + +325 +00:12:16,000 --> 00:12:18,000 +So we can say mid plus one. + +326 +00:12:18,000 --> 00:12:21,000 +So mid is two, so mid plus one is three, + +327 +00:12:21,000 --> 00:12:23,000 +and the ending is right, okay? + +328 +00:12:23,000 --> 00:12:24,000 +So that's how you call the merge sort. + +329 +00:12:24,000 --> 00:12:26,000 +We have to remember one more thing. + +330 +00:12:26,000 --> 00:12:29,000 +After doing merge sort, which will break it, + +331 +00:12:29,000 --> 00:12:31,000 +we have to merge them as well. + +332 +00:12:31,000 --> 00:12:33,000 +So when you say merge, you have to pass four things here. + +333 +00:12:33,000 --> 00:12:36,000 +First they array, the left value, the mid value, + +334 +00:12:36,000 --> 00:12:39,000 +and you have to also pass the right value. + +335 +00:12:39,000 --> 00:12:40,000 +Again, we'll see the logic later, + +336 +00:12:40,000 --> 00:12:43,000 +but this is the merge you have to pass. + +337 +00:12:43,000 --> 00:12:46,000 +But, if you call the function by itself, + +338 +00:12:46,000 --> 00:12:47,000 +you can see merge that is calling itself, + +339 +00:12:47,000 --> 00:12:49,000 +but don't you think there should be a limit + +340 +00:12:49,000 --> 00:12:50,000 +when it should end? + +341 +00:12:50,000 --> 00:12:52,000 +So, of course, there should be a condition as well. + +342 +00:12:52,000 --> 00:12:55,000 +When your left is less than right, then only you do this, + +343 +00:12:55,000 --> 00:12:56,000 +otherwise stop, okay? + +344 +00:12:56,000 --> 00:12:58,000 +This is your merge sort. + +345 +00:12:58,000 --> 00:12:59,000 +So by doing this, what you will get is + +346 +00:12:59,000 --> 00:13:02,000 +you will get the values divided. + +347 +00:13:02,000 --> 00:13:04,000 +But, where's the concept of merge? + +348 +00:13:04,000 --> 00:13:05,000 +We are calling merge, + +349 +00:13:05,000 --> 00:13:08,000 +but we are not defined the merge function yet. + +350 +00:13:08,000 --> 00:13:10,000 +Now this is where things gets complicated. + +351 +00:13:10,000 --> 00:13:13,000 +Not exactly complicated, but it's a lengthy process. + +352 +00:13:13,000 --> 00:13:15,000 +So what we need to do is when you say merge, + +353 +00:13:15,000 --> 00:13:17,000 +of course, you have to pass all these values here. + +354 +00:13:17,000 --> 00:13:20,000 +Now in this merge, what we are going to do? + +355 +00:13:20,000 --> 00:13:21,000 +Now basically the first thing is + +356 +00:13:21,000 --> 00:13:23,000 +you need to create two array. + +357 +00:13:23,000 --> 00:13:24,000 +So I will not write the logic, exact logic here, + +358 +00:13:24,000 --> 00:13:26,000 +but let me just draw something. + +359 +00:13:26,000 --> 00:13:29,000 +What we need is we basically need two arrays here. + +360 +00:13:29,000 --> 00:13:31,000 +When you say merge at one time you need two arrays. + +361 +00:13:31,000 --> 00:13:34,000 +Example, so when we are combining these two, + +362 +00:13:34,000 --> 00:13:35,000 +we got one array. + +363 +00:13:35,000 --> 00:13:38,000 +So at one point you need two add to combine + +364 +00:13:38,000 --> 00:13:39,000 +so that you will get one big array. + +365 +00:13:39,000 --> 00:13:42,000 +Same goes here, when you got two arrays here, + +366 +00:13:42,000 --> 00:13:44,000 +we to combine them to get a big array. + +367 +00:13:44,000 --> 00:13:49,000 +So that means every time I call merge, I need two arrays. + +368 +00:13:49,000 --> 00:13:53,000 +We'll call them, let's say left array and right array. + +369 +00:13:53,000 --> 00:13:54,000 +Okay, so we got two arrays here. + +370 +00:13:54,000 --> 00:13:56,000 +The next thing we need to do is we need to copy the elements + +371 +00:13:56,000 --> 00:13:58,000 +of the left array and the right array. + +372 +00:13:58,000 --> 00:13:59,000 +This is empty at this point. + +373 +00:13:59,000 --> 00:14:03,000 +So we need to copy this values from the actual array, okay? + +374 +00:14:03,000 --> 00:14:05,000 +Now, so we need to write a for loop, + +375 +00:14:05,000 --> 00:14:08,000 +which will copy data from the array. + +376 +00:14:08,000 --> 00:14:10,000 +So basically it'll copy data from the array + +377 +00:14:10,000 --> 00:14:12,000 +and put them in this list, okay? + +378 +00:14:12,000 --> 00:14:16,000 +So maybe I just, I will just write copy values. + +379 +00:14:16,000 --> 00:14:19,000 +Again, we'll see the actual logic in the code itself, okay. + +380 +00:14:19,000 --> 00:14:20,000 +Next thing we have to do is + +381 +00:14:20,000 --> 00:14:22,000 +once we have done with the copying part, + +382 +00:14:22,000 --> 00:14:25,000 +you will simply use these two arrays to combine + +383 +00:14:25,000 --> 00:14:27,000 +to get a bigger array. + +384 +00:14:27,000 --> 00:14:28,000 +Now, how will you do this? + +385 +00:14:28,000 --> 00:14:30,000 +So, of course, there should be partition here, + +386 +00:14:30,000 --> 00:14:31,000 +there should be some values. + +387 +00:14:31,000 --> 00:14:33,000 +So what we'll do is let's compare, + +388 +00:14:33,000 --> 00:14:34,000 +let's work with the last values here. + +389 +00:14:34,000 --> 00:14:38,000 +So we got the last value as five, eight, nine, + +390 +00:14:38,000 --> 00:14:41,000 +and one, six, seven. + +391 +00:14:41,000 --> 00:14:44,000 +So the logic of combining this is + +392 +00:14:44,000 --> 00:14:45,000 +you will need multiple variables here. + +393 +00:14:45,000 --> 00:14:49,000 +You need a variable i, you need a variable j. + +394 +00:14:49,000 --> 00:14:51,000 +Now why we need this two variables? + +395 +00:14:51,000 --> 00:14:53,000 +One to represents this particular array + +396 +00:14:53,000 --> 00:14:55,000 +and one to represent this particular array to iterate. + +397 +00:14:56,000 --> 00:14:59,000 +Then we also need a k, which will use, + +398 +00:14:59,000 --> 00:15:01,000 +which will be used for the big array + +399 +00:15:01,000 --> 00:15:03,000 +which you're going to create. + +400 +00:15:03,000 --> 00:15:06,000 +Sort of from these three values or three three values, + +401 +00:15:06,000 --> 00:15:08,000 +you're going to create an area of six, right? + +402 +00:15:08,000 --> 00:15:10,000 +So k will be handling this part. + +403 +00:15:10,000 --> 00:15:12,000 +Now, every time you copy one element, + +404 +00:15:12,000 --> 00:15:13,000 +so let's say when you compare five and one + +405 +00:15:13,000 --> 00:15:17,000 +and you know that you're going to copy one here. + +406 +00:15:17,000 --> 00:15:20,000 +So basically the j value will shift here, right? + +407 +00:15:20,000 --> 00:15:21,000 +Because this is done. + +408 +00:15:21,000 --> 00:15:22,000 +The same thing goes here, + +409 +00:15:22,000 --> 00:15:26,000 +so once you got one, next time you will get five here, + +410 +00:15:26,000 --> 00:15:29,000 +then you have to increment this here, so i goes here. + +411 +00:15:29,000 --> 00:15:30,000 +And, of course, every time you copy element + +412 +00:15:30,000 --> 00:15:34,000 +in the bigger array, you will increment the value of k. + +413 +00:15:34,000 --> 00:15:36,000 +That's the logic, okay? + +414 +00:15:36,000 --> 00:15:39,000 +So this is how you are going to complete the merge sort. + +415 +00:15:39,000 --> 00:15:41,000 +Now how exactly you have to write the code? + +416 +00:15:41,000 --> 00:15:43,000 +That we'll see in the practical video. + +417 +00:15:43,000 --> 00:15:44,000 +So I hope merge sort makes sense + +418 +00:15:44,000 --> 00:15:47,000 +where you basically break down the entire + +419 +00:15:47,000 --> 00:15:48,000 +array into small chunks + +420 +00:15:48,000 --> 00:15:51,000 +and then you combine them to get sorted values. + diff --git a/28 - DSA (Optional)/018 Merge Sort Code_en.srt b/28 - DSA (Optional)/018 Merge Sort Code_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..76ec153b4f9892debac9c3317db5a2d338bcbd1f --- /dev/null +++ b/28 - DSA (Optional)/018 Merge Sort Code_en.srt @@ -0,0 +1,1240 @@ +1 +00:00:00,000 --> 00:00:03,000 +-: So now let's implement this logic with the help of Java. + +2 +00:00:03,000 --> 00:00:07,000 +Now the thing is, first we need a array, right? + +3 +00:00:07,000 --> 00:00:08,000 +So let's create a array here. + +4 +00:00:08,000 --> 00:00:12,000 +So let's say int arr, and this will have certain values. + +5 +00:00:12,000 --> 00:00:13,000 +So let's have some values here. + +6 +00:00:13,000 --> 00:00:18,000 +We'll say 3, 5, 1, 4, 6, 2. + +7 +00:00:18,000 --> 00:00:20,000 +So we got this values here + +8 +00:00:20,000 --> 00:00:22,000 +and let's apply the sorting techniques on this. + +9 +00:00:22,000 --> 00:00:25,000 +So before sorting, I will print some values. + +10 +00:00:25,000 --> 00:00:28,000 +I'll say for n in array, + +11 +00:00:31,000 --> 00:00:32,000 +and let's print these values. + +12 +00:00:32,000 --> 00:00:34,000 +So I will say print n, + +13 +00:00:34,000 --> 00:00:36,000 +but I will also make sure that I will put a space, + +14 +00:00:36,000 --> 00:00:38,000 +I will not print on new line. + +15 +00:00:38,000 --> 00:00:42,000 +And once it is done, I'll print a new line + +16 +00:00:42,000 --> 00:00:44,000 +and then after sorting also, so I will say this, + +17 +00:00:44,000 --> 00:00:47,000 +I will just write it here after sorting. + +18 +00:00:47,000 --> 00:00:49,000 +So here, let's do the same thing + +19 +00:00:50,000 --> 00:00:52,000 +and let's say after sorting, it should print some value. + +20 +00:00:52,000 --> 00:00:56,000 +But at this point, if you run this code, let's run this. + +21 +00:00:56,000 --> 00:00:59,000 +And you can see in the output they both are unsorted. + +22 +00:00:59,000 --> 00:01:00,000 +So because that's why we have + +23 +00:01:00,000 --> 00:01:01,000 +to do some sorting here, right? + +24 +00:01:01,000 --> 00:01:03,000 +So let's apply the sorting logic. + +25 +00:01:03,000 --> 00:01:07,000 +So what I will do is I will simply call a merge sort method, + +26 +00:01:07,000 --> 00:01:09,000 +which will basically accept three values. + +27 +00:01:09,000 --> 00:01:11,000 +The arr, the L, + +28 +00:01:11,000 --> 00:01:13,000 +now L here is zero, + +29 +00:01:13,000 --> 00:01:15,000 +and then you have to also pause the + +30 +00:01:15,000 --> 00:01:17,000 +array dot length minus one, + +31 +00:01:17,000 --> 00:01:19,000 +basically you have to pause the R value as well. + +32 +00:01:19,000 --> 00:01:21,000 +Remember, we have to pause three variables. + +33 +00:01:21,000 --> 00:01:23,000 +Now since we don't have this method, + +34 +00:01:23,000 --> 00:01:25,000 +I would simply say more action, create this method + +35 +00:01:25,000 --> 00:01:27,000 +and you can see we got this method here. + +36 +00:01:27,000 --> 00:01:30,000 +And let me just put this method, not here, but on top. + +37 +00:01:30,000 --> 00:01:34,000 +Now this is where the actual logic is going to happen. + +38 +00:01:34,000 --> 00:01:36,000 +So you can say, we got two variables, this should be L, + +39 +00:01:36,000 --> 00:01:39,000 +and this should be R. + +40 +00:01:39,000 --> 00:01:42,000 +So left and right, okay. + +41 +00:01:42,000 --> 00:01:45,000 +Now as per the logic, if you can see in our logic, + +42 +00:01:45,000 --> 00:01:47,000 +we have already mentioned this logic. + +43 +00:01:47,000 --> 00:01:50,000 +So basically what we do is we have to first check if + +44 +00:01:50,000 --> 00:01:53,000 +the L value is less than R, then only you do certain things. + +45 +00:01:53,000 --> 00:01:55,000 +And what are the things you have to do? + +46 +00:01:55,000 --> 00:01:57,000 +Basically you have to find the middle value. + +47 +00:01:57,000 --> 00:02:02,000 +So I would say int mid is equal to L plus R divided by two, + +48 +00:02:02,000 --> 00:02:04,000 +and then you have to call mergeSort two times. + +49 +00:02:04,000 --> 00:02:05,000 +Again, I'm just going with the logic + +50 +00:02:05,000 --> 00:02:08,000 +which I returned in the code or in the board here. + +51 +00:02:08,000 --> 00:02:12,000 +So you have to pause arr, and then you have to pause the L, + +52 +00:02:12,000 --> 00:02:14,000 +and then you have to pause mid. + +53 +00:02:14,000 --> 00:02:16,000 +That's how you create two different arrays. + +54 +00:02:16,000 --> 00:02:19,000 +So mid plus one and R, okay? + +55 +00:02:19,000 --> 00:02:21,000 +And then you will simply say merge. + +56 +00:02:21,000 --> 00:02:23,000 +Of course, right after doing this, sorting + +57 +00:02:23,000 --> 00:02:24,000 +after breaking it down, you have to also merge. + +58 +00:02:24,000 --> 00:02:27,000 +But while doing merge, you have to pause three values, + +59 +00:02:27,000 --> 00:02:31,000 +arr, you have to pause the left, + +60 +00:02:31,000 --> 00:02:34,000 +mid and right, you have to pause these three values. + +61 +00:02:34,000 --> 00:02:36,000 +So this is the only logic you have to write + +62 +00:02:36,000 --> 00:02:37,000 +inside mergeSort method. + +63 +00:02:37,000 --> 00:02:39,000 +But then we have to also complete merging + +64 +00:02:39,000 --> 00:02:41,000 +because dividing is very easy. + +65 +00:02:41,000 --> 00:02:44,000 +This is how basically you are dividing the values. + +66 +00:02:44,000 --> 00:02:46,000 +The difficult part is merging them. + +67 +00:02:46,000 --> 00:02:47,000 +So let's implement merge here. + +68 +00:02:47,000 --> 00:02:51,000 +So I will just go back here, more actions, + +69 +00:02:52,000 --> 00:02:54,000 +create a method merge, yes. + +70 +00:02:54,000 --> 00:02:55,000 +So I got a method. + +71 +00:02:55,000 --> 00:02:57,000 +At this point, there are private methods is + +72 +00:02:57,000 --> 00:02:58,000 +because I'm going to call from the same + +73 +00:02:58,000 --> 00:02:59,000 +class, but depending upon your + +74 +00:02:59,000 --> 00:03:01,000 +requirement you can just change them. + +75 +00:03:01,000 --> 00:03:04,000 +And now the actual logic goes here, they merge. + +76 +00:03:04,000 --> 00:03:05,000 +Okay, now as I mentioned, you need + +77 +00:03:05,000 --> 00:03:07,000 +to create two arrays here, right? + +78 +00:03:07,000 --> 00:03:10,000 +So if you can see the logic, we need two arrays. + +79 +00:03:10,000 --> 00:03:13,000 +Let's name this as lrr, which is the left array. + +80 +00:03:14,000 --> 00:03:17,000 +And this will be an array, + +81 +00:03:17,000 --> 00:03:19,000 +but the question is what will the size of it? + +82 +00:03:19,000 --> 00:03:22,000 +Because when I create this array I have to mention the size. + +83 +00:03:22,000 --> 00:03:25,000 +I don't know what is the size here, + +84 +00:03:25,000 --> 00:03:26,000 +but based on what it is current called, + +85 +00:03:26,000 --> 00:03:28,000 +it will be different size, + +86 +00:03:28,000 --> 00:03:31,000 +but the question is how we are going to know the size, + +87 +00:03:31,000 --> 00:03:34,000 +and that's where this LM, all this thing comes, + +88 +00:03:34,000 --> 00:03:37,000 +or LR comes in the picture. + +89 +00:03:37,000 --> 00:03:39,000 +So let's use that and let's get the size. + +90 +00:03:39,000 --> 00:03:41,000 +So if you want the size of the first array, + +91 +00:03:41,000 --> 00:03:43,000 +the first section, + +92 +00:03:43,000 --> 00:03:46,000 +basically you can simply say mid minus L, + +93 +00:03:46,000 --> 00:03:49,000 +because for the first array it starts with L. + +94 +00:03:49,000 --> 00:03:53,000 +If you can see here, it starts with L and ends with mid. + +95 +00:03:53,000 --> 00:03:56,000 +So if you say mid minus L, you will get the length right, + +96 +00:03:56,000 --> 00:03:58,000 +but the problem is since this is zero indexing, + +97 +00:03:58,000 --> 00:04:00,000 +we have to also add plus one + +98 +00:04:00,000 --> 00:04:03,000 +because mid is not representing the size of the array + +99 +00:04:03,000 --> 00:04:05,000 +it represents the index value, + +100 +00:04:05,000 --> 00:04:06,000 +so you have to say plus one. + +101 +00:04:06,000 --> 00:04:07,000 +Then you will get the second, + +102 +00:04:07,000 --> 00:04:11,000 +add a length with the help of R minus mid. + +103 +00:04:11,000 --> 00:04:13,000 +So we'll just put some space here + +104 +00:04:13,000 --> 00:04:15,000 +so that it'll be much more visible. + +105 +00:04:15,000 --> 00:04:17,000 +Okay, so you can see we got these two values + +106 +00:04:17,000 --> 00:04:20,000 +and this is the size you have to mention here. + +107 +00:04:20,000 --> 00:04:22,000 +The first area will be of size n1 + +108 +00:04:22,000 --> 00:04:24,000 +the second area will be of size n2. + +109 +00:04:24,000 --> 00:04:25,000 +That's done. + +110 +00:04:25,000 --> 00:04:27,000 +The next step is actually to copy the values. + +111 +00:04:27,000 --> 00:04:28,000 +If you can see we are mentioning + +112 +00:04:28,000 --> 00:04:30,000 +the copy values here, right, in the notes. + +113 +00:04:30,000 --> 00:04:32,000 +Now how do you copy value? + +114 +00:04:32,000 --> 00:04:34,000 +We can use a for loop and let's use two different variables. + +115 +00:04:34,000 --> 00:04:36,000 +I will say X and Y. + +116 +00:04:36,000 --> 00:04:38,000 +This time X is equal to zero + +117 +00:04:38,000 --> 00:04:40,000 +and depending on the size of the array, + +118 +00:04:40,000 --> 00:04:45,000 +so for the first array the size is n1 and x plus plus, + +119 +00:04:45,000 --> 00:04:47,000 +not used to using X and Y inside a for loop, + +120 +00:04:47,000 --> 00:04:49,000 +but let's try. + +121 +00:04:49,000 --> 00:04:50,000 +So how do you copy? + +122 +00:04:50,000 --> 00:04:54,000 +So arr, this values, which is X. + +123 +00:04:54,000 --> 00:04:55,000 +Now how will you get the values? + +124 +00:04:55,000 --> 00:04:58,000 +So from arr, from the main array, + +125 +00:04:58,000 --> 00:04:59,000 +we have to start from the left. + +126 +00:04:59,000 --> 00:05:01,000 +So you have to say left plus x, + +127 +00:05:01,000 --> 00:05:02,000 +that's how you get the values. + +128 +00:05:02,000 --> 00:05:04,000 +So whatever value of the L is + +129 +00:05:04,000 --> 00:05:06,000 +example, if you look at here + +130 +00:05:07,000 --> 00:05:11,000 +for the first array, the index is 0, 1, 2, + +131 +00:05:11,000 --> 00:05:13,000 +but let's say if you're merging the second array, this one, + +132 +00:05:13,000 --> 00:05:15,000 +the index value is 3, 4, 5, right? + +133 +00:05:15,000 --> 00:05:18,000 +So whatever value of L is, which is the left, + +134 +00:05:18,000 --> 00:05:21,000 +which is three, in this case, it is three plus x. + +135 +00:05:21,000 --> 00:05:24,000 +That's how you will copy and none. + +136 +00:05:24,000 --> 00:05:26,000 +So this is for the first one, let's do the same thing + +137 +00:05:26,000 --> 00:05:28,000 +but a different value. + +138 +00:05:28,000 --> 00:05:30,000 +So I will just copy this for the second array. + +139 +00:05:30,000 --> 00:05:32,000 +Okay, we can also use X here, no problem. + +140 +00:05:32,000 --> 00:05:36,000 +We can say n2 and this will go into the right array. + +141 +00:05:36,000 --> 00:05:39,000 +But the question is what will be the value here? + +142 +00:05:39,000 --> 00:05:40,000 +Now any guess? + +143 +00:05:40,000 --> 00:05:41,000 +So when you say on the right hand side, + +144 +00:05:41,000 --> 00:05:45,000 +it is your mid plus one + +145 +00:05:45,000 --> 00:05:48,000 +because we are not considering the mid value + +146 +00:05:48,000 --> 00:05:52,000 +for the next example, if you go up the mid here was two, + +147 +00:05:52,000 --> 00:05:54,000 +but we started with three. + +148 +00:05:54,000 --> 00:05:55,000 +So you have to say two plus one, + +149 +00:05:55,000 --> 00:05:58,000 +mid plus one so that you will get three. + +150 +00:05:58,000 --> 00:06:00,000 +Okay, so we copied, what's the next step, + +151 +00:06:00,000 --> 00:06:03,000 +and now the actual work starts of merging + +152 +00:06:03,000 --> 00:06:04,000 +and already mentioned we need two variables. + +153 +00:06:04,000 --> 00:06:09,000 +One is I equal to zero, we also need J for the secondary. + +154 +00:06:10,000 --> 00:06:12,000 +So first array will be counted with I + +155 +00:06:12,000 --> 00:06:15,000 +and secondary will be handling, will be handled by J, + +156 +00:06:15,000 --> 00:06:17,000 +but we also need one more counter + +157 +00:06:17,000 --> 00:06:19,000 +which will represent the main array right? + +158 +00:06:19,000 --> 00:06:21,000 +And this value will start with L + +159 +00:06:21,000 --> 00:06:22,000 +because we have to start from the left hand side. + +160 +00:06:22,000 --> 00:06:24,000 +Now once we get these three values, + +161 +00:06:24,000 --> 00:06:25,000 +let's start merging them. + +162 +00:06:25,000 --> 00:06:27,000 +So we have to do multiple times, right? + +163 +00:06:27,000 --> 00:06:29,000 +When you say merge, you have to take one value from here. + +164 +00:06:29,000 --> 00:06:31,000 +Example, when we were doing this, + +165 +00:06:31,000 --> 00:06:32,000 +we would compare the first two values, + +166 +00:06:32,000 --> 00:06:33,000 +compare the next two values. + +167 +00:06:33,000 --> 00:06:35,000 +That's how we were doing it, right? + +168 +00:06:35,000 --> 00:06:39,000 +So when to end this, if your I value is less than n1 + +169 +00:06:39,000 --> 00:06:40,000 +and not this hand + +170 +00:06:40,000 --> 00:06:42,000 +and J value is less than n2, + +171 +00:06:44,000 --> 00:06:45,000 +we'll continue till this point. + +172 +00:06:45,000 --> 00:06:47,000 +If they're not true, then we'll have to stop + +173 +00:06:47,000 --> 00:06:49,000 +because that means we have to, + +174 +00:06:49,000 --> 00:06:50,000 +we have actually reach the end. + +175 +00:06:50,000 --> 00:06:52,000 +But now question arise, how will you merge? + +176 +00:06:52,000 --> 00:06:54,000 +And that's where you have to start comparing values. + +177 +00:06:54,000 --> 00:06:56,000 +Now when you say compare what it means, + +178 +00:06:56,000 --> 00:06:57,000 +compare five and one. + +179 +00:06:57,000 --> 00:06:59,000 +So basically the first element of this array + +180 +00:06:59,000 --> 00:07:01,000 +and the first element of this array, let's compare them. + +181 +00:07:01,000 --> 00:07:02,000 +How will you do that? + +182 +00:07:02,000 --> 00:07:05,000 +So it's simply check if the L array of I, + +183 +00:07:08,000 --> 00:07:10,000 +because that's what is representing the L array, + +184 +00:07:10,000 --> 00:07:11,000 +which is the I variable. + +185 +00:07:11,000 --> 00:07:14,000 +If it is less than, and we can also say equal to, + +186 +00:07:14,000 --> 00:07:15,000 +what if they're equal, + +187 +00:07:15,000 --> 00:07:20,000 +then we have to compare this with the R array of J. + +188 +00:07:20,000 --> 00:07:23,000 +If the left one is smaller in this case, + +189 +00:07:23,000 --> 00:07:25,000 +the other one is smaller, not the left one, + +190 +00:07:25,000 --> 00:07:26,000 +but let's say the left one is smaller, + +191 +00:07:26,000 --> 00:07:29,000 +in that case you will put that into the main array. + +192 +00:07:29,000 --> 00:07:34,000 +So array K is equal to L array, + +193 +00:07:34,000 --> 00:07:36,000 +not length, L array of I. + +194 +00:07:36,000 --> 00:07:37,000 +Now since we have used, + +195 +00:07:37,000 --> 00:07:39,000 +so let's say this was a smaller value + +196 +00:07:39,000 --> 00:07:41,000 +and once we have used this, we have to cancel this, right? + +197 +00:07:41,000 --> 00:07:42,000 +I mean this is done. + +198 +00:07:42,000 --> 00:07:45,000 +So we can shift our pointer to the next element. + +199 +00:07:45,000 --> 00:07:48,000 +How do you shift your, not the pointer, but the reference, + +200 +00:07:48,000 --> 00:07:51,000 +you will simply say I plus plus here, in this case, + +201 +00:07:51,000 --> 00:07:52,000 +but what if it is not same + +202 +00:07:52,000 --> 00:07:54,000 +or notice what if it is not true? + +203 +00:07:54,000 --> 00:07:56,000 +In that case, you will go to L spot, + +204 +00:07:56,000 --> 00:07:59,000 +you will do the same thing, but not with the left array, + +205 +00:07:59,000 --> 00:08:01,000 +this time you will do with the right array + +206 +00:08:01,000 --> 00:08:04,000 +and then the J variable and you will say J plus plus. + +207 +00:08:04,000 --> 00:08:06,000 +And every time you run this loop, because see + +208 +00:08:06,000 --> 00:08:08,000 +after one iteration of this loop, + +209 +00:08:08,000 --> 00:08:11,000 +basically you will get the first value of the array, + +210 +00:08:11,000 --> 00:08:12,000 +right, main array. + +211 +00:08:12,000 --> 00:08:13,000 +You will simply say K plus plus, + +212 +00:08:13,000 --> 00:08:16,000 +because you have to also increment this. + +213 +00:08:16,000 --> 00:08:19,000 +So once I is done, because K is here, + +214 +00:08:19,000 --> 00:08:23,000 +once I is done, you have to move K here, okay? + +215 +00:08:23,000 --> 00:08:24,000 +And that's why you're doing K plus plus + +216 +00:08:24,000 --> 00:08:26,000 +and that's it, this is your mergeSort. + +217 +00:08:26,000 --> 00:08:27,000 +Let's see if that works. + +218 +00:08:27,000 --> 00:08:28,000 +Let's rephrase this. + +219 +00:08:28,000 --> 00:08:29,000 +I'll just rerun + +220 +00:08:29,000 --> 00:08:32,000 +and we got an error, something went wrong. + +221 +00:08:32,000 --> 00:08:34,000 +You can see everything was going well, + +222 +00:08:34,000 --> 00:08:37,000 +but this for is not at right place. + +223 +00:08:37,000 --> 00:08:40,000 +We got one there, because sometime what may happen is + +224 +00:08:40,000 --> 00:08:42,000 +when you are copying two arrays, + +225 +00:08:42,000 --> 00:08:45,000 +what if the values are left in one of the array? + +226 +00:08:45,000 --> 00:08:47,000 +Example, if you look at our example + +227 +00:08:47,000 --> 00:08:48,000 +when we were copying this, + +228 +00:08:48,000 --> 00:08:50,000 +remember when we were completing the entire task, + +229 +00:08:50,000 --> 00:08:53,000 +these two elements were remaining from the first array, + +230 +00:08:53,000 --> 00:08:55,000 +which goes at the end. + +231 +00:08:55,000 --> 00:08:57,000 +So that thing is not covered yet. + +232 +00:08:57,000 --> 00:08:59,000 +So what you do is for the remaining elements + +233 +00:08:59,000 --> 00:09:01,000 +of the particular array, + +234 +00:09:01,000 --> 00:09:02,000 +you will again run a Y loop here. + +235 +00:09:02,000 --> 00:09:06,000 +So it'll say if I is still not equal + +236 +00:09:06,000 --> 00:09:08,000 +or still not less than n1, + +237 +00:09:08,000 --> 00:09:10,000 +you will simply copy the remaining elements, + +238 +00:09:10,000 --> 00:09:12,000 +whatever is left, because after this loop, + +239 +00:09:12,000 --> 00:09:15,000 +at least one array will be ended, okay? + +240 +00:09:15,000 --> 00:09:16,000 +So we have to work for the second + +241 +00:09:16,000 --> 00:09:18,000 +of the remaining element of one of the array + +242 +00:09:18,000 --> 00:09:19,000 +maybe left hand side or right hand side. + +243 +00:09:19,000 --> 00:09:21,000 +First we're trying to understand left hand side. + +244 +00:09:21,000 --> 00:09:23,000 +Let's see what happens on the left hand side, + +245 +00:09:23,000 --> 00:09:24,000 +simply copy all the values. + +246 +00:09:24,000 --> 00:09:27,000 +So you will say arr of K is equal to, + +247 +00:09:27,000 --> 00:09:30,000 +we have to copy from the left array, and let's use I here. + +248 +00:09:30,000 --> 00:09:31,000 +Now, every time you do this, + +249 +00:09:31,000 --> 00:09:36,000 +you will increment both the elements I and K both. + +250 +00:09:36,000 --> 00:09:39,000 +So this is what the left hand side left out values. + +251 +00:09:39,000 --> 00:09:41,000 +The same thing should be done for this right hand side. + +252 +00:09:41,000 --> 00:09:44,000 +So this time I will say J is less than n2. + +253 +00:09:44,000 --> 00:09:46,000 +In that case, you will copy, + +254 +00:09:46,000 --> 00:09:48,000 +but not from the right, left hand side + +255 +00:09:48,000 --> 00:09:50,000 +you will do it from the right hand side, the variable is J + +256 +00:09:50,000 --> 00:09:52,000 +and the increment you'll do for J plus plus. + +257 +00:09:52,000 --> 00:09:56,000 +Okay, and let's run this and you can see now it is sorted + +258 +00:09:56,000 --> 00:09:58,000 +and now it doesn't matter what values you have. + +259 +00:09:58,000 --> 00:09:59,000 +Let's say, let's take the values which we have + +260 +00:09:59,000 --> 00:10:00,000 +written on the board, + +261 +00:10:00,000 --> 00:10:02,000 +which was eight, what was the value? + +262 +00:10:02,000 --> 00:10:07,000 +8, 5, 9, 1, 6, 7. + +263 +00:10:07,000 --> 00:10:11,000 +And if you run this, you can see we got sorted values. + +264 +00:10:11,000 --> 00:10:13,000 +Let's add some more values here + +265 +00:10:13,000 --> 00:10:15,000 +just to remove the confusion with different values. + +266 +00:10:15,000 --> 00:10:17,000 +Let's say 1, 1, 1. + +267 +00:10:17,000 --> 00:10:19,000 +This is 57, + +268 +00:10:19,000 --> 00:10:24,000 +and now let's see, 6, 7, 8, 9, 57, 75, 1, 1, 1. + +269 +00:10:24,000 --> 00:10:25,000 +So this is your mergeSort. + +270 +00:10:25,000 --> 00:10:28,000 +So the steps, basically you have to first do + +271 +00:10:28,000 --> 00:10:30,000 +the breakdown of, + +272 +00:10:30,000 --> 00:10:32,000 +you have to break the array down into small chunks, + +273 +00:10:32,000 --> 00:10:34,000 +and then you have to also merge them. + +274 +00:10:34,000 --> 00:10:36,000 +The only thing you have to remember is, + +275 +00:10:36,000 --> 00:10:37,000 +when we were drawing this, + +276 +00:10:37,000 --> 00:10:38,000 +we were going in two sections, right? + +277 +00:10:38,000 --> 00:10:39,000 +Something like this. + +278 +00:10:39,000 --> 00:10:40,000 +We were saying, okay, + +279 +00:10:40,000 --> 00:10:42,000 +we will create two different section + +280 +00:10:42,000 --> 00:10:44,000 +and then first section will break into two parts, + +281 +00:10:44,000 --> 00:10:46,000 +and second section we'll break into two parts. + +282 +00:10:46,000 --> 00:10:47,000 +No, that's not how it's working. + +283 +00:10:47,000 --> 00:10:50,000 +If you observe, what it will do is + +284 +00:10:50,000 --> 00:10:52,000 +it will break down the entire section into two parts, yes, + +285 +00:10:52,000 --> 00:10:54,000 +but then first only you will get the first part. + +286 +00:10:54,000 --> 00:10:56,000 +Then you, you're not going to the second part, okay? + +287 +00:10:56,000 --> 00:10:59,000 +You're not looking at here, you, this is not there yet. + +288 +00:10:59,000 --> 00:11:00,000 +You're still into the first part. + +289 +00:11:00,000 --> 00:11:01,000 +Then this will divide into a section. + +290 +00:11:01,000 --> 00:11:03,000 +You will get this eight, five. + +291 +00:11:03,000 --> 00:11:04,000 +Then you're dividing into two sections, + +292 +00:11:04,000 --> 00:11:06,000 +which you will get eight, five, + +293 +00:11:06,000 --> 00:11:07,000 +and then you will merge them. + +294 +00:11:07,000 --> 00:11:09,000 +Then you will start break breaking down the second part, + +295 +00:11:09,000 --> 00:11:10,000 +which is nine, okay? + +296 +00:11:10,000 --> 00:11:12,000 +Nine was still not there. + +297 +00:11:12,000 --> 00:11:13,000 +Then once, this is also merged, + +298 +00:11:13,000 --> 00:11:17,000 +once you get this, it will start working on the next part. + +299 +00:11:17,000 --> 00:11:21,000 +Then it will work on this, it will work on this, this merge + +300 +00:11:21,000 --> 00:11:24,000 +and merge, I mean, sorry, break down. + +301 +00:11:24,000 --> 00:11:26,000 +Then you get seven, then you merge this two, + +302 +00:11:26,000 --> 00:11:28,000 +and then once you have merged, + +303 +00:11:28,000 --> 00:11:29,000 +then it'll go for the next two. + +304 +00:11:29,000 --> 00:11:31,000 +So it's not exactly breaking down + +305 +00:11:31,000 --> 00:11:33,000 +the entire section equally. + +306 +00:11:33,000 --> 00:11:36,000 +It's basically going from left to right. + +307 +00:11:36,000 --> 00:11:37,000 +So yeah, that's how things are working out. + +308 +00:11:37,000 --> 00:11:40,000 +And this is your mergeSort with the help of Java code. + +309 +00:11:40,000 --> 00:11:43,000 +And I hope this makes sense, let me in the comment section. + +310 +00:11:43,000 --> 00:11:44,000 +See you in the next video. + diff --git a/28 - DSA (Optional)/019 Linkedlist Theory_en.srt b/28 - DSA (Optional)/019 Linkedlist Theory_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..f5f990695ce73dc5d3fff5e6fd7ab00df7ab7740 --- /dev/null +++ b/28 - DSA (Optional)/019 Linkedlist Theory_en.srt @@ -0,0 +1,1704 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: In this video we'll talk about linked list, + +2 +00:00:02,000 --> 00:00:06,000 +but then why do we need to go for another data structure? + +3 +00:00:06,000 --> 00:00:08,000 +Now to this point we have talked about arrays, right? + +4 +00:00:08,000 --> 00:00:12,000 +So whatever examples we did, we did with the array concept. + +5 +00:00:12,000 --> 00:00:14,000 +Now array is great. + +6 +00:00:14,000 --> 00:00:15,000 +So basically what is array? + +7 +00:00:15,000 --> 00:00:16,000 +Just to reiterate, + +8 +00:00:16,000 --> 00:00:19,000 +if you have a sequence of values + +9 +00:00:19,000 --> 00:00:21,000 +which you want to store together, + +10 +00:00:21,000 --> 00:00:23,000 +we can basically use an array. + +11 +00:00:23,000 --> 00:00:26,000 +So this here is an aray. + +12 +00:00:26,000 --> 00:00:29,000 +So basically array a will have different values + +13 +00:00:29,000 --> 00:00:34,000 +and then every value here will have a index number as well. + +14 +00:00:34,000 --> 00:00:37,000 +So basically this is index zero, index one, index two, + +15 +00:00:37,000 --> 00:00:39,000 +index three, and inside this you can have any value. + +16 +00:00:39,000 --> 00:00:42,000 +Let's say if the value is five, eighth, one, six. + +17 +00:00:42,000 --> 00:00:44,000 +So these are the values you have there. + +18 +00:00:44,000 --> 00:00:46,000 +Now, if you want to search + +19 +00:00:46,000 --> 00:00:48,000 +or if you want to get any element from this, + +20 +00:00:48,000 --> 00:00:49,000 +it is very easy, + +21 +00:00:49,000 --> 00:00:52,000 +example, if you want a third value, + +22 +00:00:52,000 --> 00:00:53,000 +you can simply say, + +23 +00:00:53,000 --> 00:00:56,000 +"Okay, I have an array and I want a third value". + +24 +00:00:56,000 --> 00:00:58,000 +That means you have to use the index value too. + +25 +00:00:58,000 --> 00:01:02,000 +So basically getting a element from an array is very easy. + +26 +00:01:02,000 --> 00:01:04,000 +You just have to specify the index number + +27 +00:01:04,000 --> 00:01:05,000 +and you will get the value. + +28 +00:01:05,000 --> 00:01:07,000 +And if you want to understand + +29 +00:01:07,000 --> 00:01:09,000 +the time complexity for this, + +30 +00:01:09,000 --> 00:01:12,000 +this will be big O of one is because + +31 +00:01:12,000 --> 00:01:14,000 +you simply specify index number. + +32 +00:01:14,000 --> 00:01:16,000 +You will get the devalue is so fast, + +33 +00:01:16,000 --> 00:01:18,000 +but there's the some problems as well. + +34 +00:01:18,000 --> 00:01:19,000 +The problem with arrays, + +35 +00:01:19,000 --> 00:01:22,000 +the size of the array is fixed. + +36 +00:01:22,000 --> 00:01:24,000 +That's the basic property of an array. + +37 +00:01:24,000 --> 00:01:26,000 +Now once you create the array of four elements, + +38 +00:01:26,000 --> 00:01:29,000 +you can maximum have four elements. + +39 +00:01:29,000 --> 00:01:30,000 +You can't go beyond it. + +40 +00:01:30,000 --> 00:01:32,000 +So let's say if I want to add one more value here, + +41 +00:01:32,000 --> 00:01:33,000 +it is not possible. + +42 +00:01:33,000 --> 00:01:34,000 +It is because the size is fixed. + +43 +00:01:34,000 --> 00:01:36,000 +The solution could be, + +44 +00:01:36,000 --> 00:01:37,000 +there are two solutions here, actually. + +45 +00:01:37,000 --> 00:01:41,000 +One, what if you can create another big array, + +46 +00:01:41,000 --> 00:01:42,000 +something like this + +47 +00:01:42,000 --> 00:01:44,000 +and store all the four variables here. + +48 +00:01:44,000 --> 00:01:46,000 +So you have to basically copy all the variables + +49 +00:01:46,000 --> 00:01:48,000 +and then you have to copy all the values + +50 +00:01:48,000 --> 00:01:50,000 +and then you can add the extra value. + +51 +00:01:50,000 --> 00:01:51,000 +Let's say you want to add seven. + +52 +00:01:51,000 --> 00:01:55,000 +So basically you'll be having five, eight, one and six, + +53 +00:01:55,000 --> 00:01:56,000 +and then you can have seven. + +54 +00:01:56,000 --> 00:01:59,000 +So what we did is create a new array, + +55 +00:01:59,000 --> 00:02:02,000 +copy all the elements and then add a new value. + +56 +00:02:02,000 --> 00:02:03,000 +What if you want to add more values? + +57 +00:02:03,000 --> 00:02:05,000 +The same process goes on. + +58 +00:02:05,000 --> 00:02:06,000 +One more thing you can do is + +59 +00:02:06,000 --> 00:02:08,000 +when you created the new array, + +60 +00:02:08,000 --> 00:02:10,000 +let's not have a size of five. + +61 +00:02:10,000 --> 00:02:12,000 +Maybe the new array which you create + +62 +00:02:12,000 --> 00:02:13,000 +will be of size eight + +63 +00:02:13,000 --> 00:02:16,000 +so that you can accommodate four more values. + +64 +00:02:16,000 --> 00:02:18,000 +But again, the problem is if you go beyond it, + +65 +00:02:18,000 --> 00:02:20,000 +again, you have to create a new array. + +66 +00:02:20,000 --> 00:02:23,000 +Again, you have to copy the elements and then save it. + +67 +00:02:23,000 --> 00:02:24,000 +I know what you're thinking, + +68 +00:02:24,000 --> 00:02:27,000 +you're thinking, okay, what if you can create a array, + +69 +00:02:27,000 --> 00:02:31,000 +a big array of size, 1000. + +70 +00:02:31,000 --> 00:02:32,000 +Two problems, + +71 +00:02:32,000 --> 00:02:34,000 +the first one is what if you don't have + +72 +00:02:34,000 --> 00:02:36,000 +that much of amount of values to store? + +73 +00:02:36,000 --> 00:02:39,000 +Maybe you just have five to six values or 10 values + +74 +00:02:39,000 --> 00:02:41,000 +and the size of the array is 1,000. + +75 +00:02:41,000 --> 00:02:45,000 +Basically you are wasting the memory. + +76 +00:02:45,000 --> 00:02:46,000 +That's one issue. + +77 +00:02:46,000 --> 00:02:49,000 +The second issue is what if the value goes beyond 1,000? + +78 +00:02:49,000 --> 00:02:51,000 +Again, you have to create a new array. + +79 +00:02:51,000 --> 00:02:55,000 +So array is good in terms of searching, in terms of, + +80 +00:02:55,000 --> 00:02:56,000 +I mean fetching the value, + +81 +00:02:56,000 --> 00:03:00,000 +but it's not good in terms of you want to insert the value + +82 +00:03:00,000 --> 00:03:01,000 +and if you want to expand it. + +83 +00:03:01,000 --> 00:03:02,000 +Also there's one more problem + +84 +00:03:02,000 --> 00:03:03,000 +which I want to point out + +85 +00:03:03,000 --> 00:03:04,000 +is let's say if I want + +86 +00:03:04,000 --> 00:03:06,000 +to insert a value in between, + +87 +00:03:06,000 --> 00:03:09,000 +of course adding the value at the end is very easy. + +88 +00:03:09,000 --> 00:03:12,000 +Let's do that with this particular second array here. + +89 +00:03:12,000 --> 00:03:14,000 +So let's say, well let's work with this. + +90 +00:03:14,000 --> 00:03:16,000 +What if you want to insert a value somewhere here? + +91 +00:03:16,000 --> 00:03:18,000 +So I want to say five, eight + +92 +00:03:18,000 --> 00:03:20,000 +and maybe I want to insert two in between. + +93 +00:03:20,000 --> 00:03:22,000 +Now, when I insert a value two in between, + +94 +00:03:22,000 --> 00:03:25,000 +what we have to do is we have to move all this value + +95 +00:03:25,000 --> 00:03:26,000 +on the right hand side. + +96 +00:03:26,000 --> 00:03:27,000 +Again, a complex process + +97 +00:03:27,000 --> 00:03:30,000 +and also it'll take a lot of time. + +98 +00:03:30,000 --> 00:03:31,000 +So basically doing anything, + +99 +00:03:31,000 --> 00:03:33,000 +updating with the array is a headache. + +100 +00:03:33,000 --> 00:03:36,000 +Yes, if you add the value at the end, not a problem, + +101 +00:03:36,000 --> 00:03:38,000 +but if you want to add a value at the start + +102 +00:03:38,000 --> 00:03:41,000 +or in between, it creates issue. + +103 +00:03:41,000 --> 00:03:43,000 +And that's where we can look at something + +104 +00:03:43,000 --> 00:03:44,000 +called a linked list. + +105 +00:03:44,000 --> 00:03:46,000 +Now what we do in linked list + +106 +00:03:46,000 --> 00:03:48,000 +is we don't store the value in sequence. + +107 +00:03:48,000 --> 00:03:51,000 +It has a different data structure altogether. + +108 +00:03:51,000 --> 00:03:53,000 +So you don't store the values in sequence. + +109 +00:03:53,000 --> 00:03:54,000 +So how do we do it? + +110 +00:03:54,000 --> 00:03:56,000 +So what I will do is I will just use + +111 +00:03:56,000 --> 00:03:58,000 +another page here to talk about it. + +112 +00:03:58,000 --> 00:04:01,000 +So when you talk about a concept like linked list + +113 +00:04:01,000 --> 00:04:03,000 +in this, what you do is you do a simple thing. + +114 +00:04:03,000 --> 00:04:04,000 +If you want to store values, + +115 +00:04:04,000 --> 00:04:07,000 +let's say I want to store five, eight, one, three, + +116 +00:04:07,000 --> 00:04:09,000 +let's have this values here. + +117 +00:04:09,000 --> 00:04:10,000 +I want to store this values somewhere. + +118 +00:04:10,000 --> 00:04:12,000 +One way you can use array, + +119 +00:04:12,000 --> 00:04:13,000 +but we have talked about the problem with it. + +120 +00:04:13,000 --> 00:04:15,000 +So what we can do is we can store the values + +121 +00:04:15,000 --> 00:04:17,000 +in something called node. + +122 +00:04:17,000 --> 00:04:18,000 +So when you talk about linked list, + +123 +00:04:18,000 --> 00:04:20,000 +we have certain terms which have, + +124 +00:04:20,000 --> 00:04:23,000 +remember the terms are node, + +125 +00:04:23,000 --> 00:04:25,000 +the terms are reference, + +126 +00:04:25,000 --> 00:04:26,000 +or you can also say address + +127 +00:04:26,000 --> 00:04:29,000 +in some of the languages we call them as address. + +128 +00:04:29,000 --> 00:04:30,000 +Let's start with these two. + +129 +00:04:30,000 --> 00:04:34,000 +So basically what you do is you create the value. + +130 +00:04:34,000 --> 00:04:35,000 +So you store the values in a node. + +131 +00:04:35,000 --> 00:04:37,000 +So this is how the node looks like. + +132 +00:04:37,000 --> 00:04:38,000 +This will be a node, and in this node + +133 +00:04:38,000 --> 00:04:40,000 +you will store the value five, + +134 +00:04:40,000 --> 00:04:42,000 +you will take another node, + +135 +00:04:42,000 --> 00:04:44,000 +and in this way in this you will store eight, + +136 +00:04:44,000 --> 00:04:47,000 +you will take another node where you will store one, + +137 +00:04:47,000 --> 00:04:49,000 +you will take another node where you will store three. + +138 +00:04:49,000 --> 00:04:51,000 +So basically what we we're doing is + +139 +00:04:51,000 --> 00:04:53,000 +we are storing all the values in the nodes. + +140 +00:04:53,000 --> 00:04:55,000 +So these are nodes. + +141 +00:04:55,000 --> 00:04:56,000 +But the problem is + +142 +00:04:56,000 --> 00:04:57,000 +how will you maintain the sequence? + +143 +00:04:57,000 --> 00:04:59,000 +Because these are stored somewhere. + +144 +00:04:59,000 --> 00:05:01,000 +They're not a sequenced memory + +145 +00:05:01,000 --> 00:05:02,000 +because in array, what you do is + +146 +00:05:02,000 --> 00:05:04,000 +you use index values. + +147 +00:05:04,000 --> 00:05:06,000 +So example, if I name this array as N, + +148 +00:05:06,000 --> 00:05:09,000 +basically the address of N + +149 +00:05:09,000 --> 00:05:12,000 +represents the first value, which is five in this case. + +150 +00:05:12,000 --> 00:05:13,000 +Now, if you want the next value, + +151 +00:05:13,000 --> 00:05:16,000 +what you can do is you will take the address of N, + +152 +00:05:16,000 --> 00:05:17,000 +let's say 101. + +153 +00:05:17,000 --> 00:05:19,000 +If you want the first value, you can say, + +154 +00:05:19,000 --> 00:05:22,000 +"Okay, I want something which is at the address 101". + +155 +00:05:22,000 --> 00:05:24,000 +But if you want the next value, you can say, + +156 +00:05:24,000 --> 00:05:28,000 +"The next value will be at the address 101 plus one." + +157 +00:05:28,000 --> 00:05:31,000 +"The next value will be available at 101 plus two." + +158 +00:05:31,000 --> 00:05:33,000 +And that's why we have this index value, + +159 +00:05:33,000 --> 00:05:34,000 +something like this. + +160 +00:05:34,000 --> 00:05:36,000 +But in linked list we have, we don't have that scenario. + +161 +00:05:36,000 --> 00:05:39,000 +Of course, these nodes will be having some address to it. + +162 +00:05:39,000 --> 00:05:42,000 +Let's say the address for this is 105. + +163 +00:05:42,000 --> 00:05:45,000 +The address for this is 206, + +164 +00:05:45,000 --> 00:05:48,000 +and the address for this is 512. + +165 +00:05:48,000 --> 00:05:51,000 +This address for this is 68. + +166 +00:05:51,000 --> 00:05:53,000 +Now all these are addresses. + +167 +00:05:53,000 --> 00:05:56,000 +Nothing fancy, just simple addresses. + +168 +00:05:56,000 --> 00:05:58,000 +Now, if I want to fetch a value, let's say five, + +169 +00:05:58,000 --> 00:06:00,000 +then of course I should know the address of five, + +170 +00:06:00,000 --> 00:06:02,000 +which is 105. + +171 +00:06:02,000 --> 00:06:03,000 +If you know it, that's great. + +172 +00:06:03,000 --> 00:06:03,000 +You will get the value. + +173 +00:06:03,000 --> 00:06:06,000 +If you want the second value for linked list. + +174 +00:06:06,000 --> 00:06:09,000 +Now we have no idea what is a second value, + +175 +00:06:09,000 --> 00:06:11,000 +but if you know the address, you can find it. + +176 +00:06:11,000 --> 00:06:13,000 +But if you say, "Hey, I got a linked list, + +177 +00:06:13,000 --> 00:06:14,000 +I want the second value", there is a second value. + +178 +00:06:14,000 --> 00:06:17,000 +So what you have to do is if you want to chain them, + +179 +00:06:17,000 --> 00:06:21,000 +if you want to specify the sequence in a node, + +180 +00:06:21,000 --> 00:06:22,000 +basically you can have two things. + +181 +00:06:22,000 --> 00:06:25,000 +The first thing you'll have is data, + +182 +00:06:25,000 --> 00:06:27,000 +and the second thing you will have is reference. + +183 +00:06:27,000 --> 00:06:28,000 +Or maybe you can say address. + +184 +00:06:28,000 --> 00:06:32,000 +So here a node will have one more section. + +185 +00:06:32,000 --> 00:06:37,000 +Every node will have one extra section here, + +186 +00:06:37,000 --> 00:06:38,000 +let me draw it here. + +187 +00:06:38,000 --> 00:06:41,000 +And then what you'll be having in this section, + +188 +00:06:41,000 --> 00:06:42,000 +it's very simple. + +189 +00:06:42,000 --> 00:06:43,000 +In this section, + +190 +00:06:43,000 --> 00:06:44,000 +basically you will have the address + +191 +00:06:44,000 --> 00:06:47,000 +or the reference of the next node. + +192 +00:06:47,000 --> 00:06:48,000 +So for the first node, + +193 +00:06:48,000 --> 00:06:49,000 +when you talk about this node, + +194 +00:06:49,000 --> 00:06:51,000 +this node should have the reference + +195 +00:06:51,000 --> 00:06:52,000 +of this particular node. + +196 +00:06:52,000 --> 00:06:53,000 +Now, how will it do it? + +197 +00:06:53,000 --> 00:06:54,000 +It's very simple. + +198 +00:06:54,000 --> 00:06:56,000 +The second box in the node + +199 +00:06:56,000 --> 00:07:01,000 +will have the address, which is 206. + +200 +00:07:02,000 --> 00:07:03,000 +Now, by doing this, + +201 +00:07:03,000 --> 00:07:05,000 +what you're doing is you are creating + +202 +00:07:05,000 --> 00:07:07,000 +a logical linking between these two. + +203 +00:07:07,000 --> 00:07:09,000 +Then what about this next element? + +204 +00:07:09,000 --> 00:07:12,000 +So we are saying that this one here + +205 +00:07:12,000 --> 00:07:13,000 +is the third element. + +206 +00:07:13,000 --> 00:07:17,000 +So of course what you will do is you will say 512 here. + +207 +00:07:17,000 --> 00:07:20,000 +By doing this, you are creating a link here. + +208 +00:07:20,000 --> 00:07:21,000 +Again, what about the next value? + +209 +00:07:21,000 --> 00:07:23,000 +So of course it should be 68 + +210 +00:07:23,000 --> 00:07:25,000 +so that you can create a link here. + +211 +00:07:25,000 --> 00:07:27,000 +And what about the last value? + +212 +00:07:27,000 --> 00:07:28,000 +Nothing. + +213 +00:07:28,000 --> 00:07:29,000 +We don't have any elements after that. + +214 +00:07:29,000 --> 00:07:32,000 +So you will simply say null + +215 +00:07:32,000 --> 00:07:34,000 +because we don't have anything after it. + +216 +00:07:34,000 --> 00:07:36,000 +Now, this is your linked list. + +217 +00:07:36,000 --> 00:07:38,000 +Now why it is called a linked list? + +218 +00:07:38,000 --> 00:07:42,000 +Because we have a list of values and they are linked. + +219 +00:07:42,000 --> 00:07:45,000 +They're linked with the address or the reference. + +220 +00:07:45,000 --> 00:07:50,000 +So the first section here is called the data, + +221 +00:07:50,000 --> 00:07:51,000 +or you can say the info. + +222 +00:07:51,000 --> 00:07:55,000 +The second section here is basically called the reference. + +223 +00:07:55,000 --> 00:07:57,000 +And same goes for each node which you have. + +224 +00:07:59,000 --> 00:08:01,000 +That's how you can basically add the values. + +225 +00:08:01,000 --> 00:08:03,000 +Now you'll be saying, "Okay, what if I want + +226 +00:08:03,000 --> 00:08:05,000 +to insert a value in between?" + +227 +00:08:05,000 --> 00:08:08,000 +Or maybe let's add the value at the end first. + +228 +00:08:08,000 --> 00:08:11,000 +What if you want to insert a new value? + +229 +00:08:11,000 --> 00:08:12,000 +Very simple. + +230 +00:08:12,000 --> 00:08:13,000 +What you will do is the moment you say + +231 +00:08:13,000 --> 00:08:15,000 +you want to insert a new value, + +232 +00:08:15,000 --> 00:08:17,000 +every time you want to say insert new value, + +233 +00:08:17,000 --> 00:08:18,000 +you have to create a node. + +234 +00:08:18,000 --> 00:08:20,000 +A node will have two things as we know now. + +235 +00:08:20,000 --> 00:08:22,000 +The first thing, it will have the data. + +236 +00:08:22,000 --> 00:08:24,000 +Let's say the new value you want to insert + +237 +00:08:24,000 --> 00:08:27,000 +is maybe three or three is already there. + +238 +00:08:27,000 --> 00:08:28,000 +Let's say six. + +239 +00:08:28,000 --> 00:08:30,000 +You want to insert six, create a new node, + +240 +00:08:30,000 --> 00:08:32,000 +and in this node you will assign the value, + +241 +00:08:32,000 --> 00:08:33,000 +which is six, + +242 +00:08:33,000 --> 00:08:37,000 +and by default, the reference will be null. + +243 +00:08:37,000 --> 00:08:39,000 +But then we want to make sure + +244 +00:08:39,000 --> 00:08:42,000 +that this element is the last element in your list. + +245 +00:08:42,000 --> 00:08:44,000 +In that case, you have to make some changes. + +246 +00:08:44,000 --> 00:08:45,000 +Now, what are the changes? + +247 +00:08:45,000 --> 00:08:49,000 +Basically we have to create a link between these two. + +248 +00:08:49,000 --> 00:08:50,000 +How will you get the link? + +249 +00:08:50,000 --> 00:08:54,000 +The way you do that is by changing this value of null. + +250 +00:08:54,000 --> 00:08:58,000 +So you will simply remove this null from here + +251 +00:08:58,000 --> 00:09:01,000 +and change the value to the address. + +252 +00:09:01,000 --> 00:09:03,000 +Now, we don't have the address yet, + +253 +00:09:03,000 --> 00:09:06,000 +so let's say the address for this is 126. + +254 +00:09:06,000 --> 00:09:10,000 +So basically you will insert 126 here, + +255 +00:09:10,000 --> 00:09:11,000 +and that's how you can create a list + +256 +00:09:11,000 --> 00:09:13,000 +and you can append the values. + +257 +00:09:13,000 --> 00:09:15,000 +So the first value is this. + +258 +00:09:15,000 --> 00:09:16,000 +So this is your first value, + +259 +00:09:16,000 --> 00:09:18,000 +second value, third value, + +260 +00:09:18,000 --> 00:09:20,000 +fourth value, and fifth value. + +261 +00:09:20,000 --> 00:09:21,000 +Now, how do we know the sequences? + +262 +00:09:21,000 --> 00:09:24,000 +Because you start from the first element + +263 +00:09:24,000 --> 00:09:26,000 +and then you would jump to the next element, the next. + +264 +00:09:26,000 --> 00:09:28,000 +So basically you go from here to here, + +265 +00:09:28,000 --> 00:09:31,000 +third element, fourth element, and fifth element. + +266 +00:09:31,000 --> 00:09:32,000 +It's that simple. + +267 +00:09:32,000 --> 00:09:34,000 +But again, the question is how do you know + +268 +00:09:34,000 --> 00:09:35,000 +that this is a first element? + +269 +00:09:35,000 --> 00:09:38,000 +So basically we need to have one more concept here, + +270 +00:09:38,000 --> 00:09:42,000 +which is called the head. + +271 +00:09:42,000 --> 00:09:44,000 +So every linked list will have a head as well. + +272 +00:09:44,000 --> 00:09:47,000 +So head is just a reference which + +273 +00:09:47,000 --> 00:09:49,000 +is pointing to the first location. + +274 +00:09:49,000 --> 00:09:51,000 +So you'll be having an extra variable, + +275 +00:09:51,000 --> 00:09:54,000 +which will point to the first location. + +276 +00:09:54,000 --> 00:09:55,000 +It's that simple. + +277 +00:09:55,000 --> 00:09:58,000 +Now what if you want to insert more nodes? + +278 +00:09:58,000 --> 00:10:00,000 +But not at the end this time, + +279 +00:10:00,000 --> 00:10:01,000 +maybe you want to insert in between. + +280 +00:10:01,000 --> 00:10:03,000 +What you do in array + +281 +00:10:03,000 --> 00:10:05,000 +is you basically shift all the values. + +282 +00:10:05,000 --> 00:10:06,000 +Time consuming process, right? + +283 +00:10:06,000 --> 00:10:08,000 +So here what you do is, + +284 +00:10:08,000 --> 00:10:10,000 +again, when I say you want to insert + +285 +00:10:10,000 --> 00:10:13,000 +a new value in between, + +286 +00:10:13,000 --> 00:10:15,000 +or maybe let's say in between later, + +287 +00:10:15,000 --> 00:10:16,000 +you want to insert a new value, + +288 +00:10:16,000 --> 00:10:18,000 +you want to have a new node. + +289 +00:10:18,000 --> 00:10:21,000 +So what you basically do is you create a new node. + +290 +00:10:21,000 --> 00:10:22,000 +So maybe I want to insert the value + +291 +00:10:22,000 --> 00:10:25,000 +in between these two. + +292 +00:10:25,000 --> 00:10:28,000 +So after eight, I want to have another value, + +293 +00:10:28,000 --> 00:10:29,000 +or maybe I will just write the value here. + +294 +00:10:29,000 --> 00:10:33,000 +So let me just shift all this value on this side, + +295 +00:10:33,000 --> 00:10:34,000 +and the value I want to insert here + +296 +00:10:34,000 --> 00:10:36,000 +in between is let's say two. + +297 +00:10:36,000 --> 00:10:38,000 +So I want to insert two in between. + +298 +00:10:38,000 --> 00:10:40,000 +So between eight and one... + +299 +00:10:40,000 --> 00:10:41,000 +So the steps are very simple. + +300 +00:10:41,000 --> 00:10:44,000 +You create a new node, that's what we always do, + +301 +00:10:44,000 --> 00:10:48,000 +create new node and assign the value, which is two, + +302 +00:10:48,000 --> 00:10:50,000 +but we have no idea what should be the reference. + +303 +00:10:50,000 --> 00:10:53,000 +So by default, the reference will be null, as always. + +304 +00:10:53,000 --> 00:10:55,000 +But now we are saying that we got a new node + +305 +00:10:55,000 --> 00:10:58,000 +and this should be coming between eight and one. + +306 +00:10:58,000 --> 00:11:00,000 +So what you do is you make some changes. + +307 +00:11:00,000 --> 00:11:03,000 +The changes which you'll make is this. + +308 +00:11:03,000 --> 00:11:05,000 +First, you will remove this part + +309 +00:11:05,000 --> 00:11:07,000 +because this should be now referring + +310 +00:11:07,000 --> 00:11:10,000 +not to one value should refer + +311 +00:11:10,000 --> 00:11:11,000 +to this particular location, + +312 +00:11:11,000 --> 00:11:13,000 +and for that you have to change this address + +313 +00:11:13,000 --> 00:11:16,000 +from whatever value initially it was, maybe 512. + +314 +00:11:16,000 --> 00:11:18,000 +You have to change this to a new location. + +315 +00:11:18,000 --> 00:11:19,000 +So new address for this, + +316 +00:11:19,000 --> 00:11:22,000 +it should be having some address, let's say 136. + +317 +00:11:22,000 --> 00:11:23,000 +So now this is a new address, + +318 +00:11:23,000 --> 00:11:26,000 +so you will insert this address here. + +319 +00:11:26,000 --> 00:11:29,000 +Now by doing this, what we have also done is + +320 +00:11:29,000 --> 00:11:33,000 +now this particular arrow is not pointing here. + +321 +00:11:33,000 --> 00:11:34,000 +So this need to be removed. + +322 +00:11:34,000 --> 00:11:35,000 +So there's no more arrow here. + +323 +00:11:35,000 --> 00:11:38,000 +The arrow will be pointing to this location. + +324 +00:11:38,000 --> 00:11:40,000 +And then we have to also make sure + +325 +00:11:40,000 --> 00:11:43,000 +that these two, next location is this one. + +326 +00:11:43,000 --> 00:11:44,000 +So again, you'll make some changes. + +327 +00:11:44,000 --> 00:11:47,000 +You have to make this particular a link. + +328 +00:11:47,000 --> 00:11:49,000 +And to do that, the same step which have to follow, + +329 +00:11:49,000 --> 00:11:53,000 +change this address from this value to 512. + +330 +00:11:54,000 --> 00:11:57,000 +By doing this, we have added a new value + +331 +00:11:57,000 --> 00:12:01,000 +or new node in between the list. + +332 +00:12:01,000 --> 00:12:03,000 +So we have added new value, which is two. + +333 +00:12:03,000 --> 00:12:04,000 +It's that simple. + +334 +00:12:04,000 --> 00:12:05,000 +This is how basically linked list works. + +335 +00:12:05,000 --> 00:12:06,000 +Now, can we remove a value? + +336 +00:12:06,000 --> 00:12:08,000 +So I want to remove this one. + +337 +00:12:08,000 --> 00:12:09,000 +How do we remove one? + +338 +00:12:09,000 --> 00:12:11,000 +So you think about this, pause the video, + +339 +00:12:11,000 --> 00:12:13,000 +think about this, and then we can continue. + +340 +00:12:13,000 --> 00:12:14,000 +So I hope you have paused. + +341 +00:12:14,000 --> 00:12:16,000 +You have thought about this for some time. + +342 +00:12:16,000 --> 00:12:18,000 +Now let's understand. + +343 +00:12:18,000 --> 00:12:19,000 +Now, when I say you want to remove this one, + +344 +00:12:19,000 --> 00:12:20,000 +it's very simple. + +345 +00:12:20,000 --> 00:12:24,000 +The only change you have to make is this address. + +346 +00:12:24,000 --> 00:12:26,000 +If you can change that, your job is done. + +347 +00:12:26,000 --> 00:12:31,000 +So you can simply change this address from 512 to 68. + +348 +00:12:31,000 --> 00:12:32,000 +By doing this, + +349 +00:12:32,000 --> 00:12:36,000 +what we are doing is we are referring this here. + +350 +00:12:36,000 --> 00:12:37,000 +So basically we'll be referring this here. + +351 +00:12:37,000 --> 00:12:39,000 +So that is referred. + +352 +00:12:39,000 --> 00:12:40,000 +What happens to this one? + +353 +00:12:40,000 --> 00:12:41,000 +this will get garbage collected + +354 +00:12:41,000 --> 00:12:43,000 +if you are using a garbage values + +355 +00:12:43,000 --> 00:12:46,000 +because there's nothing which is referring it. + +356 +00:12:46,000 --> 00:12:48,000 +So now we are referring this. + +357 +00:12:48,000 --> 00:12:51,000 +That means we have to remove this arrow + +358 +00:12:51,000 --> 00:12:52,000 +and this arrow, this will be deleted. + +359 +00:12:52,000 --> 00:12:55,000 +There will be a link here, but it doesn't make sense. + +360 +00:12:55,000 --> 00:12:56,000 +It'll not be used. + +361 +00:12:56,000 --> 00:12:58,000 +So if you want, you can delete this reference also, + +362 +00:12:58,000 --> 00:13:01,000 +and in fact, this will be garbage selected. + +363 +00:13:01,000 --> 00:13:02,000 +So this value is gone. + +364 +00:13:02,000 --> 00:13:04,000 +Now, if you see the list, we only have + +365 +00:13:04,000 --> 00:13:07,000 +one, two, three, four, five values. + +366 +00:13:07,000 --> 00:13:08,000 +So this is how, basically, linked list works. + +367 +00:13:08,000 --> 00:13:11,000 +So now this type of linked list is called + +368 +00:13:11,000 --> 00:13:13,000 +the singly linked list. + +369 +00:13:13,000 --> 00:13:16,000 +Reason being, you can only go one way. + +370 +00:13:16,000 --> 00:13:17,000 +So if you know the first value, + +371 +00:13:17,000 --> 00:13:20,000 +which is this, so by using this, + +372 +00:13:20,000 --> 00:13:21,000 +you can go to the next value, + +373 +00:13:21,000 --> 00:13:22,000 +you can go to the next value, + +374 +00:13:22,000 --> 00:13:23,000 +you can go to the next value and next value. + +375 +00:13:23,000 --> 00:13:25,000 +But the question is, can you come back? + +376 +00:13:25,000 --> 00:13:26,000 +Let's say we are here, + +377 +00:13:26,000 --> 00:13:27,000 +it is easier to go forward + +378 +00:13:27,000 --> 00:13:28,000 +because you know the next value. + +379 +00:13:28,000 --> 00:13:30,000 +But can you go back? + +380 +00:13:30,000 --> 00:13:32,000 +Can you traverse in the reverse direction? + +381 +00:13:32,000 --> 00:13:34,000 +No, you can't do that. + +382 +00:13:34,000 --> 00:13:35,000 +And that's why if you want to achieve it, + +383 +00:13:35,000 --> 00:13:38,000 +we have to use something called a doubly linked list. + +384 +00:13:38,000 --> 00:13:40,000 +Now, what it means is doubly linked list + +385 +00:13:40,000 --> 00:13:42,000 +will not just have one address, + +386 +00:13:42,000 --> 00:13:43,000 +it'll have two addresses. + +387 +00:13:43,000 --> 00:13:46,000 +So every node will have two address. + +388 +00:13:46,000 --> 00:13:48,000 +So something like this. + +389 +00:13:48,000 --> 00:13:51,000 +So this will have one more address here. + +390 +00:13:51,000 --> 00:13:52,000 +This will have one more address. + +391 +00:13:52,000 --> 00:13:53,000 +This will have one more address. + +392 +00:13:53,000 --> 00:13:56,000 +This will have one more address and one more address. + +393 +00:13:56,000 --> 00:13:58,000 +Now, in this new address or reference, + +394 +00:13:58,000 --> 00:14:00,000 +what you will have is, + +395 +00:14:00,000 --> 00:14:03,000 +let me just remove this particular arrow from here. + +396 +00:14:03,000 --> 00:14:06,000 +So what you will have in the first one is null, + +397 +00:14:06,000 --> 00:14:08,000 +because you don't have anything before this. + +398 +00:14:08,000 --> 00:14:09,000 +So before five, we don't have anything. + +399 +00:14:09,000 --> 00:14:13,000 +In the eight, in this particular value, + +400 +00:14:13,000 --> 00:14:15,000 +you will have 206. + +401 +00:14:15,000 --> 00:14:17,000 +By doing this, what we are doing is we are specifying + +402 +00:14:17,000 --> 00:14:20,000 +that the previous value for this eight is five. + +403 +00:14:20,000 --> 00:14:21,000 +So that is in 206. + +404 +00:14:21,000 --> 00:14:24,000 +Now what you'll mention here is 105 + +405 +00:14:24,000 --> 00:14:28,000 +is because the previous value for this eight is five, + +406 +00:14:28,000 --> 00:14:30,000 +which is the address is 105. + +407 +00:14:30,000 --> 00:14:32,000 +Now what will happen here, this will have 206, + +408 +00:14:32,000 --> 00:14:34,000 +the previous address, what will have this, + +409 +00:14:34,000 --> 00:14:37,000 +it'll have 136, the previous address. + +410 +00:14:37,000 --> 00:14:39,000 +Now it doesn't matter where you are. + +411 +00:14:39,000 --> 00:14:42,000 +We have to make changes here as well, which is 68. + +412 +00:14:42,000 --> 00:14:44,000 +Now, it doesn't matter where you are, + +413 +00:14:44,000 --> 00:14:45,000 +you can actually trace back. + +414 +00:14:45,000 --> 00:14:47,000 +So if you want to go forward, + +415 +00:14:47,000 --> 00:14:50,000 +you will use this particular address for going forward. + +416 +00:14:50,000 --> 00:14:51,000 +If you want to come back, + +417 +00:14:51,000 --> 00:14:54,000 +you have to use this particular address. + +418 +00:14:55,000 --> 00:14:58,000 +And that's how basically you use linked list, + +419 +00:14:58,000 --> 00:15:00,000 +which you have singly linked list, doubly linked list, + +420 +00:15:00,000 --> 00:15:02,000 +and that's your traverse. + +421 +00:15:02,000 --> 00:15:03,000 +What if you want to print values? + +422 +00:15:03,000 --> 00:15:04,000 +It's very simple, + +423 +00:15:04,000 --> 00:15:05,000 +you traverse between the elements + +424 +00:15:05,000 --> 00:15:07,000 +and print the values. + +425 +00:15:07,000 --> 00:15:09,000 +How you're gonna do that in the code? + +426 +00:15:09,000 --> 00:15:11,000 +That we'll see in the next video. + diff --git a/28 - DSA (Optional)/020 Linkedlist Code For Adding Values_en.srt b/28 - DSA (Optional)/020 Linkedlist Code For Adding Values_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..7ca5c46ed7ac30709efe9f21786a5ddcef400ea2 --- /dev/null +++ b/28 - DSA (Optional)/020 Linkedlist Code For Adding Values_en.srt @@ -0,0 +1,1592 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: Now let's try to implement LinkedList using Java. + +2 +00:00:02,000 --> 00:00:05,000 +So what we'll do is, first of all, let's open the ID, + +3 +00:00:05,000 --> 00:00:07,000 +and you can see we have an ID here. + +4 +00:00:07,000 --> 00:00:09,000 +And then here let me create a file, + +5 +00:00:09,000 --> 00:00:13,000 +and we'll name this file as, let's say, Main.java. + +6 +00:00:13,000 --> 00:00:15,000 +This is where we'll write our Java code, + +7 +00:00:15,000 --> 00:00:17,000 +I mean the main code. + +8 +00:00:17,000 --> 00:00:19,000 +So let's have a main method here. + +9 +00:00:19,000 --> 00:00:21,000 +Now the thing is when we talk about LinkedList, + +10 +00:00:21,000 --> 00:00:24,000 +of course, we'll try to implement LinkedList by ourselves, + +11 +00:00:24,000 --> 00:00:27,000 +but then in Java, we have a in-built class + +12 +00:00:27,000 --> 00:00:29,000 +called LinkedList. + +13 +00:00:29,000 --> 00:00:32,000 +So if I say LinkedList of Java, which is in-built, + +14 +00:00:32,000 --> 00:00:34,000 +so let me just use this class. + +15 +00:00:34,000 --> 00:00:37,000 +You can see this class belongs to a package called util. + +16 +00:00:37,000 --> 00:00:41,000 +And if I jump to this class, there are lot of methods + +17 +00:00:41,000 --> 00:00:43,000 +inside this already present. + +18 +00:00:43,000 --> 00:00:45,000 +So if we can see, we got linkFirst, + +19 +00:00:45,000 --> 00:00:48,000 +which will give you the first element. + +20 +00:00:48,000 --> 00:00:50,000 +Then we got linkBefore, linkLast. + +21 +00:00:50,000 --> 00:00:52,000 +There are different methods available, + +22 +00:00:52,000 --> 00:00:53,000 +but if I want to explore it, + +23 +00:00:53,000 --> 00:00:56,000 +let me create object of LinkedList first. + +24 +00:00:56,000 --> 00:00:59,000 +And I will say the link list is nums. + +25 +00:00:59,000 --> 00:01:02,000 +So let's say we are creating a list of numbers here. + +26 +00:01:02,000 --> 00:01:05,000 +And then, of course, it is giving you a warning + +27 +00:01:05,000 --> 00:01:06,000 +because when you use collection, + +28 +00:01:06,000 --> 00:01:08,000 +basically it belongs to a package + +29 +00:01:08,000 --> 00:01:10,000 +called collections or a concept of collection. + +30 +00:01:10,000 --> 00:01:12,000 +And when you work with collection, + +31 +00:01:12,000 --> 00:01:13,000 +we have to also specify the type of elements + +32 +00:01:13,000 --> 00:01:15,000 +we are working with, but at this point, + +33 +00:01:15,000 --> 00:01:16,000 +let's keep it very simple. + +34 +00:01:16,000 --> 00:01:17,000 +Let's say numbers. + +35 +00:01:17,000 --> 00:01:19,000 +If you want remove this warning + +36 +00:01:19,000 --> 00:01:21,000 +and if you know collection API on Generics, + +37 +00:01:21,000 --> 00:01:22,000 +we can specify integer. + +38 +00:01:22,000 --> 00:01:23,000 +You can see that the warning is gone, + +39 +00:01:23,000 --> 00:01:26,000 +but just to simplify stuff, let's not do that. + +40 +00:01:26,000 --> 00:01:28,000 +Ignore this error or warning to this point. + +41 +00:01:28,000 --> 00:01:31,000 +Now, if I want to use this nums, + +42 +00:01:31,000 --> 00:01:32,000 +I can add some values here. + +43 +00:01:32,000 --> 00:01:35,000 +So let's say if I want to insert a value, + +44 +00:01:35,000 --> 00:01:36,000 +I can insert value, which is five. + +45 +00:01:36,000 --> 00:01:38,000 +So that's my first value. + +46 +00:01:38,000 --> 00:01:40,000 +Add one more element, let's say nine, + +47 +00:01:40,000 --> 00:01:44,000 +and all this, actually, we are creating the elements, + +48 +00:01:44,000 --> 00:01:47,000 +and then every element will have the reference as well. + +49 +00:01:47,000 --> 00:01:49,000 +So basically when you create five, + +50 +00:01:49,000 --> 00:01:51,000 +it will have a reference of nine as well. + +51 +00:01:51,000 --> 00:01:53,000 +Okay, can we print all the values here? + +52 +00:01:53,000 --> 00:01:55,000 +So if I say nums., I think it should print. + +53 +00:01:55,000 --> 00:01:57,000 +nums should print all the values. + +54 +00:01:57,000 --> 00:02:01,000 +Run, and you can see we got five and nine. + +55 +00:02:01,000 --> 00:02:03,000 +And the sequence also, you can see they're in sequence. + +56 +00:02:03,000 --> 00:02:05,000 +Basically, they are following the concept, + +57 +00:02:05,000 --> 00:02:06,000 +which we have done here. + +58 +00:02:06,000 --> 00:02:08,000 +So there's element, and there is also a reference. + +59 +00:02:08,000 --> 00:02:11,000 +So every element will have a data + +60 +00:02:11,000 --> 00:02:13,000 +and the reference as well for the next element. + +61 +00:02:13,000 --> 00:02:15,000 +Okay, do we have some other methods here? + +62 +00:02:15,000 --> 00:02:16,000 +Can I add addFirst? + +63 +00:02:16,000 --> 00:02:19,000 +You can see we have a method for that, which is addFirst, + +64 +00:02:19,000 --> 00:02:21,000 +and we can have an element, which is nine. + +65 +00:02:21,000 --> 00:02:24,000 +So now, or not nine, nine is already there, letter six. + +66 +00:02:24,000 --> 00:02:27,000 +So now what will happen is it will add five and nine, + +67 +00:02:27,000 --> 00:02:29,000 +and then we are adding six later. + +68 +00:02:29,000 --> 00:02:31,000 +But then the method which we are using is addFirst, + +69 +00:02:31,000 --> 00:02:34,000 +which means the six will go at the start. + +70 +00:02:34,000 --> 00:02:37,000 +Read on this, and you can see we got six, five, nine. + +71 +00:02:37,000 --> 00:02:42,000 +Other methods, we'll say nums. what else we can use. + +72 +00:02:43,000 --> 00:02:45,000 +We can also get an element with the help of index value. + +73 +00:02:45,000 --> 00:02:48,000 +Now when I say index two, it should return nine. + +74 +00:02:48,000 --> 00:02:51,000 +So in fact, I need to print this first. + +75 +00:02:51,000 --> 00:02:54,000 +So printing it here and run, + +76 +00:02:54,000 --> 00:02:55,000 +and you can see it is printing nine. + +77 +00:02:55,000 --> 00:02:58,000 +So that's element at the last, and it's index value two. + +78 +00:02:58,000 --> 00:03:02,000 +Now the funny part is LinkedList data structure, + +79 +00:03:02,000 --> 00:03:05,000 +which is an abstract data, does not work with index values. + +80 +00:03:05,000 --> 00:03:06,000 +Now even if you mention two here, + +81 +00:03:06,000 --> 00:03:08,000 +what is happening is it's basically tracking + +82 +00:03:08,000 --> 00:03:09,000 +between the elements. + +83 +00:03:09,000 --> 00:03:11,000 +And this can be time consuming, okay? + +84 +00:03:11,000 --> 00:03:13,000 +So when you talk about analyst or array, + +85 +00:03:13,000 --> 00:03:16,000 +they are fastest because they follow index numbers. + +86 +00:03:16,000 --> 00:03:17,000 +Here we don't have index value. + +87 +00:03:17,000 --> 00:03:19,000 +Basically we are jumping between the elements. + +88 +00:03:19,000 --> 00:03:20,000 +So it goes to head first. + +89 +00:03:20,000 --> 00:03:22,000 +Head is pointing to five, + +90 +00:03:22,000 --> 00:03:23,000 +Then it go... + +91 +00:03:23,000 --> 00:03:24,000 +Oh, sorry, head is pointing to six + +92 +00:03:24,000 --> 00:03:25,000 +because that's the first element, + +93 +00:03:25,000 --> 00:03:27,000 +and then five, then nine. + +94 +00:03:27,000 --> 00:03:29,000 +It finds nine, and it is printing nine. + +95 +00:03:29,000 --> 00:03:31,000 +Do we have a way to print the head? + +96 +00:03:31,000 --> 00:03:33,000 +So basically we have a method called peekFirst, + +97 +00:03:33,000 --> 00:03:36,000 +which should give you the head, + +98 +00:03:36,000 --> 00:03:37,000 +but when you say peek, + +99 +00:03:37,000 --> 00:03:40,000 +peak will give you the head element. + +100 +00:03:40,000 --> 00:03:43,000 +Okay, so we can also use this to get the head element. + +101 +00:03:43,000 --> 00:03:46,000 +Okay, so this is how the in-built LinkedList works. + +102 +00:03:46,000 --> 00:03:48,000 +But then we don't want to use in-build, + +103 +00:03:48,000 --> 00:03:51,000 +we want to do this by using our own code. + +104 +00:03:51,000 --> 00:03:52,000 +And I don't want to use this name, which is add. + +105 +00:03:52,000 --> 00:03:55,000 +Of course, we can actually use add and addFirst, + +106 +00:03:55,000 --> 00:03:56,000 +or maybe let's use this. + +107 +00:03:56,000 --> 00:03:59,000 +So what I will do is I will remove this package from here. + +108 +00:03:59,000 --> 00:04:01,000 +The moment you remove this package, + +109 +00:04:01,000 --> 00:04:03,000 +now we have no idea where this LinkedList is. + +110 +00:04:03,000 --> 00:04:07,000 +So for our Java code, this LinkedList does not exist. + +111 +00:04:07,000 --> 00:04:08,000 +So let's create one. + +112 +00:04:08,000 --> 00:04:10,000 +Of course, you can use the in-built one, + +113 +00:04:10,000 --> 00:04:12,000 +but let's create our own LinkedList. + +114 +00:04:12,000 --> 00:04:13,000 +So I'll click on More actions, + +115 +00:04:13,000 --> 00:04:16,000 +and I will create a class called LinkedList. + +116 +00:04:16,000 --> 00:04:18,000 +I want this to be in the same package. + +117 +00:04:18,000 --> 00:04:20,000 +So you can see LinkedList class is created now. + +118 +00:04:20,000 --> 00:04:22,000 +And if you try to see the code side by side, + +119 +00:04:22,000 --> 00:04:26,000 +so this is how the code looks like side by side, + +120 +00:04:26,000 --> 00:04:28,000 +or maybe I will also put this in the bottom. + +121 +00:04:28,000 --> 00:04:32,000 +So move this to left bottom. + +122 +00:04:32,000 --> 00:04:33,000 +So now you can see it is in the bottom. + +123 +00:04:33,000 --> 00:04:35,000 +So we can actually have the view + +124 +00:04:35,000 --> 00:04:37,000 +of those screens here to code. + +125 +00:04:37,000 --> 00:04:38,000 +So basically what we're doing + +126 +00:04:38,000 --> 00:04:39,000 +is we are calling LinkedList, right? + +127 +00:04:39,000 --> 00:04:41,000 +So class is created, and you can see there's no error + +128 +00:04:41,000 --> 00:04:45,000 +on this line now, but there is an error on this line. + +129 +00:04:45,000 --> 00:04:48,000 +So what we need is we basically need a method, + +130 +00:04:48,000 --> 00:04:52,000 +which will add, so I will say public void add, + +131 +00:04:52,000 --> 00:04:53,000 +which will take a number. + +132 +00:04:53,000 --> 00:04:55,000 +So I will say integer i, + +133 +00:04:55,000 --> 00:04:56,000 +and we are only working with numbers, + +134 +00:04:56,000 --> 00:04:57,000 +so let's stick to it. + +135 +00:04:57,000 --> 00:04:59,000 +And you can see there's no problem now. + +136 +00:04:59,000 --> 00:05:01,000 +So these two lines are clear, + +137 +00:05:01,000 --> 00:05:03,000 +but the question is, how will you add? + +138 +00:05:03,000 --> 00:05:06,000 +Now remember, every time you add an element, + +139 +00:05:06,000 --> 00:05:09,000 +basically we have to create a node, or you can say element, + +140 +00:05:09,000 --> 00:05:11,000 +but let's say let's be specific, node, + +141 +00:05:11,000 --> 00:05:13,000 +and we don't have a node yet. + +142 +00:05:13,000 --> 00:05:14,000 +So what I will do is I will create a node, + +143 +00:05:14,000 --> 00:05:16,000 +So I will say class Node. + +144 +00:05:16,000 --> 00:05:19,000 +That's a class I want, which represents two things. + +145 +00:05:19,000 --> 00:05:21,000 +So every node, as I mentioned before, + +146 +00:05:21,000 --> 00:05:23,000 +so this was the example for doubly LinkedList. + +147 +00:05:23,000 --> 00:05:24,000 +Let's create one more again. + +148 +00:05:24,000 --> 00:05:27,000 +So basically every time you create a node, + +149 +00:05:27,000 --> 00:05:29,000 +a node will have two things. + +150 +00:05:29,000 --> 00:05:31,000 +So this is your node. + +151 +00:05:31,000 --> 00:05:34,000 +Node will have two things, the value and the address. + +152 +00:05:34,000 --> 00:05:37,000 +Let's say the address for the next one is 216, + +153 +00:05:37,000 --> 00:05:39,000 +but at this point we don't know the address. + +154 +00:05:39,000 --> 00:05:42,000 +So initially, it will be null here. + +155 +00:05:42,000 --> 00:05:47,000 +So this is your data, and this is your reference, + +156 +00:05:47,000 --> 00:05:48,000 +or we can say address here. + +157 +00:05:48,000 --> 00:05:50,000 +So how do we do that in Java? + +158 +00:05:50,000 --> 00:05:51,000 +So of course, if you want to store data, + +159 +00:05:51,000 --> 00:05:54,000 +we can directly say data, but what about the reference? + +160 +00:05:54,000 --> 00:05:55,000 +How will you get the reference? + +161 +00:05:55,000 --> 00:05:58,000 +Now in Java, if you want to refer something, + +162 +00:05:58,000 --> 00:06:00,000 +we basically create a reference of it. + +163 +00:06:00,000 --> 00:06:01,000 +Now, reference of what? + +164 +00:06:01,000 --> 00:06:03,000 +The reference of the next node. + +165 +00:06:03,000 --> 00:06:05,000 +So whatever next node you are going to create, + +166 +00:06:05,000 --> 00:06:07,000 +of course, this is also a node, right? + +167 +00:06:07,000 --> 00:06:10,000 +So type of this particular element here is node. + +168 +00:06:10,000 --> 00:06:12,000 +So when you say you're getting a reference, + +169 +00:06:12,000 --> 00:06:13,000 +you have to get a reference of node, + +170 +00:06:13,000 --> 00:06:15,000 +and we'll say next, just name for it. + +171 +00:06:15,000 --> 00:06:19,000 +So what we are doing is instead of using a name reference, + +172 +00:06:19,000 --> 00:06:23,000 +we are going to say this as next. + +173 +00:06:23,000 --> 00:06:26,000 +Okay, so we got data, and we got next. + +174 +00:06:26,000 --> 00:06:28,000 +Okay, so there are two things. + +175 +00:06:28,000 --> 00:06:30,000 +And maybe just to simplify this mode, + +176 +00:06:30,000 --> 00:06:32,000 +every time I create a node, + +177 +00:06:32,000 --> 00:06:34,000 +maybe I want to assign data as well. + +178 +00:06:34,000 --> 00:06:37,000 +So maybe we can do that with the help of a constructor, + +179 +00:06:37,000 --> 00:06:37,000 +or maybe not. + +180 +00:06:37,000 --> 00:06:40,000 +Let's try that next time or after some time. + +181 +00:06:40,000 --> 00:06:41,000 +Now let's add a value. + +182 +00:06:41,000 --> 00:06:42,000 +How will you add the value? + +183 +00:06:42,000 --> 00:06:44,000 +Now what exactly adding value means? + +184 +00:06:44,000 --> 00:06:46,000 +And first of all, let me just change this to data itself. + +185 +00:06:46,000 --> 00:06:50,000 +It makes much more sense to use data as available name. + +186 +00:06:50,000 --> 00:06:54,000 +Now, what I will do is I will create a new node. + +187 +00:06:54,000 --> 00:06:56,000 +Basically every time you say you want to add element, + +188 +00:06:56,000 --> 00:06:58,000 +we have to create new node, right? + +189 +00:06:58,000 --> 00:07:00,000 +Example, if I say I want to add eight, + +190 +00:07:00,000 --> 00:07:03,000 +I have to create a new node basically, right? + +191 +00:07:03,000 --> 00:07:05,000 +So let's create a node here, + +192 +00:07:05,000 --> 00:07:07,000 +and I will call this as newNode = new Node. + +193 +00:07:09,000 --> 00:07:12,000 +Okay, so we're getting a new node here, + +194 +00:07:12,000 --> 00:07:14,000 +and I want to assign a value to it. + +195 +00:07:14,000 --> 00:07:19,000 +So of course, you can say newNode.data = data. + +196 +00:07:19,000 --> 00:07:22,000 +So whatever data you're receiving from here + +197 +00:07:22,000 --> 00:07:24,000 +is getting assigned to this new node data. + +198 +00:07:24,000 --> 00:07:27,000 +So data is there, but what about the next element? + +199 +00:07:27,000 --> 00:07:30,000 +So by default, the next element will be null. + +200 +00:07:30,000 --> 00:07:33,000 +So instead of specifying that two things here, + +201 +00:07:33,000 --> 00:07:35,000 +we can do one more thing, you know? + +202 +00:07:35,000 --> 00:07:38,000 +So we can create a constructor here for this node, + +203 +00:07:38,000 --> 00:07:43,000 +and this constructor will take the data + +204 +00:07:43,000 --> 00:07:47,000 +as the parameter, and then we can say this. + +205 +00:07:47,000 --> 00:07:50,000 +And I hope you know the concept of this now at this point. + +206 +00:07:50,000 --> 00:07:51,000 +So this.data = data, + +207 +00:07:52,000 --> 00:07:56,000 +and next = null by default. + +208 +00:07:56,000 --> 00:08:00,000 +So every time I create a node, the next will be null. + +209 +00:08:00,000 --> 00:08:02,000 +So by doing this, the advantage you will get + +210 +00:08:02,000 --> 00:08:04,000 +is you can directly pass your data here + +211 +00:08:04,000 --> 00:08:06,000 +so you don't have to write one more line. + +212 +00:08:06,000 --> 00:08:09,000 +Okay, so if you're thinking this is done, + +213 +00:08:09,000 --> 00:08:10,000 +yes, we got a new node. + +214 +00:08:10,000 --> 00:08:12,000 +So let's say let's keep this empty. + +215 +00:08:12,000 --> 00:08:15,000 +So, let me just clear the entire stuff from here. + +216 +00:08:15,000 --> 00:08:18,000 +Let's say the the entire LinkedList is empty. + +217 +00:08:18,000 --> 00:08:19,000 +Now when you say LinkedList, + +218 +00:08:19,000 --> 00:08:22,000 +of course, there should be a variable called header, right? + +219 +00:08:22,000 --> 00:08:25,000 +So every time you create a node, + +220 +00:08:25,000 --> 00:08:27,000 +and if this node is your first element, + +221 +00:08:27,000 --> 00:08:29,000 +then you have to make sure that your head + +222 +00:08:29,000 --> 00:08:31,000 +is pointing to that element, okay? + +223 +00:08:31,000 --> 00:08:34,000 +Also, what if this is not your first element? + +224 +00:08:34,000 --> 00:08:36,000 +In that case, you have to add. + +225 +00:08:36,000 --> 00:08:36,000 +You have to make sure + +226 +00:08:36,000 --> 00:08:40,000 +that this new node gets added at the end. + +227 +00:08:40,000 --> 00:08:43,000 +Okay, so when you're saying nums.add(5), + +228 +00:08:43,000 --> 00:08:45,000 +basically it is creating a new node, + +229 +00:08:45,000 --> 00:08:47,000 +but we are not pointing it to header. + +230 +00:08:47,000 --> 00:08:48,000 +How do you refer that header? + +231 +00:08:48,000 --> 00:08:49,000 +It's very simple. + +232 +00:08:49,000 --> 00:08:50,000 +You come back here, and don't you think + +233 +00:08:50,000 --> 00:08:52,000 +the head itself is a node? + +234 +00:08:52,000 --> 00:08:55,000 +So you will say Node head is equal... + +235 +00:08:55,000 --> 00:08:58,000 +I mean, Node head, which will, by default, equal to null + +236 +00:08:58,000 --> 00:08:59,000 +because it's not pointing to anything. + +237 +00:08:59,000 --> 00:09:02,000 +And now what you can do is now when you are adding five, + +238 +00:09:02,000 --> 00:09:04,000 +of course, it will create a new node + +239 +00:09:04,000 --> 00:09:06,000 +for you which will have two things. + +240 +00:09:06,000 --> 00:09:08,000 +It will be having a value, which is five. + +241 +00:09:08,000 --> 00:09:09,000 +That's what you're adding here. + +242 +00:09:09,000 --> 00:09:13,000 +And then the by default value for reference is null. + +243 +00:09:13,000 --> 00:09:15,000 +But we have to also make sure that your head + +244 +00:09:15,000 --> 00:09:17,000 +is referring to this particular node. + +245 +00:09:17,000 --> 00:09:18,000 +We're not doing that yet. + +246 +00:09:18,000 --> 00:09:20,000 +Head is still referring to null. + +247 +00:09:20,000 --> 00:09:23,000 +Now if you want to make sure that your head reference there, + +248 +00:09:23,000 --> 00:09:26,000 +what we can do is we can say head = newNode. + +249 +00:09:28,000 --> 00:09:29,000 +Okay, our job is done. + +250 +00:09:29,000 --> 00:09:31,000 +Now, your head is actually referring to newNode. + +251 +00:09:31,000 --> 00:09:34,000 +But what if here if you say line number nine, + +252 +00:09:34,000 --> 00:09:35,000 +what we are doing? + +253 +00:09:35,000 --> 00:09:37,000 +We are adding a new element, which is nine. + +254 +00:09:37,000 --> 00:09:41,000 +So that means we are creating a new node here, + +255 +00:09:41,000 --> 00:09:42,000 +which will be nine. + +256 +00:09:42,000 --> 00:09:45,000 +And by default the value for reference is null, + +257 +00:09:45,000 --> 00:09:48,000 +which next is null. + +258 +00:09:48,000 --> 00:09:49,000 +But we have to also make sure + +259 +00:09:49,000 --> 00:09:54,000 +that when you add nine, so five should refer here, + +260 +00:09:54,000 --> 00:09:57,000 +but looking at our code, even if you add nine, + +261 +00:09:57,000 --> 00:09:59,000 +even we are making nine as a header, + +262 +00:09:59,000 --> 00:10:01,000 +don't you think that is wrong? + +263 +00:10:01,000 --> 00:10:03,000 +Nine is not head, right? + +264 +00:10:03,000 --> 00:10:05,000 +So that means we need to do this + +265 +00:10:05,000 --> 00:10:08,000 +only when your head is null. + +266 +00:10:08,000 --> 00:10:09,000 +If your head is null, + +267 +00:10:09,000 --> 00:10:11,000 +then only you will write this kind of code. + +268 +00:10:11,000 --> 00:10:14,000 +Otherwise, what if your head is not null? + +269 +00:10:14,000 --> 00:10:19,000 +That means you have to make sure that your next element, + +270 +00:10:19,000 --> 00:10:22,000 +which is nine in this case, is the next part of five, + +271 +00:10:22,000 --> 00:10:23,000 +is the next element of five. + +272 +00:10:23,000 --> 00:10:25,000 +But how do you know which is the last element? + +273 +00:10:25,000 --> 00:10:28,000 +So in this case, five is the last element, + +274 +00:10:28,000 --> 00:10:30,000 +but we have no idea five is the last element. + +275 +00:10:30,000 --> 00:10:31,000 +How do we know in the code? + +276 +00:10:31,000 --> 00:10:34,000 +So what we can do here is we have to basically track. + +277 +00:10:34,000 --> 00:10:36,000 +We have to basically track from the first element + +278 +00:10:36,000 --> 00:10:37,000 +until the last element. + +279 +00:10:37,000 --> 00:10:39,000 +And when you say you want to track, + +280 +00:10:39,000 --> 00:10:41,000 +basically we need one more variable here, + +281 +00:10:41,000 --> 00:10:43,000 +which we call as current, which is referring to something. + +282 +00:10:43,000 --> 00:10:45,000 +That's what we are saying, by default is null, + +283 +00:10:45,000 --> 00:10:48,000 +or maybe by default we can say this is head, + +284 +00:10:48,000 --> 00:10:49,000 +but if the head is null... + +285 +00:10:49,000 --> 00:10:51,000 +Okay, so we can say by default is head. + +286 +00:10:51,000 --> 00:10:53,000 +So what we can do is we can traverse, + +287 +00:10:53,000 --> 00:10:55,000 +and the way you traverse is with the help of while loop. + +288 +00:10:55,000 --> 00:10:57,000 +So we can say while(current.next!=null). + +289 +00:11:00,000 --> 00:11:01,000 +So what we're doing is if current + +290 +00:11:01,000 --> 00:11:04,000 +is referring to head by default, + +291 +00:11:04,000 --> 00:11:06,000 +so current is here initially, + +292 +00:11:06,000 --> 00:11:08,000 +and we want to know which is the last element, so null... + +293 +00:11:08,000 --> 00:11:10,000 +So in this case, five is the last element, right? + +294 +00:11:10,000 --> 00:11:11,000 +So that's what we're checking. + +295 +00:11:11,000 --> 00:11:15,000 +If the current, next, which is here is not null, + +296 +00:11:15,000 --> 00:11:19,000 +so basically just to change the value for five, + +297 +00:11:19,000 --> 00:11:20,000 +the reference for five, what you can do + +298 +00:11:20,000 --> 00:11:24,000 +is you can say current.next = newNode. + +299 +00:11:26,000 --> 00:11:28,000 +The moment you do that, what we are doing + +300 +00:11:28,000 --> 00:11:30,000 +is the new node here is this nine, right? + +301 +00:11:30,000 --> 00:11:32,000 +Nine has the address, let's say, 121, + +302 +00:11:32,000 --> 00:11:35,000 +and five has the address, let's say, 101. + +303 +00:11:35,000 --> 00:11:37,000 +So what we are doing is we are replacing the value + +304 +00:11:37,000 --> 00:11:41,000 +of null here with 121. + +305 +00:11:42,000 --> 00:11:46,000 +That's what we are doing in else block here, okay? + +306 +00:11:46,000 --> 00:11:49,000 +So current.next, next is this particular value + +307 +00:11:49,000 --> 00:11:53,000 +or this particular box, and we are replacing that with 121. + +308 +00:11:53,000 --> 00:11:57,000 +But then what if you are adding one element here? + +309 +00:11:57,000 --> 00:11:59,000 +In the code, are we doing that? + +310 +00:11:59,000 --> 00:12:00,000 +No. + +311 +00:12:00,000 --> 00:12:02,000 +So let me add one element, not addFirst. + +312 +00:12:02,000 --> 00:12:04,000 +Let's say add(6). + +313 +00:12:04,000 --> 00:12:06,000 +So now, this is six. + +314 +00:12:06,000 --> 00:12:08,000 +By default, the value here will be null. + +315 +00:12:08,000 --> 00:12:11,000 +And let's say the address is 212. + +316 +00:12:11,000 --> 00:12:12,000 +Now we have to make sure + +317 +00:12:12,000 --> 00:12:16,000 +that the 212 address is specified here. + +318 +00:12:16,000 --> 00:12:16,000 +Now how will you do this? + +319 +00:12:16,000 --> 00:12:18,000 +How will you create this link? + +320 +00:12:18,000 --> 00:12:21,000 +So basically the code which we have returned, + +321 +00:12:21,000 --> 00:12:25,000 +by doing this code, every time you create a new node, + +322 +00:12:25,000 --> 00:12:27,000 +basically it will change the address of the first node. + +323 +00:12:27,000 --> 00:12:29,000 +We don't want to change that + +324 +00:12:29,000 --> 00:12:30,000 +because in the else block, that's what we are doing. + +325 +00:12:30,000 --> 00:12:33,000 +We are not traversing to the last element. + +326 +00:12:33,000 --> 00:12:35,000 +Now how do you traverse to the last element? + +327 +00:12:35,000 --> 00:12:38,000 +So to traverse, you will simply say + +328 +00:12:38,000 --> 00:12:39,000 +if (current.next != null), + +329 +00:12:44,000 --> 00:12:47,000 +you will simply change your current value. + +330 +00:12:47,000 --> 00:12:48,000 +Now do you change the current value? + +331 +00:12:48,000 --> 00:12:51,000 +So you'll say current.current.next. + +332 +00:12:51,000 --> 00:12:52,000 +By doing this, what we are doing + +333 +00:12:52,000 --> 00:12:55,000 +is initially current will be here, right? + +334 +00:12:55,000 --> 00:12:56,000 +And then we are saying this current + +335 +00:12:56,000 --> 00:12:59,000 +is referring to the head next. + +336 +00:12:59,000 --> 00:13:00,000 +That means it is referring here. + +337 +00:13:00,000 --> 00:13:02,000 +So this is what we are doing here. + +338 +00:13:02,000 --> 00:13:03,000 +So current = current.next. + +339 +00:13:03,000 --> 00:13:07,000 +So it will refer to the next element, okay? + +340 +00:13:07,000 --> 00:13:09,000 +And then, again, if this is not null, + +341 +00:13:09,000 --> 00:13:10,000 +it will go for the next one. + +342 +00:13:10,000 --> 00:13:11,000 +So that's how we are traversing. + +343 +00:13:11,000 --> 00:13:13,000 +And by doing this, basically we have done + +344 +00:13:13,000 --> 00:13:14,000 +with the adding part. + +345 +00:13:14,000 --> 00:13:16,000 +Let's see if this works. + +346 +00:13:16,000 --> 00:13:18,000 +Let me just rerun this. + +347 +00:13:18,000 --> 00:13:20,000 +Okay, we will get the other + +348 +00:13:20,000 --> 00:13:23,000 +because we have not done get and peek. + +349 +00:13:23,000 --> 00:13:27,000 +At this point, let's comment it and run this once again. + +350 +00:13:27,000 --> 00:13:29,000 +And you can see it is printing the address of LinkedList. + +351 +00:13:29,000 --> 00:13:32,000 +Okay, so the thing is LinkedList is our own class. + +352 +00:13:32,000 --> 00:13:35,000 +So instead of saying nums, printing nums, + +353 +00:13:35,000 --> 00:13:39,000 +what we should do is we say nums.printValues. + +354 +00:13:39,000 --> 00:13:41,000 +So basically, it should call printValues, + +355 +00:13:41,000 --> 00:13:42,000 +but how will you print? + +356 +00:13:42,000 --> 00:13:44,000 +So what we need is we need a method + +357 +00:13:45,000 --> 00:13:47,000 +which will print the values. + +358 +00:13:47,000 --> 00:13:51,000 +Okay, so it should print all the values of this list. + +359 +00:13:51,000 --> 00:13:52,000 +Now how do you print the values? + +360 +00:13:52,000 --> 00:13:53,000 +Now that's a tricky point. + +361 +00:13:53,000 --> 00:13:55,000 +Now when you say print, it's actually easy. + +362 +00:13:55,000 --> 00:13:58,000 +What you have to do is you have to start + +363 +00:13:58,000 --> 00:14:01,000 +from the first element, print the value, + +364 +00:14:01,000 --> 00:14:04,000 +then move to the next element, print the value, + +365 +00:14:04,000 --> 00:14:06,000 +move to the next element, and print the value. + +366 +00:14:06,000 --> 00:14:09,000 +That means you need a counter, okay, not counter + +367 +00:14:09,000 --> 00:14:11,000 +because this is not adding. + +368 +00:14:11,000 --> 00:14:14,000 +You basically need a reference of node, + +369 +00:14:14,000 --> 00:14:15,000 +and let's say, again, current + +370 +00:14:15,000 --> 00:14:16,000 +because at one point, you're referring + +371 +00:14:16,000 --> 00:14:17,000 +to only one node, right? + +372 +00:14:17,000 --> 00:14:20,000 +So let's say current, and current will start from head. + +373 +00:14:20,000 --> 00:14:22,000 +And the only thing you have to do is since we are jumping + +374 +00:14:22,000 --> 00:14:25,000 +between the elements multiple times, we'll use a while loop. + +375 +00:14:25,000 --> 00:14:29,000 +And we just have to verify if current is not equal to null, + +376 +00:14:29,000 --> 00:14:30,000 +then, of course, you have to print. + +377 +00:14:30,000 --> 00:14:33,000 +What if the LinkedList is empty? + +378 +00:14:33,000 --> 00:14:35,000 +In that case, the head will be null. + +379 +00:14:35,000 --> 00:14:36,000 +So we'll say while(current!=null), + +380 +00:14:37,000 --> 00:14:41,000 +you will simply print current.data. + +381 +00:14:41,000 --> 00:14:43,000 +And then you have to move your current. + +382 +00:14:43,000 --> 00:14:46,000 +So basically, once you print data of the first element, + +383 +00:14:46,000 --> 00:14:50,000 +which is this, then you will say next. + +384 +00:14:50,000 --> 00:14:51,000 +And we know how to do next now. + +385 +00:14:51,000 --> 00:14:56,000 +So I have to say current = to current.next, and that's it. + +386 +00:14:56,000 --> 00:14:57,000 +It will print all the values. + +387 +00:14:57,000 --> 00:15:00,000 +The only thing is it will print on new line. + +388 +00:15:00,000 --> 00:15:01,000 +I want to print on the same line + +389 +00:15:01,000 --> 00:15:03,000 +so I'll give a space here. + +390 +00:15:03,000 --> 00:15:06,000 +And once the while loop is over, + +391 +00:15:06,000 --> 00:15:09,000 +I'll print a new line, and let's run this. + +392 +00:15:09,000 --> 00:15:11,000 +And you can see we got 596, + +393 +00:15:11,000 --> 00:15:15,000 +and we got all the values in sequence. + +394 +00:15:15,000 --> 00:15:17,000 +So, yeah, this is how basically we use LinkedList + +395 +00:15:17,000 --> 00:15:21,000 +to add the values, but we have more things to do, right? + +396 +00:15:21,000 --> 00:15:23,000 +What if you want to add the value at the start? + +397 +00:15:23,000 --> 00:15:27,000 +What if you want to delete the value from between? + +398 +00:15:27,000 --> 00:15:29,000 +Now that we'll see in the upcoming video. + diff --git a/28 - DSA (Optional)/021 Linkedlist Addfirst And Delete Code Part 2_en.srt b/28 - DSA (Optional)/021 Linkedlist Addfirst And Delete Code Part 2_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..0a575410787c4e7ad3194ec795383f9bc3f2c6c6 --- /dev/null +++ b/28 - DSA (Optional)/021 Linkedlist Addfirst And Delete Code Part 2_en.srt @@ -0,0 +1,912 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: So till this point, we were able to add the elements + +2 +00:00:02,000 --> 00:00:04,000 +in the linked list, right? + +3 +00:00:04,000 --> 00:00:07,000 +So basically, what we did is, we have added three values, + +4 +00:00:07,000 --> 00:00:09,000 +if we can see here. + +5 +00:00:09,000 --> 00:00:10,000 +We got five, nine, six. + +6 +00:00:10,000 --> 00:00:13,000 +Those are the values we have added. + +7 +00:00:13,000 --> 00:00:15,000 +Let's not work with this, too, okay. + +8 +00:00:15,000 --> 00:00:17,000 +So you can see we have added this three values, + +9 +00:00:17,000 --> 00:00:18,000 +and then when you run this code, + +10 +00:00:18,000 --> 00:00:20,000 +this is the output which we were getting, right? + +11 +00:00:20,000 --> 00:00:24,000 +So we got five, nine, six, and it's working. + +12 +00:00:24,000 --> 00:00:26,000 +Now, basically, we have to do one more, + +13 +00:00:26,000 --> 00:00:28,000 +in fact, two more operations. + +14 +00:00:28,000 --> 00:00:31,000 +One is to add the element at the start, + +15 +00:00:31,000 --> 00:00:35,000 +and then the second one is basically to delete an element. + +16 +00:00:35,000 --> 00:00:38,000 +So whatever we have done in the practical is basically, + +17 +00:00:38,000 --> 00:00:40,000 +we have created three nodes. + +18 +00:00:40,000 --> 00:00:42,000 +So when the first node was created, + +19 +00:00:42,000 --> 00:00:45,000 +so this was basically your first node, + +20 +00:00:45,000 --> 00:00:46,000 +which has two section, the data, + +21 +00:00:46,000 --> 00:00:48,000 +of course, the data is five here, + +22 +00:00:48,000 --> 00:00:51,000 +and of course, there's some value here, + +23 +00:00:51,000 --> 00:00:54,000 +some reference, which will, by default, null, + +24 +00:00:54,000 --> 00:00:55,000 +but we'll change this later, + +25 +00:00:55,000 --> 00:01:00,000 +and this will also have the address, let's say this is 121. + +26 +00:01:00,000 --> 00:01:02,000 +And then we got another element here, + +27 +00:01:02,000 --> 00:01:06,000 +so this is another element in a box, + +28 +00:01:06,000 --> 00:01:09,000 +and let's say the value we have is nine, + +29 +00:01:09,000 --> 00:01:11,000 +and this will be null by default, + +30 +00:01:11,000 --> 00:01:13,000 +and then we have one more element here, + +31 +00:01:13,000 --> 00:01:15,000 +so let me just draw that here, + +32 +00:01:15,000 --> 00:01:19,000 +and then the value we got is six, and it will be null. + +33 +00:01:19,000 --> 00:01:20,000 +And everything will be having an address. + +34 +00:01:20,000 --> 00:01:23,000 +Let's say this is 301, this is 101, + +35 +00:01:23,000 --> 00:01:25,000 +but what we are doing is, since they are in a link, + +36 +00:01:25,000 --> 00:01:28,000 +and with the help of this code which is add, + +37 +00:01:28,000 --> 00:01:31,000 +we have also made sure that, every time you add the element, + +38 +00:01:31,000 --> 00:01:34,000 +you basically get the address of the new element + +39 +00:01:34,000 --> 00:01:35,000 +and add in to the previous one, right? + +40 +00:01:35,000 --> 00:01:38,000 +So basically, the value which was null here + +41 +00:01:38,000 --> 00:01:42,000 +will be replaced by the new value, which is 301, + +42 +00:01:42,000 --> 00:01:45,000 +which is referring this, and then this will refer here. + +43 +00:01:45,000 --> 00:01:49,000 +The way it will do that is by removing this null, + +44 +00:01:49,000 --> 00:01:51,000 +and this will have 101. + +45 +00:01:51,000 --> 00:01:52,000 +This is what we have done, right? + +46 +00:01:52,000 --> 00:01:53,000 +The last element will be null + +47 +00:01:53,000 --> 00:01:56,000 +because we don't have an element after that. + +48 +00:01:56,000 --> 00:01:59,000 +Now, we also have one more variable, which is your head, + +49 +00:01:59,000 --> 00:02:02,000 +and head is basically pointing to the first element, right? + +50 +00:02:02,000 --> 00:02:05,000 +That's what we have done when we created this linked list. + +51 +00:02:05,000 --> 00:02:06,000 +So every time you add the value, + +52 +00:02:06,000 --> 00:02:08,000 +it will check if the head is null, + +53 +00:02:08,000 --> 00:02:10,000 +it will assign the new node there, + +54 +00:02:10,000 --> 00:02:12,000 +but for other elements, it will simply add the element + +55 +00:02:12,000 --> 00:02:15,000 +by tracing where this is the last element. + +56 +00:02:15,000 --> 00:02:18,000 +Now what I will do is, I want to also have a function here. + +57 +00:02:18,000 --> 00:02:22,000 +Let's say if I say nums.add, I want to say add first + +58 +00:02:22,000 --> 00:02:24,000 +so that it will add at the start, + +59 +00:02:24,000 --> 00:02:26,000 +so the element I want to add here is, let's say, seven. + +60 +00:02:26,000 --> 00:02:29,000 +So I want the seven to be the first element here. + +61 +00:02:29,000 --> 00:02:30,000 +Now, how do we do that? + +62 +00:02:30,000 --> 00:02:31,000 +First of all, we don't have this method, + +63 +00:02:31,000 --> 00:02:33,000 +so what I can do is I can just click here. + +64 +00:02:33,000 --> 00:02:36,000 +Now, based on which ID you use, you will get this option. + +65 +00:02:36,000 --> 00:02:39,000 +So create a method, and that's the method we have, + +66 +00:02:39,000 --> 00:02:41,000 +so this is responsible to add the element, + +67 +00:02:41,000 --> 00:02:44,000 +and let's say this is not I, this is data. + +68 +00:02:44,000 --> 00:02:46,000 +Now, how do you add the element at the first? + +69 +00:02:46,000 --> 00:02:47,000 +Now, this is tricky. + +70 +00:02:47,000 --> 00:02:50,000 +So when I say I want to add the element at the first, + +71 +00:02:50,000 --> 00:02:52,000 +the first thing you do is you need a node, + +72 +00:02:52,000 --> 00:02:53,000 +so we have to create a node, + +73 +00:02:53,000 --> 00:02:54,000 +and this node will have two things. + +74 +00:02:54,000 --> 00:02:56,000 +The data which we are adding is seven, + +75 +00:02:56,000 --> 00:03:01,000 +so that's pretty clear, but what about the reference, + +76 +00:03:01,000 --> 00:03:04,000 +which will be null, which is the next value, basically? + +77 +00:03:04,000 --> 00:03:06,000 +And then of course, this will also have some address. + +78 +00:03:06,000 --> 00:03:09,000 +Let's say this is 206, that's the address we have. + +79 +00:03:09,000 --> 00:03:12,000 +Now, when you say, this is your first element, + +80 +00:03:12,000 --> 00:03:13,000 +don't you think the only thing you have to do + +81 +00:03:13,000 --> 00:03:16,000 +is first create the node and just shift your head here? + +82 +00:03:16,000 --> 00:03:19,000 +So instead of having your head here, let's shift it. + +83 +00:03:19,000 --> 00:03:23,000 +So the way you do that is, first you create a node, + +84 +00:03:23,000 --> 00:03:25,000 +so I would say new node. + +85 +00:03:25,000 --> 00:03:27,000 +In fact, you can just reuse the code, right? + +86 +00:03:27,000 --> 00:03:29,000 +So whenever you add the element, + +87 +00:03:29,000 --> 00:03:31,000 +this is something you will be doing for sure. + +88 +00:03:31,000 --> 00:03:34,000 +So not cut, I don't want to cut that, + +89 +00:03:34,000 --> 00:03:37,000 +so let's reuse the code, so copy paste. + +90 +00:03:37,000 --> 00:03:41,000 +Now, that's done, so we have created the new node. + +91 +00:03:41,000 --> 00:03:42,000 +The next thing you have to do is, + +92 +00:03:42,000 --> 00:03:43,000 +you have to change your head. + +93 +00:03:43,000 --> 00:03:46,000 +I mean, not your head, the head from the linked list. + +94 +00:03:46,000 --> 00:03:47,000 +Now, there's one problem. + +95 +00:03:47,000 --> 00:03:50,000 +If you directly change your head here, + +96 +00:03:50,000 --> 00:03:54,000 +we also want to know that if this is the first element, + +97 +00:03:54,000 --> 00:03:57,000 +then if you move your head here, + +98 +00:03:57,000 --> 00:03:59,000 +this element should be the second element, right? + +99 +00:03:59,000 --> 00:04:01,000 +So we have to make this our second element. + +100 +00:04:01,000 --> 00:04:04,000 +That also means that this should know the first element. + +101 +00:04:04,000 --> 00:04:06,000 +So basically, we are moving our head here, + +102 +00:04:06,000 --> 00:04:07,000 +and then we have to refer this. + +103 +00:04:07,000 --> 00:04:09,000 +Okay, so how do we do this? + +104 +00:04:09,000 --> 00:04:11,000 +So one thing you can do here is, + +105 +00:04:11,000 --> 00:04:15,000 +so this newNode.next will be referring to the first element, + +106 +00:04:15,000 --> 00:04:17,000 +but how do we know the first element? + +107 +00:04:17,000 --> 00:04:18,000 +The first element is actually referring + +108 +00:04:18,000 --> 00:04:20,000 +to the head as well, right? + +109 +00:04:20,000 --> 00:04:22,000 +So if you want to create this link here, + +110 +00:04:22,000 --> 00:04:25,000 +basically, we have to get this address, which is 102, + +111 +00:04:25,000 --> 00:04:26,000 +and that's what we are doing here, + +112 +00:04:26,000 --> 00:04:29,000 +so we are saying newNode.next is equal to head. + +113 +00:04:29,000 --> 00:04:30,000 +That's where head was pointing, + +114 +00:04:30,000 --> 00:04:33,000 +and now, once you got it, you just have to shift your head, + +115 +00:04:33,000 --> 00:04:35,000 +so you have to say head is equal to new node, and that's it. + +116 +00:04:35,000 --> 00:04:38,000 +We are just shifting the things here. + +117 +00:04:38,000 --> 00:04:39,000 +So head is referring here, + +118 +00:04:39,000 --> 00:04:40,000 +and this is referring to the first element, + +119 +00:04:40,000 --> 00:04:42,000 +which was previous element, + +120 +00:04:42,000 --> 00:04:45,000 +and now we got a new element which is at the start. + +121 +00:04:45,000 --> 00:04:48,000 +Let's see if this works, and I will just run this code. + +122 +00:04:48,000 --> 00:04:50,000 +We should see seven at the start, + +123 +00:04:50,000 --> 00:04:52,000 +and you can see we got seven at the start. + +124 +00:04:52,000 --> 00:04:54,000 +So adding the element at the start is done. + +125 +00:04:54,000 --> 00:04:56,000 +What's next? Maybe I want to delete an element. + +126 +00:04:56,000 --> 00:05:01,000 +So here, let's say I want to delete, nums.delete. + +127 +00:05:01,000 --> 00:05:03,000 +I want to delete, let's say, nine, okay? + +128 +00:05:03,000 --> 00:05:04,000 +This is the element I want to delete. + +129 +00:05:04,000 --> 00:05:06,000 +How do we delete nine? + +130 +00:05:06,000 --> 00:05:08,000 +So basically, we need a method, first of all, + +131 +00:05:08,000 --> 00:05:11,000 +which will do this, so I will say create delete method, + +132 +00:05:11,000 --> 00:05:14,000 +and in this, I want to delete a particular data. + +133 +00:05:14,000 --> 00:05:17,000 +Now, first of all, we don't have any idea + +134 +00:05:17,000 --> 00:05:18,000 +that where is the element, + +135 +00:05:18,000 --> 00:05:21,000 +because this element can be anywhere. + +136 +00:05:21,000 --> 00:05:23,000 +So basically, we have to travel + +137 +00:05:23,000 --> 00:05:25,000 +to that particular node, right? + +138 +00:05:25,000 --> 00:05:27,000 +And we have to start from head, + +139 +00:05:27,000 --> 00:05:29,000 +so basically, we'll start searching for head. + +140 +00:05:29,000 --> 00:05:30,000 +So we'll go here. + +141 +00:05:30,000 --> 00:05:33,000 +Is it nine? No. Is it nine? No. Is it nine? Yes. + +142 +00:05:33,000 --> 00:05:36,000 +So once we get nine, we'll delete it. + +143 +00:05:36,000 --> 00:05:38,000 +Now, when you say you want to delete, + +144 +00:05:38,000 --> 00:05:39,000 +the only thing you have to do is, + +145 +00:05:39,000 --> 00:05:41,000 +okay, first of all, let's go to that nine, okay? + +146 +00:05:41,000 --> 00:05:43,000 +So the way you can go to the nine, + +147 +00:05:43,000 --> 00:05:45,000 +first, you have to take a reference. + +148 +00:05:45,000 --> 00:05:48,000 +Let's say current, and current will start with head, + +149 +00:05:48,000 --> 00:05:50,000 +so that's how you're starting now from head. + +150 +00:05:50,000 --> 00:05:52,000 +So how do we basically travel between the nodes? + +151 +00:05:52,000 --> 00:05:54,000 +So we can use a while loop because we wanna travel, + +152 +00:05:54,000 --> 00:05:57,000 +and we can simply say current.next, + +153 +00:05:57,000 --> 00:06:00,000 +We have to verify, is the next element empty? + +154 +00:06:00,000 --> 00:06:02,000 +Because of course, you have to stop, right? + +155 +00:06:02,000 --> 00:06:03,000 +We have to stop somewhere, + +156 +00:06:03,000 --> 00:06:05,000 +so the moment you get null, we'll stop it. + +157 +00:06:05,000 --> 00:06:06,000 +Otherwise, we'll continue. + +158 +00:06:06,000 --> 00:06:08,000 +And also, we have to check one more condition, + +159 +00:06:08,000 --> 00:06:12,000 +that the data which you are searching for is not there, + +160 +00:06:12,000 --> 00:06:14,000 +because once you get the data, you will stop. + +161 +00:06:14,000 --> 00:06:15,000 +But when I have to stop? + +162 +00:06:15,000 --> 00:06:18,000 +So basically, the moment I go here, then... + +163 +00:06:18,000 --> 00:06:20,000 +So initially, we are at the head, + +164 +00:06:20,000 --> 00:06:24,000 +so we'll check, is this this element we are searching for? + +165 +00:06:24,000 --> 00:06:28,000 +If no, then we'll shift here and we'll check for this data. + +166 +00:06:28,000 --> 00:06:30,000 +When we reach here, we'll check for this data. + +167 +00:06:30,000 --> 00:06:32,000 +That means, when you are here, + +168 +00:06:32,000 --> 00:06:33,000 +then you are checking the database nine, + +169 +00:06:33,000 --> 00:06:35,000 +but when you are here, + +170 +00:06:35,000 --> 00:06:38,000 +how will you check the data of the next node? + +171 +00:06:38,000 --> 00:06:42,000 +So we can say current.next.data, right? + +172 +00:06:42,000 --> 00:06:45,000 +So currently, we are at five, + +173 +00:06:45,000 --> 00:06:47,000 +and then we are checking for the next element data, + +174 +00:06:47,000 --> 00:06:51,000 +so the way you can do that is by saying current.next.data + +175 +00:06:51,000 --> 00:06:54,000 +should not be equal to data which you're searching for, + +176 +00:06:54,000 --> 00:06:56,000 +because if it is equal, then you have to stop. + +177 +00:06:56,000 --> 00:06:57,000 +That's where you're stopping. + +178 +00:06:57,000 --> 00:06:59,000 +But if it is not there, then you have to keep going. + +179 +00:06:59,000 --> 00:07:01,000 +The way you keep going + +180 +00:07:01,000 --> 00:07:03,000 +is by saying current is equal to current.next. + +181 +00:07:03,000 --> 00:07:06,000 +So what we are doing in this step is, + +182 +00:07:06,000 --> 00:07:08,000 +basically, we are shifting. + +183 +00:07:08,000 --> 00:07:10,000 +So if you are here, then we are shifting here. + +184 +00:07:10,000 --> 00:07:11,000 +How do I shifting here? + +185 +00:07:11,000 --> 00:07:14,000 +So we have to saying current is equal to current.next, + +186 +00:07:14,000 --> 00:07:16,000 +which is this element. + +187 +00:07:16,000 --> 00:07:17,000 +Oh, I forgot to change this data here, + +188 +00:07:17,000 --> 00:07:19,000 +so this should not be null. + +189 +00:07:19,000 --> 00:07:23,000 +This should be 121, right? Yeah. + +190 +00:07:23,000 --> 00:07:26,000 +So let's say we have reached here. + +191 +00:07:26,000 --> 00:07:27,000 +Now, once you reach here, + +192 +00:07:27,000 --> 00:07:29,000 +and you know that the next element is nine, + +193 +00:07:29,000 --> 00:07:30,000 +what is a step you will do? + +194 +00:07:30,000 --> 00:07:32,000 +So the only step which you have to do is, + +195 +00:07:32,000 --> 00:07:33,000 +if you want to delete this, + +196 +00:07:33,000 --> 00:07:36,000 +what if you can simply change your next element + +197 +00:07:36,000 --> 00:07:37,000 +to this element? + +198 +00:07:37,000 --> 00:07:39,000 +Don't you think nine will be removed? + +199 +00:07:39,000 --> 00:07:40,000 +So how do we do that? + +200 +00:07:40,000 --> 00:07:41,000 +So first of all, we have to check + +201 +00:07:41,000 --> 00:07:45,000 +if current.next is not equal to null. + +202 +00:07:45,000 --> 00:07:47,000 +You have to make sure that the next one is not null, + +203 +00:07:47,000 --> 00:07:49,000 +and then you can perform the operation. + +204 +00:07:49,000 --> 00:07:52,000 +So the only thing you have to do is, if you are here. + +205 +00:07:52,000 --> 00:07:55,000 +so this.next should not be this element. + +206 +00:07:55,000 --> 00:07:57,000 +This.next should be this element. + +207 +00:07:57,000 --> 00:07:58,000 +How do we do that? + +208 +00:07:58,000 --> 00:08:03,000 +So to say current.next is equal to current.next.next, + +209 +00:08:06,000 --> 00:08:09,000 +because current.next will give you, so if we are here, + +210 +00:08:09,000 --> 00:08:10,000 +current.next will give you this. + +211 +00:08:10,000 --> 00:08:12,000 +I mean, this value will be referring to this element. + +212 +00:08:12,000 --> 00:08:16,000 +We just have to say this value should be 101. + +213 +00:08:16,000 --> 00:08:19,000 +So the only thing you have to do is change this value + +214 +00:08:19,000 --> 00:08:22,000 +from 301 to 101, and the way you're doing that + +215 +00:08:22,000 --> 00:08:25,000 +is with the help of this line, and that's deleted. + +216 +00:08:25,000 --> 00:08:26,000 +Okay, let's see if this works. + +217 +00:08:26,000 --> 00:08:29,000 +Let's run this, and you can see nine gone. + +218 +00:08:29,000 --> 00:08:30,000 +Okay, this is that simple. + +219 +00:08:30,000 --> 00:08:32,000 +It's just traveling to that point + +220 +00:08:32,000 --> 00:08:35,000 +and changing the node of the previous one, + +221 +00:08:35,000 --> 00:08:38,000 +because the moment you change this value, you're done. + +222 +00:08:38,000 --> 00:08:39,000 +You don't have to actually delete the value + +223 +00:08:39,000 --> 00:08:40,000 +or delete the node. + +224 +00:08:40,000 --> 00:08:43,000 +Because now we don't have any reference for this one, + +225 +00:08:43,000 --> 00:08:45,000 +this will be garbage collected. + +226 +00:08:45,000 --> 00:08:47,000 +Especially in Java, we we do garbage collection. + +227 +00:08:47,000 --> 00:08:49,000 +So yeah, that's about linked list where we were able + +228 +00:08:49,000 --> 00:08:52,000 +to add element at the end, at the start, and also delete. + diff --git a/28 - DSA (Optional)/022 Stack Theory_en.srt b/28 - DSA (Optional)/022 Stack Theory_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..358cfaf319bac9edf2f219237a752597523d21cf --- /dev/null +++ b/28 - DSA (Optional)/022 Stack Theory_en.srt @@ -0,0 +1,592 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: In this video we'll talk about Stack. + +2 +00:00:02,000 --> 00:00:05,000 +Now, Stack is basically a Linear Data Structure, + +3 +00:00:05,000 --> 00:00:07,000 +so we can use it to store data. + +4 +00:00:07,000 --> 00:00:09,000 +Now the question arise, "Why it is different." + +5 +00:00:09,000 --> 00:00:11,000 +The thing is Stack follows something called, + +6 +00:00:11,000 --> 00:00:14,000 +Last IN, First OUT. + +7 +00:00:14,000 --> 00:00:15,000 +So as you have seen, basically we were able + +8 +00:00:15,000 --> 00:00:18,000 +to Stack data on top of other. + +9 +00:00:18,000 --> 00:00:19,000 +So here we were using devices, + +10 +00:00:19,000 --> 00:00:22,000 +but we can also store data in the same format. + +11 +00:00:22,000 --> 00:00:24,000 +So basically, what happens is you can create a Stack, + +12 +00:00:24,000 --> 00:00:26,000 +but then Stack looks something like this. + +13 +00:00:26,000 --> 00:00:29,000 +Basically, you have this type of memory, + +14 +00:00:29,000 --> 00:00:32,000 +where you only have one entry point + +15 +00:00:32,000 --> 00:00:35,000 +and the same entry point is also an exit point. + +16 +00:00:35,000 --> 00:00:38,000 +So let's say if you have some list of values + +17 +00:00:38,000 --> 00:00:39,000 +which you want to store. + +18 +00:00:39,000 --> 00:00:42,000 +So maybe I want to store five, eight, six, three. + +19 +00:00:42,000 --> 00:00:44,000 +So I want to store these four values. + +20 +00:00:44,000 --> 00:00:47,000 +Now what happens is when you want to store this five, + +21 +00:00:47,000 --> 00:00:50,000 +this five basically goes into the Stack, + +22 +00:00:50,000 --> 00:00:52,000 +but the question is where it will be stored. + +23 +00:00:52,000 --> 00:00:55,000 +So it will be stored somewhere here. + +24 +00:00:55,000 --> 00:00:57,000 +So five will be here, okay? + +25 +00:00:57,000 --> 00:00:59,000 +And now once you store the five, + +26 +00:00:59,000 --> 00:01:02,000 +now you want to store a eight. + +27 +00:01:02,000 --> 00:01:04,000 +It goes on top of five. + +28 +00:01:04,000 --> 00:01:05,000 +So eight goes here. + +29 +00:01:05,000 --> 00:01:08,000 +Now what happens here is it follows, + +30 +00:01:08,000 --> 00:01:09,000 +as I mentioned, it follows LIFO, + +31 +00:01:09,000 --> 00:01:11,000 +which is Last IN, First OUT. + +32 +00:01:11,000 --> 00:01:14,000 +Basically, the last element which have entered + +33 +00:01:14,000 --> 00:01:17,000 +will be the first element which will come out, + +34 +00:01:17,000 --> 00:01:18,000 +just like stack of books + +35 +00:01:18,000 --> 00:01:20,000 +or stack of devices or stack of plates. + +36 +00:01:20,000 --> 00:01:24,000 +So the last thing which went inside, which is eight, + +37 +00:01:24,000 --> 00:01:26,000 +is the first thing to come out, okay? + +38 +00:01:26,000 --> 00:01:28,000 +So we use a concept called, + +39 +00:01:28,000 --> 00:01:30,000 +Push and Pop. + +40 +00:01:30,000 --> 00:01:33,000 +So when you want to insert data, we use Push. + +41 +00:01:33,000 --> 00:01:36,000 +When you want to get data out, we use Pop. + +42 +00:01:36,000 --> 00:01:39,000 +So at this point, if I say, "Hey, this is my Stack," + +43 +00:01:39,000 --> 00:01:41,000 +let's say Stack is NUMs. + +44 +00:01:41,000 --> 00:01:43,000 +And if I say, "Hey, NUMs, Pop." + +45 +00:01:43,000 --> 00:01:45,000 +So what NUMs will do is it will give you + +46 +00:01:45,000 --> 00:01:49,000 +the last element entered, which is eight, in this case. + +47 +00:01:49,000 --> 00:01:51,000 +But let's say you enter some more data, + +48 +00:01:51,000 --> 00:01:52,000 +you want to insert six as well. + +49 +00:01:52,000 --> 00:01:54,000 +So six goes here. + +50 +00:01:54,000 --> 00:01:55,000 +Now, when six goes there, + +51 +00:01:55,000 --> 00:01:56,000 +basically, when you say, + +52 +00:01:56,000 --> 00:02:00,000 +"Hey, I want to Pop now," so you have, it'll give you six. + +53 +00:02:00,000 --> 00:02:02,000 +So if you want to insert data, you will say Push. + +54 +00:02:02,000 --> 00:02:05,000 +When you want to get data out, we use Pop. + +55 +00:02:05,000 --> 00:02:09,000 +Basically, we have certain terms you have to remember. + +56 +00:02:09,000 --> 00:02:11,000 +What if the Stack is full? + +57 +00:02:11,000 --> 00:02:13,000 +So let's say the size of this Stack is four + +58 +00:02:13,000 --> 00:02:14,000 +or maybe three instead. + +59 +00:02:14,000 --> 00:02:16,000 +Let's say the Stack, the size of this stack is three, + +60 +00:02:16,000 --> 00:02:18,000 +and now you have entered three values. + +61 +00:02:18,000 --> 00:02:20,000 +But what if you are entering a new value? + +62 +00:02:20,000 --> 00:02:22,000 +So instead of three, let's have some other values, + +63 +00:02:22,000 --> 00:02:23,000 +so that you will not get confused + +64 +00:02:23,000 --> 00:02:26,000 +with the size of the Stack and the values. + +65 +00:02:26,000 --> 00:02:27,000 +Let's say the value is nine here. + +66 +00:02:27,000 --> 00:02:29,000 +So when you are inserting nine now, + +67 +00:02:29,000 --> 00:02:30,000 +now we don't have a space, right? + +68 +00:02:30,000 --> 00:02:33,000 +So nine, when you try to Push nine, + +69 +00:02:33,000 --> 00:02:38,000 +it will give you an error called, Overflow, + +70 +00:02:38,000 --> 00:02:39,000 +and you might have seen this error, right? + +71 +00:02:39,000 --> 00:02:42,000 +Which is called Stack Overflow, the website? + +72 +00:02:42,000 --> 00:02:44,000 +It came from this word, which is Overflow. + +73 +00:02:44,000 --> 00:02:46,000 +So the Stack overflowing. + +74 +00:02:46,000 --> 00:02:48,000 +So nine is not able to save there. + +75 +00:02:48,000 --> 00:02:51,000 +Also, there's one more term called, Underflow. + +76 +00:02:51,000 --> 00:02:51,000 +Now what is Underflow? + +77 +00:02:51,000 --> 00:02:53,000 +Let's say you have a stack which is empty. + +78 +00:02:53,000 --> 00:02:54,000 +So we are getting a new stack here, + +79 +00:02:54,000 --> 00:02:56,000 +which is empty in this case. + +80 +00:02:56,000 --> 00:02:58,000 +And when you say, "Hey, I have an empty stack here, + +81 +00:02:58,000 --> 00:02:59,000 +I want to Pop." + +82 +00:02:59,000 --> 00:03:01,000 +Now we can only Pop the elements + +83 +00:03:01,000 --> 00:03:02,000 +when they are there, it's not there. + +84 +00:03:02,000 --> 00:03:04,000 +So we don't have an element there. + +85 +00:03:04,000 --> 00:03:07,000 +So it will give you another, which is Underflow. + +86 +00:03:07,000 --> 00:03:10,000 +So Overflow when you have a Stack full, + +87 +00:03:10,000 --> 00:03:12,000 +but still you're trying to insert data. + +88 +00:03:12,000 --> 00:03:14,000 +And Underflow is basically, + +89 +00:03:14,000 --> 00:03:16,000 +where you don't have any elements there, + +90 +00:03:16,000 --> 00:03:18,000 +but you still try to fetch data or remove data. + +91 +00:03:18,000 --> 00:03:20,000 +So one thing to remember with Pop, + +92 +00:03:20,000 --> 00:03:23,000 +Pop is basically, a function or a way + +93 +00:03:23,000 --> 00:03:25,000 +to get the element out. + +94 +00:03:25,000 --> 00:03:26,000 +So of course, it will give you the element. + +95 +00:03:26,000 --> 00:03:29,000 +That also means that the element is coming out. + +96 +00:03:29,000 --> 00:03:31,000 +So example, let's say at this point, + +97 +00:03:31,000 --> 00:03:34,000 +I want to do a Pop on this Stack. + +98 +00:03:34,000 --> 00:03:35,000 +Basically, what I'm also doing + +99 +00:03:35,000 --> 00:03:37,000 +is I'm Popping this six out. + +100 +00:03:37,000 --> 00:03:39,000 +So now, if you say, + +101 +00:03:39,000 --> 00:03:41,000 +"How many elements I have in this stack?" + +102 +00:03:41,000 --> 00:03:42,000 +Only two, okay? + +103 +00:03:42,000 --> 00:03:45,000 +And again, if you say Pop on this particular NUMs, + +104 +00:03:45,000 --> 00:03:47,000 +it will take this eight out, + +105 +00:03:47,000 --> 00:03:48,000 +at which came with the underscore, + +106 +00:03:48,000 --> 00:03:50,000 +but that's not how it works. + +107 +00:03:50,000 --> 00:03:52,000 +Let me just do that again. + +108 +00:03:52,000 --> 00:03:54,000 +So eight will come out. + +109 +00:03:54,000 --> 00:03:56,000 +Again, when you say Pop, the five will come out. + +110 +00:03:56,000 --> 00:04:00,000 +Again, when you say Pop, it will give you an error, + +111 +00:04:00,000 --> 00:04:03,000 +which is the Underflow error or Underflow exception. + +112 +00:04:03,000 --> 00:04:06,000 +So basically, Pop means getting the data out. + +113 +00:04:06,000 --> 00:04:08,000 +But let's say you don't want to get the data out, + +114 +00:04:08,000 --> 00:04:11,000 +you just want to see what is a last element. + +115 +00:04:11,000 --> 00:04:14,000 +So we use one more thing for that, which is called, Peek. + +116 +00:04:14,000 --> 00:04:17,000 +Now, Peek will give you the last value, + +117 +00:04:17,000 --> 00:04:19,000 +but it will not remove the value from the Stack. + +118 +00:04:19,000 --> 00:04:21,000 +So if you just want to check, you want to Peek, + +119 +00:04:21,000 --> 00:04:23,000 +you can use Peek. + +120 +00:04:23,000 --> 00:04:26,000 +But if you want to get the element out, use a Pop. + +121 +00:04:26,000 --> 00:04:28,000 +When you want to insert data, you say Push. + +122 +00:04:28,000 --> 00:04:31,000 +So this is your Last IN, First OUT inside Stack. + +123 +00:04:31,000 --> 00:04:32,000 +Now, question arise, + +124 +00:04:32,000 --> 00:04:33,000 +"How we are going to implement this?" + +125 +00:04:33,000 --> 00:04:35,000 +So there are two ways of implementing this. + +126 +00:04:35,000 --> 00:04:38,000 +One, you can use Normal Array. + +127 +00:04:38,000 --> 00:04:40,000 +In fact, in Array itself, we can use + +128 +00:04:41,000 --> 00:04:45,000 +Fixed Size Array or we can use Dynamic Array. + +129 +00:04:45,000 --> 00:04:47,000 +Now in Fixed Size, basically, by default + +130 +00:04:47,000 --> 00:04:49,000 +in most of the languages, when you say Array, + +131 +00:04:49,000 --> 00:04:52,000 +it's a Fixed Size, but you can make it Dynamic. + +132 +00:04:52,000 --> 00:04:56,000 +So you can expand the Stack or the Array the way you want. + +133 +00:04:56,000 --> 00:04:58,000 +So overall, Stack is a concept, okay? + +134 +00:04:58,000 --> 00:05:00,000 +So if you want to implement that, we have to use Array. + +135 +00:05:00,000 --> 00:05:03,000 +The another way you can do that is with Linked List. + +136 +00:05:03,000 --> 00:05:05,000 +We have already seen Linked List. + +137 +00:05:05,000 --> 00:05:08,000 +So we can also implement Stack with help of Linked List. + +138 +00:05:08,000 --> 00:05:12,000 +The advantage would be there's no problem of the size there, + +139 +00:05:12,000 --> 00:05:14,000 +because in Linked List you can expand the Linked List. + +140 +00:05:14,000 --> 00:05:16,000 +The drawback is it will be bit slow, but that's fine. + +141 +00:05:16,000 --> 00:05:17,000 +Anyway, you are going to remove the data + +142 +00:05:17,000 --> 00:05:19,000 +from the last, right? + +143 +00:05:19,000 --> 00:05:21,000 +So you can implement Stack with help of Linked List as well. + +144 +00:05:21,000 --> 00:05:23,000 +So yeah, that's about Stack + +145 +00:05:23,000 --> 00:05:25,000 +and how do we are going to implement that. + +146 +00:05:25,000 --> 00:05:27,000 +So we are going to write a code for Array, + +147 +00:05:27,000 --> 00:05:29,000 +I mean using Array for Stack. + +148 +00:05:29,000 --> 00:05:31,000 +And that we'll see in the next video. + diff --git a/28 - DSA (Optional)/023 Stack Code Push_en.srt b/28 - DSA (Optional)/023 Stack Code Push_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..df59e1622d098559786470c0357a32e744f1efb7 --- /dev/null +++ b/28 - DSA (Optional)/023 Stack Code Push_en.srt @@ -0,0 +1,912 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: Okay, so let's try to implement the Stack in Java. + +2 +00:00:02,000 --> 00:00:04,000 +So what you do is basically, + +3 +00:00:04,000 --> 00:00:06,000 +we have talked about the theory, right? + +4 +00:00:06,000 --> 00:00:09,000 +Now if you want to use Stack in Java, + +5 +00:00:09,000 --> 00:00:11,000 +basically we can simply say Stack, + +6 +00:00:11,000 --> 00:00:14,000 +and it's a inbuilt class available in Java. + +7 +00:00:14,000 --> 00:00:15,000 +So I can simply say Stack. + +8 +00:00:15,000 --> 00:00:16,000 +And if you can see, this is the package + +9 +00:00:16,000 --> 00:00:18,000 +where you find Stack. + +10 +00:00:18,000 --> 00:00:20,000 +So if I click on Stack, you can see it's a class. + +11 +00:00:20,000 --> 00:00:23,000 +And basically it provides you the methods + +12 +00:00:23,000 --> 00:00:24,000 +which we have talked about. + +13 +00:00:24,000 --> 00:00:27,000 +We have talked about push, we also have a pop here. + +14 +00:00:27,000 --> 00:00:31,000 +Then we can see also peek. So everything is there, right? + +15 +00:00:31,000 --> 00:00:32,000 +But then I don't want to use inbuilt, + +16 +00:00:32,000 --> 00:00:34,000 +but even if you are using in inbuilt, let's see + +17 +00:00:34,000 --> 00:00:39,000 +what happens if I say Stack nums is equal to new Stack, + +18 +00:00:39,000 --> 00:00:42,000 +I don't want to use any specific type of data which we want. + +19 +00:00:42,000 --> 00:00:44,000 +Let's go for objects. + +20 +00:00:44,000 --> 00:00:47,000 +And if I want to add data, I can simply say push. + +21 +00:00:47,000 --> 00:00:49,000 +And when I say push data, let's say 10. + +22 +00:00:49,000 --> 00:00:51,000 +Now what happens is in this Stack, + +23 +00:00:51,000 --> 00:00:53,000 +we only have one value, which is 10. + +24 +00:00:53,000 --> 00:00:56,000 +Okay? So if I try to print the Stack, + +25 +00:00:56,000 --> 00:00:58,000 +which is nums, let's see what happens. + +26 +00:00:58,000 --> 00:01:00,000 +And I click this and it says Run. + +27 +00:01:00,000 --> 00:01:01,000 +And in the output you can see it says 10. + +28 +00:01:01,000 --> 00:01:03,000 +So you have one data, + +29 +00:01:03,000 --> 00:01:06,000 +but what happens when you push more data here? + +30 +00:01:06,000 --> 00:01:09,000 +So I can just simply copy paste this one more time. + +31 +00:01:09,000 --> 00:01:11,000 +So we got four values now, + +32 +00:01:11,000 --> 00:01:14,000 +and let's say this is 30, this is 70, this is 20. + +33 +00:01:14,000 --> 00:01:18,000 +And when you add all the values here, if you run this, + +34 +00:01:18,000 --> 00:01:19,000 +you will get the output + +35 +00:01:19,000 --> 00:01:22,000 +of all the values you've got 10, 30, 70, 20. + +36 +00:01:22,000 --> 00:01:25,000 +Now yes, it looks like sequence, the way you have added, + +37 +00:01:25,000 --> 00:01:28,000 +but in Stack it goes in first in, first out. + +38 +00:01:28,000 --> 00:01:32,000 +Basically the 10 got insert first, 30 get insert later, + +39 +00:01:32,000 --> 00:01:34,000 +then 70, then 20. + +40 +00:01:34,000 --> 00:01:35,000 +Let me prove my point. + +41 +00:01:35,000 --> 00:01:37,000 +What I will do is I will simply say, + +42 +00:01:37,000 --> 00:01:39,000 +nums.pop + +43 +00:01:39,000 --> 00:01:43,000 +Now what pop will do is it'll remove the last element, + +44 +00:01:43,000 --> 00:01:47,000 +or maybe I can also do this, not here, but after 30. + +45 +00:01:47,000 --> 00:01:48,000 +So when I say pop, + +46 +00:01:48,000 --> 00:01:51,000 +pop will remove the element, the first element, okay? + +47 +00:01:51,000 --> 00:01:53,000 +So when I say first element, not exactly first element, + +48 +00:01:53,000 --> 00:01:55,000 +it'll remove the last element, + +49 +00:01:55,000 --> 00:01:57,000 +last element, which got added. + +50 +00:01:57,000 --> 00:02:00,000 +So the first element which you can access + +51 +00:02:00,000 --> 00:02:01,000 +is the last element. + +52 +00:02:01,000 --> 00:02:03,000 +And that sounds weird, but that's how it works. + +53 +00:02:03,000 --> 00:02:06,000 +So if I run this again, it'll remove 30. + +54 +00:02:06,000 --> 00:02:08,000 +So that's where it got popped. + +55 +00:02:08,000 --> 00:02:11,000 +In fact, pop will give you the value if you want. + +56 +00:02:11,000 --> 00:02:12,000 +So you can store that value somewhere. + +57 +00:02:12,000 --> 00:02:15,000 +Maybe you can print that value somewhere if you want. + +58 +00:02:15,000 --> 00:02:17,000 +Let me just print it here. + +59 +00:02:17,000 --> 00:02:20,000 +So I'm printing that value, which we popped out of this. + +60 +00:02:20,000 --> 00:02:23,000 +And you can see the value study. So we can do these things. + +61 +00:02:23,000 --> 00:02:24,000 +In fact, we can also use peek, + +62 +00:02:24,000 --> 00:02:28,000 +but I don't want to use the in-build plus. + +63 +00:02:28,000 --> 00:02:30,000 +Let's create our own. + +64 +00:02:30,000 --> 00:02:32,000 +So the one thing you have to do is when you see the Stack, + +65 +00:02:32,000 --> 00:02:35,000 +let's create our own Stack. + +66 +00:02:35,000 --> 00:02:38,000 +So if I say, "Okay, gimme a suggestion." + +67 +00:02:38,000 --> 00:02:42,000 +Okay, so more action depending upon which IDE you are using, + +68 +00:02:42,000 --> 00:02:43,000 +it'll have the same thing. + +69 +00:02:43,000 --> 00:02:46,000 +Click here, I want to go for the default package. + +70 +00:02:46,000 --> 00:02:48,000 +And you can see we got a Stack plus. + +71 +00:02:48,000 --> 00:02:50,000 +So we have two Stack on the top, + +72 +00:02:50,000 --> 00:02:53,000 +but one is coming from the util package, which is inbuilt. + +73 +00:02:53,000 --> 00:02:55,000 +I don't want to use it. This is the Stack I want to use. + +74 +00:02:55,000 --> 00:02:59,000 +Let me separate the code somewhere so that you can see both. + +75 +00:02:59,000 --> 00:03:01,000 +Okay? So right inside we have our own class, + +76 +00:03:01,000 --> 00:03:03,000 +and on this side we have the main. + +77 +00:03:03,000 --> 00:03:05,000 +Okay, so I want to achieve the same thing, + +78 +00:03:05,000 --> 00:03:08,000 +but at this point, let me just comment the pop part, + +79 +00:03:08,000 --> 00:03:09,000 +let me also only work with push. + +80 +00:03:09,000 --> 00:03:11,000 +Unfortunately we don't have push here. + +81 +00:03:11,000 --> 00:03:15,000 +So let me create one method, which is public void push, + +82 +00:03:15,000 --> 00:03:17,000 +and this will take a value from you. + +83 +00:03:17,000 --> 00:03:19,000 +So I would say int data, let's say, + +84 +00:03:19,000 --> 00:03:22,000 +and okay, not declaring it, defining it, okay, + +85 +00:03:22,000 --> 00:03:24,000 +but then very will push it. + +86 +00:03:24,000 --> 00:03:25,000 +Of course you need an add-in here, right? + +87 +00:03:25,000 --> 00:03:28,000 +So as I mentioned, there are different way + +88 +00:03:28,000 --> 00:03:29,000 +of implementing Stack. + +89 +00:03:29,000 --> 00:03:31,000 +One of the way is you can use array. + +90 +00:03:31,000 --> 00:03:32,000 +So let me use an array here. + +91 +00:03:32,000 --> 00:03:34,000 +So I will use a private int array. + +92 +00:03:34,000 --> 00:03:36,000 +So let's name this as array itself, + +93 +00:03:36,000 --> 00:03:39,000 +of course you can use any variable name, doesn't matter. + +94 +00:03:39,000 --> 00:03:41,000 +And then I can assign some size to it. + +95 +00:03:41,000 --> 00:03:43,000 +So let's say the size of this array is only five. + +96 +00:03:43,000 --> 00:03:46,000 +So the max value you can have is five. Okay? + +97 +00:03:46,000 --> 00:03:48,000 +Now apart from this, if you remember the theory, + +98 +00:03:48,000 --> 00:03:50,000 +we have talked about some variables. + +99 +00:03:50,000 --> 00:03:52,000 +One of the variables you have to remember is top. + +100 +00:03:52,000 --> 00:03:53,000 +Now top will refer + +101 +00:03:53,000 --> 00:03:57,000 +to which is the last element which I've added. + +102 +00:03:57,000 --> 00:03:58,000 +I'm in the index value. + +103 +00:03:58,000 --> 00:04:00,000 +So let's say you have inserted three values. + +104 +00:04:00,000 --> 00:04:02,000 +So the top will be representing three. + +105 +00:04:02,000 --> 00:04:05,000 +But then by default, the value for top would be minus one, + +106 +00:04:05,000 --> 00:04:07,000 +because I want to start from minus one + +107 +00:04:07,000 --> 00:04:08,000 +because we don't have any values yet. + +108 +00:04:08,000 --> 00:04:10,000 +So it's minus one. + +109 +00:04:10,000 --> 00:04:11,000 +We can take one more variable here, + +110 +00:04:11,000 --> 00:04:14,000 +which will define the size of the array. + +111 +00:04:14,000 --> 00:04:15,000 +So I can say int size, + +112 +00:04:15,000 --> 00:04:17,000 +but then I can't assign the value here. + +113 +00:04:17,000 --> 00:04:19,000 +We should take the value of the size of the array. + +114 +00:04:19,000 --> 00:04:23,000 +So what we can take that in a Stack constructor. + +115 +00:04:23,000 --> 00:04:24,000 +So I can say Stack. + +116 +00:04:24,000 --> 00:04:28,000 +And inside this I can say size is equal to the size + +117 +00:04:28,000 --> 00:04:30,000 +of the array.length + +118 +00:04:30,000 --> 00:04:32,000 +So that's my size, that's the maximum size I can have. + +119 +00:04:32,000 --> 00:04:35,000 +In fact, let's also define the size + +120 +00:04:35,000 --> 00:04:38,000 +of top inside the constructor. + +121 +00:04:38,000 --> 00:04:39,000 +And that's done. + +122 +00:04:39,000 --> 00:04:40,000 +If you want, you can also define this + +123 +00:04:40,000 --> 00:04:43,000 +array creation inside the constructor. + +124 +00:04:43,000 --> 00:04:45,000 +So you can have a declaration here, assignment, + +125 +00:04:45,000 --> 00:04:46,000 +you can do it in the constructor, + +126 +00:04:46,000 --> 00:04:48,000 +but again, that's your implementation way. + +127 +00:04:48,000 --> 00:04:52,000 +Okay, so once we got that, how do you push element there? + +128 +00:04:52,000 --> 00:04:53,000 +So pushing is actually very easy. + +129 +00:04:53,000 --> 00:04:56,000 +You take your array and say, "Okay, I want + +130 +00:04:56,000 --> 00:04:58,000 +to insert at the first location, which is zero" + +131 +00:04:58,000 --> 00:05:00,000 +because array starts with zero. + +132 +00:05:00,000 --> 00:05:02,000 +And you can save the data, the job is done, + +133 +00:05:02,000 --> 00:05:03,000 +you are able to push it. + +134 +00:05:03,000 --> 00:05:04,000 +Okay, let's see how it works. + +135 +00:05:04,000 --> 00:05:07,000 +But also we can't say nums here + +136 +00:05:07,000 --> 00:05:09,000 +because we are creating our own class. + +137 +00:05:09,000 --> 00:05:11,000 +So if you want this to work, you have + +138 +00:05:11,000 --> 00:05:13,000 +to implement two-string method. + +139 +00:05:13,000 --> 00:05:16,000 +But instead of doing that, I will say public void, + +140 +00:05:16,000 --> 00:05:20,000 +print string or print Stack, not print string, print Stack. + +141 +00:05:20,000 --> 00:05:22,000 +So it'll print the entire Stack for me. + +142 +00:05:22,000 --> 00:05:23,000 +And the way you can do that, + +143 +00:05:23,000 --> 00:05:27,000 +you can use enhanced for loop here, which is int n + +144 +00:05:27,000 --> 00:05:30,000 +in array and we can print all the values. + +145 +00:05:30,000 --> 00:05:35,000 +So we can say without add-in I want to print all the values. + +146 +00:05:35,000 --> 00:05:38,000 +We can print N, and I can give a space in between. + +147 +00:05:38,000 --> 00:05:41,000 +And at the end, I will print the new line so + +148 +00:05:41,000 --> 00:05:43,000 +that it will come out the new line after printing. + +149 +00:05:43,000 --> 00:05:46,000 +Okay, so here I cannot say NUMs, + +150 +00:05:46,000 --> 00:05:49,000 +I can say nums.print + +151 +00:05:50,000 --> 00:05:52,000 +Okay, let's say this works. + +152 +00:05:52,000 --> 00:05:54,000 +We have added two values, 10 and 30. + +153 +00:05:54,000 --> 00:05:56,000 +And then I'm trying to say print Stack. + +154 +00:05:56,000 --> 00:05:58,000 +Let's run this code. + +155 +00:05:58,000 --> 00:06:01,000 +And you can see it's printing only one value which is 30. + +156 +00:06:01,000 --> 00:06:03,000 +Not the other values. Why? + +157 +00:06:03,000 --> 00:06:06,000 +The reason is every time you say push, + +158 +00:06:06,000 --> 00:06:08,000 +don't you think you're pushing at the same location, zero? + +159 +00:06:08,000 --> 00:06:09,000 +It should change, right? + +160 +00:06:09,000 --> 00:06:12,000 +So the first value should go at the first location. + +161 +00:06:12,000 --> 00:06:13,000 +The second value should go to second location. + +162 +00:06:13,000 --> 00:06:16,000 +So how do we know where to push? + +163 +00:06:16,000 --> 00:06:18,000 +So we can use something called top here. + +164 +00:06:18,000 --> 00:06:21,000 +So every time you basically push a value, we have + +165 +00:06:21,000 --> 00:06:23,000 +to increment the value of top. + +166 +00:06:23,000 --> 00:06:25,000 +But also there's one more problem. + +167 +00:06:25,000 --> 00:06:26,000 +The top initial value is minus one. + +168 +00:06:26,000 --> 00:06:29,000 +That means we need to do the increment + +169 +00:06:29,000 --> 00:06:31,000 +before we assign the value. + +170 +00:06:31,000 --> 00:06:34,000 +So once we do this, the top value, which is + +171 +00:06:34,000 --> 00:06:36,000 +by default minus one, will go to zero. + +172 +00:06:36,000 --> 00:06:38,000 +And then you are assigning it here. + +173 +00:06:38,000 --> 00:06:42,000 +Next time when you do that, when you insert 30 here, + +174 +00:06:42,000 --> 00:06:43,000 +what it'll do is it'll again increment the value of top, + +175 +00:06:43,000 --> 00:06:46,000 +which is zero, which will become one. + +176 +00:06:46,000 --> 00:06:48,000 +And then you're inserting at the second location. + +177 +00:06:48,000 --> 00:06:49,000 +Let's say if this works, and yes it works, + +178 +00:06:49,000 --> 00:06:51,000 +we can say we got 10, 30. + +179 +00:06:51,000 --> 00:06:53,000 +We can have some more values here. + +180 +00:06:53,000 --> 00:06:55,000 +In fact, I can just uncommand this section + +181 +00:06:55,000 --> 00:06:57,000 +and maybe add two more values. + +182 +00:06:57,000 --> 00:06:59,000 +Or maybe let's try one more value. + +183 +00:06:59,000 --> 00:07:03,000 +Let's say this is 50, with 50, it also works. + +184 +00:07:03,000 --> 00:07:04,000 +You can see we've got all these values. + +185 +00:07:04,000 --> 00:07:09,000 +I can have one more here, and let's say this is 90. + +186 +00:07:09,000 --> 00:07:12,000 +And the moment I do that, you can see we will get an error, + +187 +00:07:12,000 --> 00:07:16,000 +which is add a error index out of bound exception. + +188 +00:07:16,000 --> 00:07:18,000 +So this is not what we wanted, right? + +189 +00:07:18,000 --> 00:07:22,000 +Basically this is going out of the box + +190 +00:07:22,000 --> 00:07:23,000 +because we only have five values, + +191 +00:07:23,000 --> 00:07:24,000 +we're inserting six values. + +192 +00:07:24,000 --> 00:07:25,000 +Not good. + +193 +00:07:25,000 --> 00:07:27,000 +So that means when you are pushing data, we have + +194 +00:07:27,000 --> 00:07:29,000 +to also check if the Stack is full. + +195 +00:07:29,000 --> 00:07:30,000 +How do we do that? + +196 +00:07:30,000 --> 00:07:33,000 +So here we can actually check if, + +197 +00:07:33,000 --> 00:07:35,000 +so, before doing all this operation, + +198 +00:07:35,000 --> 00:07:37,000 +we can check if the size of the Stack. + +199 +00:07:37,000 --> 00:07:39,000 +Now, how do we know the size of the Stack? + +200 +00:07:39,000 --> 00:07:41,000 +I mean the values which have inserted, we know the size, + +201 +00:07:41,000 --> 00:07:44,000 +but the values inserted will be stored in top. + +202 +00:07:44,000 --> 00:07:46,000 +So we can simply check with top. + +203 +00:07:46,000 --> 00:07:48,000 +If top is greater than equal to size, + +204 +00:07:48,000 --> 00:07:51,000 +we should not be doing this, or we should do this + +205 +00:07:51,000 --> 00:07:53,000 +only when the top is less than the size, + +206 +00:07:53,000 --> 00:07:54,000 +then it makes sense, right? + +207 +00:07:54,000 --> 00:07:59,000 +So this two section should be a part of this If condition. + +208 +00:07:59,000 --> 00:08:02,000 +Now I think there's no issue. Let's run this. + +209 +00:08:02,000 --> 00:08:04,000 +Okay, we still have an issue. + +210 +00:08:04,000 --> 00:08:05,000 +That means we are not, + +211 +00:08:05,000 --> 00:08:07,000 +okay because we are incrementing. + +212 +00:08:07,000 --> 00:08:08,000 +This should be outside + +213 +00:08:09,000 --> 00:08:12,000 +because how it'll check if it is not incrementing? + +214 +00:08:12,000 --> 00:08:14,000 +And since we only have one statement, + +215 +00:08:14,000 --> 00:08:16,000 +we can remove this curly brackets. + +216 +00:08:16,000 --> 00:08:19,000 +Let's try and yeah, it worked. + +217 +00:08:19,000 --> 00:08:20,000 +You can see the last element + +218 +00:08:20,000 --> 00:08:21,000 +which I've added is not inserted, + +219 +00:08:21,000 --> 00:08:23,000 +but also you can do one more thing here + +220 +00:08:23,000 --> 00:08:25,000 +in the else part. + +221 +00:08:25,000 --> 00:08:30,000 +You can check or maybe you can print Stack overflow, okay? + +222 +00:08:31,000 --> 00:08:32,000 +The famous website, + +223 +00:08:32,000 --> 00:08:35,000 +run, and you can see this is printing Stack overflow + +224 +00:08:35,000 --> 00:08:36,000 +for the last element. + +225 +00:08:36,000 --> 00:08:39,000 +Okay, so you can see now we are getting Stack overflow. + +226 +00:08:39,000 --> 00:08:41,000 +So that means this is working. + +227 +00:08:41,000 --> 00:08:45,000 +So push is working, what about pop and peek? + +228 +00:08:45,000 --> 00:08:47,000 +Let's see that in the next video. + diff --git a/28 - DSA (Optional)/024 Stack Code Pop Peek_en.srt b/28 - DSA (Optional)/024 Stack Code Pop Peek_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..259997a71aa7e52575e8bafd5944dc59d2af3ca0 --- /dev/null +++ b/28 - DSA (Optional)/024 Stack Code Pop Peek_en.srt @@ -0,0 +1,408 @@ +1 +00:00:00,000 --> 00:00:01,000 +-: So we are done with push. + +2 +00:00:01,000 --> 00:00:03,000 +Now it's time for pop and peak. + +3 +00:00:03,000 --> 00:00:05,000 +So let's write a method for push. + +4 +00:00:05,000 --> 00:00:08,000 +Let's say at this point I want to say pop so + +5 +00:00:08,000 --> 00:00:10,000 +that when you push the last element, so in total, + +6 +00:00:10,000 --> 00:00:12,000 +what do you think when I say pop here + +7 +00:00:12,000 --> 00:00:15,000 +and if it works, how many elements will be getting added? + +8 +00:00:15,000 --> 00:00:17,000 +Of course, we'll add six elements, + +9 +00:00:17,000 --> 00:00:19,000 +but then ultimately we'll be having five is because + +10 +00:00:19,000 --> 00:00:21,000 +after pushing 10 and 30, + +11 +00:00:21,000 --> 00:00:24,000 +so basically at this point you are removing 30, right? + +12 +00:00:24,000 --> 00:00:27,000 +The last element, then you only have 10, + +13 +00:00:27,000 --> 00:00:28,000 +and then you're inserting four. + +14 +00:00:28,000 --> 00:00:29,000 +So in total you'll be having five + +15 +00:00:29,000 --> 00:00:30,000 +or we are going to implement pop. + +16 +00:00:30,000 --> 00:00:32,000 +So think, remember when you say pop, + +17 +00:00:32,000 --> 00:00:33,000 +it returns a value, right? + +18 +00:00:33,000 --> 00:00:36,000 +So you have to say public int pop, we don't have + +19 +00:00:36,000 --> 00:00:38,000 +to pass any parameter, we just have to implement. + +20 +00:00:38,000 --> 00:00:40,000 +Now we have to return something, right? + +21 +00:00:40,000 --> 00:00:41,000 +Now what we have to return. + +22 +00:00:41,000 --> 00:00:43,000 +Remember when you talk about the stack, + +23 +00:00:43,000 --> 00:00:46,000 +there's one variable which is pointing to the last element, + +24 +00:00:46,000 --> 00:00:48,000 +and that is your top. + +25 +00:00:48,000 --> 00:00:52,000 +So if you can simply get the value from the array of top, + +26 +00:00:52,000 --> 00:00:53,000 +your job is done, don't you think? + +27 +00:00:53,000 --> 00:00:55,000 +Okay, so this is how it works. + +28 +00:00:55,000 --> 00:00:56,000 +You can simply return the pop. + +29 +00:00:56,000 --> 00:00:57,000 +And when you run this code, + +30 +00:00:57,000 --> 00:01:00,000 +and we are printing pop as well, let's run this + +31 +00:01:00,000 --> 00:01:02,000 +and you can say it is printing 30. + +32 +00:01:02,000 --> 00:01:04,000 +So that's right, we are getting the value. + +33 +00:01:04,000 --> 00:01:06,000 +What it is not doing is removing the element + +34 +00:01:06,000 --> 00:01:10,000 +because see in the output, 30 is still there. + +35 +00:01:10,000 --> 00:01:12,000 +So that means we have to remove as well. + +36 +00:01:12,000 --> 00:01:13,000 +So how do we remove? + +37 +00:01:13,000 --> 00:01:14,000 +So we can do one thing. + +38 +00:01:14,000 --> 00:01:15,000 +We can say minus minus. + +39 +00:01:15,000 --> 00:01:17,000 +What it'll do is it'll return the value + +40 +00:01:17,000 --> 00:01:18,000 +of top and then it'll decrement. + +41 +00:01:18,000 --> 00:01:20,000 +This is post decrement, right? + +42 +00:01:20,000 --> 00:01:22,000 +So post decrement means it'll give you the value first, + +43 +00:01:22,000 --> 00:01:25,000 +then it'll decrement, and let's see if that works. + +44 +00:01:25,000 --> 00:01:28,000 +Run. And yes, it worked, you can see it is giving you 30. + +45 +00:01:28,000 --> 00:01:30,000 +That's the right output, but in the entire + +46 +00:01:30,000 --> 00:01:31,000 +stack, we don't have 30 now. + +47 +00:01:31,000 --> 00:01:32,000 +So pop is working. + +48 +00:01:32,000 --> 00:01:35,000 +Don't you think the same thing can be done in push as well? + +49 +00:01:35,000 --> 00:01:36,000 +So instead of incrementing it here, + +50 +00:01:36,000 --> 00:01:38,000 +what if you increment here? + +51 +00:01:38,000 --> 00:01:40,000 +I think that should work, that's one. + +52 +00:01:40,000 --> 00:01:42,000 +And yeah, push is still working. + +53 +00:01:42,000 --> 00:01:44,000 +So we can also reduce one line here. + +54 +00:01:44,000 --> 00:01:46,000 +Now here we have to do pre increment is + +55 +00:01:46,000 --> 00:01:48,000 +because first we have incrementing, + +56 +00:01:48,000 --> 00:01:50,000 +then we are assigning a value. + +57 +00:01:50,000 --> 00:01:51,000 +So this was pop. + +58 +00:01:51,000 --> 00:01:53,000 +Now since we have worked with pop, + +59 +00:01:53,000 --> 00:01:55,000 +we can also work with peak here. + +60 +00:01:55,000 --> 00:01:58,000 +So here, if I say it's peak, the only thing you have + +61 +00:01:58,000 --> 00:02:01,000 +to do is don't remove the value. + +62 +00:02:01,000 --> 00:02:04,000 +Okay? So now if I, at any given point, + +63 +00:02:04,000 --> 00:02:08,000 +let's say at this point, if I say system dot, out dot print, + +64 +00:02:08,000 --> 00:02:11,000 +nums dot peak, it'll basically give the value, + +65 +00:02:11,000 --> 00:02:12,000 +but it'll not remove that value. + +66 +00:02:12,000 --> 00:02:15,000 +So you can see that our value is giving us 20. + +67 +00:02:15,000 --> 00:02:16,000 +It is not removing 20. + +68 +00:02:16,000 --> 00:02:18,000 +So that's the main difference between pop and peak. + +69 +00:02:18,000 --> 00:02:20,000 +You can do some more things here. + +70 +00:02:20,000 --> 00:02:24,000 +Example, we are checking for the size here, + +71 +00:02:24,000 --> 00:02:26,000 +we still have something to do with pop. + +72 +00:02:26,000 --> 00:02:27,000 +So let me just talk about that. + +73 +00:02:27,000 --> 00:02:29,000 +But you can modify this + +74 +00:02:29,000 --> 00:02:33,000 +and you can create another method called is full is empty. + +75 +00:02:33,000 --> 00:02:35,000 +That also makes sense because then you don't have + +76 +00:02:35,000 --> 00:02:37,000 +to compare in this, you can simply say is full. + +77 +00:02:37,000 --> 00:02:40,000 +Then you don't do this, otherwise you can do this. + +78 +00:02:40,000 --> 00:02:41,000 +So you can have another method. + +79 +00:02:41,000 --> 00:02:42,000 +Okay, so now why I'm saying + +80 +00:02:42,000 --> 00:02:45,000 +that this pop is not complete, let me show you something. + +81 +00:02:45,000 --> 00:02:46,000 +What if I just uncomment everything? + +82 +00:02:46,000 --> 00:02:49,000 +And here now since we don't have any value, + +83 +00:02:49,000 --> 00:02:52,000 +what if I send nums dot pop? + +84 +00:02:52,000 --> 00:02:55,000 +What will happen is, or it'll give you an error. + +85 +00:02:55,000 --> 00:02:57,000 +It is again saying in this out of bound exceptions, + +86 +00:02:57,000 --> 00:03:00,000 +because by default top is minus one. + +87 +00:03:00,000 --> 00:03:01,000 +Okay? So we don't wanna do that. + +88 +00:03:01,000 --> 00:03:03,000 +So when should we return? + +89 +00:03:03,000 --> 00:03:08,000 +So what you can do is if the top is more than minus + +90 +00:03:08,000 --> 00:03:10,000 +one, then on the return, otherwise don't return. + +91 +00:03:10,000 --> 00:03:12,000 +That's what we're gonna say, but then we cannot put return + +92 +00:03:12,000 --> 00:03:16,000 +inside if, I can simply say return here as well. + +93 +00:03:16,000 --> 00:03:18,000 +Let's return with some value, it says zero. + +94 +00:03:18,000 --> 00:03:20,000 +Okay, what if it is the stack is empty? + +95 +00:03:20,000 --> 00:03:24,000 +In that case, I can print stack underflow. + +96 +00:03:24,000 --> 00:03:26,000 +So at this point it should print stack underflow. + +97 +00:03:26,000 --> 00:03:28,000 +So the one, you can see there's no problem. + +98 +00:03:28,000 --> 00:03:29,000 +It is printing stack underflow. + +99 +00:03:29,000 --> 00:03:31,000 +Okay. So that's how we can add this. + +100 +00:03:31,000 --> 00:03:33,000 +Again, if you can simplify this, + +101 +00:03:33,000 --> 00:03:35,000 +you can also use is empty method. + +102 +00:03:35,000 --> 00:03:38,000 +That should also work, so this is all about stack in Java. + diff --git a/28 - DSA (Optional)/025 Queue Theory_en.srt b/28 - DSA (Optional)/025 Queue Theory_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..53190731d515d054c3ee8c3ad435d87f7a0526c0 --- /dev/null +++ b/28 - DSA (Optional)/025 Queue Theory_en.srt @@ -0,0 +1,1212 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: In this video, we'll talk about queue. + +2 +00:00:02,000 --> 00:00:04,000 +The thing is, we have talked about stack, right, + +3 +00:00:04,000 --> 00:00:06,000 +which is last in, first out. + +4 +00:00:06,000 --> 00:00:07,000 +But what if you don't want to do that? + +5 +00:00:07,000 --> 00:00:09,000 +You don't want to do last in, first out, + +6 +00:00:09,000 --> 00:00:11,000 +because in the real world when you go to shopping, + +7 +00:00:11,000 --> 00:00:14,000 +when you have a checkout counter, basically the first person + +8 +00:00:14,000 --> 00:00:18,000 +who goes there is the first person to go out, right? + +9 +00:00:18,000 --> 00:00:21,000 +You don't want to have stack in that situation. + +10 +00:00:21,000 --> 00:00:23,000 +And that's where we also have a word + +11 +00:00:23,000 --> 00:00:24,000 +called queue in English. + +12 +00:00:24,000 --> 00:00:27,000 +So we use a concept called queue there, which simply means + +13 +00:00:27,000 --> 00:00:30,000 +that it will not follow last in, first out. + +14 +00:00:30,000 --> 00:00:34,000 +It will follow first in, first out, something like this. + +15 +00:00:34,000 --> 00:00:36,000 +So let's say you got an array here, + +16 +00:00:36,000 --> 00:00:38,000 +and this array has multiple values, + +17 +00:00:38,000 --> 00:00:43,000 +let's say four values in this case, so 1, 2, 3, 4. + +18 +00:00:43,000 --> 00:00:45,000 +And then if you want to insert some value, + +19 +00:00:45,000 --> 00:00:47,000 +basically it will go from one end + +20 +00:00:47,000 --> 00:00:49,000 +and it will come out from the other end. + +21 +00:00:49,000 --> 00:00:52,000 +So what I want is, let's say I have different values here, + +22 +00:00:52,000 --> 00:00:55,000 +which is 5, 8, 2 and 3. + +23 +00:00:55,000 --> 00:00:56,000 +So let's say I have these four values, + +24 +00:00:56,000 --> 00:00:57,000 +which I want to insert. + +25 +00:00:57,000 --> 00:01:00,000 +So basically it will go from one end. + +26 +00:01:00,000 --> 00:01:02,000 +It will come out from the other end. + +27 +00:01:02,000 --> 00:01:05,000 +So we can say this is my front + +28 +00:01:05,000 --> 00:01:07,000 +and this is my rear. + +29 +00:01:07,000 --> 00:01:09,000 +So basically the values will get inside from the rear. + +30 +00:01:09,000 --> 00:01:11,000 +So example, if I want to insert 5, + +31 +00:01:11,000 --> 00:01:13,000 +5 will come from here, + +32 +00:01:13,000 --> 00:01:17,000 +but then it will check where we can place it. + +33 +00:01:17,000 --> 00:01:19,000 +Basically, it will always try to paste at the other end. + +34 +00:01:19,000 --> 00:01:22,000 +So 5 basically goes here. What about the next value? + +35 +00:01:22,000 --> 00:01:25,000 +So 8 comes and 8 goes here, + +36 +00:01:25,000 --> 00:01:26,000 +2 comes and 2 goes here, + +37 +00:01:26,000 --> 00:01:28,000 +and 3 comes and 3 goes here. + +38 +00:01:28,000 --> 00:01:29,000 +It makes sense, right? + +39 +00:01:29,000 --> 00:01:31,000 +Of course, that's how we'll paste the value. + +40 +00:01:31,000 --> 00:01:35,000 +What makes this different from stack is first in, first out. + +41 +00:01:35,000 --> 00:01:38,000 +Basically, which is the first value, which I have entered? + +42 +00:01:38,000 --> 00:01:39,000 +Of course 5, right? + +43 +00:01:39,000 --> 00:01:41,000 +Now, if I want to remove the value + +44 +00:01:41,000 --> 00:01:42,000 +or take out the value, + +45 +00:01:42,000 --> 00:01:44,000 +which would the first one? + +46 +00:01:44,000 --> 00:01:47,000 +It is your 5. So 5 will come out from here, + +47 +00:01:47,000 --> 00:01:49,000 +so first in first out. + +48 +00:01:49,000 --> 00:01:51,000 +So this is how basically queue works, okay? + +49 +00:01:51,000 --> 00:01:53,000 +But there are certain terms which you have to remember. + +50 +00:01:53,000 --> 00:01:55,000 +So when you say you are inserting the data, + +51 +00:01:55,000 --> 00:01:58,000 +we normally call that as enqueue. + +52 +00:01:58,000 --> 00:02:01,000 +So enqueue means you are inserting data. + +53 +00:02:01,000 --> 00:02:02,000 +Then we have a word called dequeue. + +54 +00:02:02,000 --> 00:02:04,000 +Dequeue means removing the data. + +55 +00:02:04,000 --> 00:02:05,000 +So example, whenever you insert the value, + +56 +00:02:05,000 --> 00:02:07,000 +that's your enqueue. + +57 +00:02:07,000 --> 00:02:09,000 +Whenever you remove the value, that's your dequeue. + +58 +00:02:09,000 --> 00:02:11,000 +So what happens in dequeue is + +59 +00:02:11,000 --> 00:02:13,000 +the first element basically comes out. + +60 +00:02:13,000 --> 00:02:17,000 +So the first element comes out. That's your dequeue. + +61 +00:02:17,000 --> 00:02:20,000 +And when you insert the next value, that is your enqueue. + +62 +00:02:20,000 --> 00:02:22,000 +But then the problem is the size of this queue. + +63 +00:02:22,000 --> 00:02:25,000 +Of course we can implement that with multiple things. + +64 +00:02:25,000 --> 00:02:27,000 +You can use array or link list. + +65 +00:02:27,000 --> 00:02:28,000 +But let's say if you have, if you're using array, + +66 +00:02:28,000 --> 00:02:30,000 +the size of this is four, right, + +67 +00:02:30,000 --> 00:02:33,000 +which means you cannot insert another value. + +68 +00:02:33,000 --> 00:02:34,000 +So let's say someone says, + +69 +00:02:34,000 --> 00:02:35,000 +"Hey, I want to insert one more value," + +70 +00:02:35,000 --> 00:02:38,000 +which is let's say 9, now you cannot insert 9 there + +71 +00:02:38,000 --> 00:02:39,000 +because we don't have a space. + +72 +00:02:39,000 --> 00:02:41,000 +But logically we do have a space, right? + +73 +00:02:41,000 --> 00:02:43,000 +We have a space at the start, + +74 +00:02:43,000 --> 00:02:47,000 +but unfortunately we can only insert from the rear end. + +75 +00:02:47,000 --> 00:02:49,000 +Now, that's one of the tricky point. How do you do that? + +76 +00:02:49,000 --> 00:02:52,000 +So one solution could be move all the elements. + +77 +00:02:52,000 --> 00:02:53,000 +You know, move 8 on this side, + +78 +00:02:53,000 --> 00:02:55,000 +2 on this side, and 3 on this side + +79 +00:02:55,000 --> 00:02:56,000 +so you can have a space there. + +80 +00:02:56,000 --> 00:03:00,000 +But don't you think that will consume a lot of computation? + +81 +00:03:00,000 --> 00:03:03,000 +Because shifting values is a time-consuming process. + +82 +00:03:03,000 --> 00:03:05,000 +But here that's a problem we are going to face + +83 +00:03:05,000 --> 00:03:07,000 +and let's try to solve that in some time. + +84 +00:03:07,000 --> 00:03:09,000 +But important thing is there's a concept + +85 +00:03:09,000 --> 00:03:10,000 +of enqueue, which is inserting. + +86 +00:03:10,000 --> 00:03:12,000 +Dequeue is removing the elements. + +87 +00:03:12,000 --> 00:03:15,000 +So let's say we have removed 5, now 5 is gone. + +88 +00:03:15,000 --> 00:03:17,000 +Now again, when I say dequeue on this queue, + +89 +00:03:17,000 --> 00:03:20,000 +what happens is it will go for the next element. + +90 +00:03:20,000 --> 00:03:22,000 +So the first element from that queue, + +91 +00:03:22,000 --> 00:03:24,000 +so 8 will come out. + +92 +00:03:24,000 --> 00:03:26,000 +Now remaining is only 2 and 3. + +93 +00:03:26,000 --> 00:03:27,000 +So basically every time you say dequeue, + +94 +00:03:27,000 --> 00:03:30,000 +you are reducing the size of your queue. + +95 +00:03:30,000 --> 00:03:31,000 +Every time you say enqueue, + +96 +00:03:31,000 --> 00:03:34,000 +you are increasing the size of your queue. + +97 +00:03:34,000 --> 00:03:35,000 +Now, apart from these 2 methods, + +98 +00:03:35,000 --> 00:03:39,000 +we can also use something called a peak. + +99 +00:03:39,000 --> 00:03:40,000 +Now, we have talked about peak before. + +100 +00:03:40,000 --> 00:03:44,000 +So peak, what it does is it gives you the value, + +101 +00:03:44,000 --> 00:03:47,000 +but it will not remove the value from the queue. + +102 +00:03:47,000 --> 00:03:49,000 +So example, let's say if I want to do peak now. + +103 +00:03:49,000 --> 00:03:51,000 +On this particular queue, it will give you 2, + +104 +00:03:51,000 --> 00:03:55,000 +which is the value here, but it will not remove the value. + +105 +00:03:55,000 --> 00:03:57,000 +So difference between dequeue and peak is + +106 +00:03:57,000 --> 00:03:58,000 +both will give you the value, + +107 +00:03:58,000 --> 00:04:00,000 +but dequeue will remove the value + +108 +00:04:00,000 --> 00:04:04,000 +and peak will just give you the value, okay, not remove it. + +109 +00:04:04,000 --> 00:04:06,000 +Okay, let's get back this value inside + +110 +00:04:06,000 --> 00:04:07,000 +because I want to use them. + +111 +00:04:07,000 --> 00:04:09,000 +So let's get 5 and 8. + +112 +00:04:09,000 --> 00:04:10,000 +So let's say we have a scenario + +113 +00:04:10,000 --> 00:04:11,000 +where you have all the four values. + +114 +00:04:11,000 --> 00:04:13,000 +How we are going to implement this? + +115 +00:04:13,000 --> 00:04:14,000 +How you're going to implement queue? + +116 +00:04:14,000 --> 00:04:17,000 +So what we do is we take two variables. + +117 +00:04:17,000 --> 00:04:18,000 +We have already used those two variables. + +118 +00:04:18,000 --> 00:04:20,000 +We have front and we have rear. + +119 +00:04:20,000 --> 00:04:23,000 +So basically every time you insert the value, + +120 +00:04:23,000 --> 00:04:25,000 +you increase your rear value, + +121 +00:04:25,000 --> 00:04:27,000 +which means initially the value + +122 +00:04:27,000 --> 00:04:30,000 +of rear will not be at the end. + +123 +00:04:30,000 --> 00:04:33,000 +So how do we implement this? Let's take a example here. + +124 +00:04:33,000 --> 00:04:33,000 +So let's say again + +125 +00:04:33,000 --> 00:04:37,000 +I want to insert the same values, which is 5, 8, 2 and 3. + +126 +00:04:37,000 --> 00:04:38,000 +So I want to insert these values. + +127 +00:04:38,000 --> 00:04:40,000 +So what you do is you take a queue. + +128 +00:04:40,000 --> 00:04:42,000 +Basically this is your queue here, + +129 +00:04:42,000 --> 00:04:44,000 +which will have four values. + +130 +00:04:44,000 --> 00:04:47,000 +Let me create box, and let's insert the values here. + +131 +00:04:47,000 --> 00:04:48,000 +But before inserting, + +132 +00:04:48,000 --> 00:04:50,000 +we have to take those variables, right? + +133 +00:04:50,000 --> 00:04:53,000 +So the two variables we have is we have front + +134 +00:04:53,000 --> 00:04:54,000 +and we have rear. + +135 +00:04:54,000 --> 00:04:57,000 +The value for them, by default, the front will make it 0 + +136 +00:04:57,000 --> 00:04:59,000 +and rear will be -1. + +137 +00:05:00,000 --> 00:05:05,000 +So that means rear is basically pointing somewhere here, + +138 +00:05:05,000 --> 00:05:06,000 +and front is here. + +139 +00:05:06,000 --> 00:05:08,000 +In fact, just to simplify this, + +140 +00:05:08,000 --> 00:05:10,000 +I will not be using rear or front. + +141 +00:05:10,000 --> 00:05:12,000 +I'll be using r and f. + +142 +00:05:12,000 --> 00:05:13,000 +So r will be here + +143 +00:05:13,000 --> 00:05:15,000 +and f will be here, right. + +144 +00:05:15,000 --> 00:05:18,000 +Now when you insert the first value, + +145 +00:05:18,000 --> 00:05:20,000 +so let's say if you want to insert 5, + +146 +00:05:20,000 --> 00:05:24,000 +now 5 basically comes here, we got the value 5. + +147 +00:05:24,000 --> 00:05:29,000 +Now what you do is you move your rear here, okay? + +148 +00:05:29,000 --> 00:05:32,000 +And then let's say you want to insert 8, so 8 goes here. + +149 +00:05:32,000 --> 00:05:34,000 +But don't you think even before you insert 5, + +150 +00:05:34,000 --> 00:05:35,000 +you should move your rear? + +151 +00:05:35,000 --> 00:05:38,000 +So what I mean to say is we try to make sense, + +152 +00:05:38,000 --> 00:05:40,000 +but then even before you insert 5, + +153 +00:05:40,000 --> 00:05:44,000 +because we have no idea from rear to put this, + +154 +00:05:44,000 --> 00:05:48,000 +so what you do is you basically move your rear here first. + +155 +00:05:48,000 --> 00:05:50,000 +And then you know, okay, whenever you have r, + +156 +00:05:50,000 --> 00:05:52,000 +basically you put your value there. + +157 +00:05:52,000 --> 00:05:53,000 +So 5 goes here. + +158 +00:05:53,000 --> 00:05:55,000 +Then when you want to insert 8, + +159 +00:05:55,000 --> 00:05:58,000 +again, you move your r next. + +160 +00:05:58,000 --> 00:05:59,000 +Let's put it here. + +161 +00:05:59,000 --> 00:06:01,000 +And then you say, okay, 8 goes here now. + +162 +00:06:01,000 --> 00:06:03,000 +Again, when you want to insert 2, + +163 +00:06:03,000 --> 00:06:07,000 +you basically move your rear once again here. + +164 +00:06:07,000 --> 00:06:08,000 +So whenever you want to insert value, + +165 +00:06:08,000 --> 00:06:10,000 +you move your rear there. + +166 +00:06:10,000 --> 00:06:12,000 +So basically you have to just increment rear + +167 +00:06:12,000 --> 00:06:14,000 +every time you insert the value. + +168 +00:06:14,000 --> 00:06:16,000 +Okay, this makes sense, right? + +169 +00:06:16,000 --> 00:06:19,000 +But what about the front? + +170 +00:06:19,000 --> 00:06:20,000 +Now basically when you say front, + +171 +00:06:20,000 --> 00:06:22,000 +it will be used for dequeueing. + +172 +00:06:22,000 --> 00:06:24,000 +So let's say now when you're dequeueing it, + +173 +00:06:24,000 --> 00:06:25,000 +how will you do it? + +174 +00:06:25,000 --> 00:06:26,000 +So when you say dequeue, + +175 +00:06:26,000 --> 00:06:29,000 +basically first you will remove the value, + +176 +00:06:29,000 --> 00:06:31,000 +and then you have to make sure + +177 +00:06:31,000 --> 00:06:34,000 +that you move your front here. + +178 +00:06:34,000 --> 00:06:35,000 +Why we have to move is + +179 +00:06:35,000 --> 00:06:38,000 +because now 8 is the first element, right? + +180 +00:06:38,000 --> 00:06:40,000 +So which one to remove is done with the help of front. + +181 +00:06:40,000 --> 00:06:43,000 +Where to insert, that will be handled by the rear. + +182 +00:06:43,000 --> 00:06:46,000 +Now, do you want to move rear first before? + +183 +00:06:46,000 --> 00:06:48,000 +I mean, that depends upon your implementation. + +184 +00:06:48,000 --> 00:06:51,000 +We need one more variable here, which is called size, + +185 +00:06:51,000 --> 00:06:54,000 +so to know where we are, okay? + +186 +00:06:54,000 --> 00:06:55,000 +Okay, now there's one problem as I mentioned. + +187 +00:06:55,000 --> 00:06:57,000 +Let's say if I want to insert 3, + +188 +00:06:57,000 --> 00:06:59,000 +so 3 will come here, no problem. + +189 +00:06:59,000 --> 00:07:01,000 +But let's say after that if I want to insert 9, + +190 +00:07:01,000 --> 00:07:03,000 +where will 9 go? + +191 +00:07:03,000 --> 00:07:05,000 +We do have a space here at the start. + +192 +00:07:05,000 --> 00:07:07,000 +We can insert 9 here. + +193 +00:07:07,000 --> 00:07:08,000 +But then don't you think till this point + +194 +00:07:08,000 --> 00:07:12,000 +when you inserted your 3, your r is here, + +195 +00:07:12,000 --> 00:07:14,000 +how will you make sure that r goes back here? + +196 +00:07:14,000 --> 00:07:16,000 +The only way you can do that, + +197 +00:07:16,000 --> 00:07:18,000 +because okay, first of all, why do we not do that? + +198 +00:07:18,000 --> 00:07:19,000 +Because we are using index number, right? + +199 +00:07:19,000 --> 00:07:22,000 +We have 1, 2, 3. These are the index numbers we have. + +200 +00:07:22,000 --> 00:07:23,000 +So every time you can increment, + +201 +00:07:23,000 --> 00:07:25,000 +you can say from 1 to 2, 2 to 3, + +202 +00:07:25,000 --> 00:07:27,000 +but you cannot say 3 to 0 again. + +203 +00:07:27,000 --> 00:07:29,000 +But if you can do that, the problem will be solved. + +204 +00:07:29,000 --> 00:07:33,000 +So what I want is after r goes to 3, + +205 +00:07:33,000 --> 00:07:34,000 +maybe I just want to make sure + +206 +00:07:34,000 --> 00:07:36,000 +that this comes back here, okay? + +207 +00:07:36,000 --> 00:07:37,000 +And the way you can do that + +208 +00:07:37,000 --> 00:07:40,000 +provided you should know that there's a box empty here, + +209 +00:07:40,000 --> 00:07:41,000 +that can be handled with this size variable. + +210 +00:07:41,000 --> 00:07:43,000 +This will solve that problem. + +211 +00:07:43,000 --> 00:07:46,000 +But if you can move this, if you can come back + +212 +00:07:46,000 --> 00:07:50,000 +from 3 to 0, don't you think that we are going in a circle? + +213 +00:07:50,000 --> 00:07:53,000 +And that's why there's one special type of queue, + +214 +00:07:53,000 --> 00:07:57,000 +which is called a circular queue. + +215 +00:07:57,000 --> 00:07:59,000 +Now, what is circular queue? + +216 +00:07:59,000 --> 00:08:00,000 +Again, the same thing we have done before. + +217 +00:08:00,000 --> 00:08:03,000 +So r moves basically from the last element + +218 +00:08:03,000 --> 00:08:04,000 +to the first element. + +219 +00:08:04,000 --> 00:08:06,000 +And if you want to imagine this, + +220 +00:08:06,000 --> 00:08:08,000 +maybe you can imagine a circle. + +221 +00:08:08,000 --> 00:08:11,000 +So you can say we have two circle here + +222 +00:08:11,000 --> 00:08:13,000 +and we have four values. + +223 +00:08:13,000 --> 00:08:14,000 +Okay? So we've got four values now. + +224 +00:08:14,000 --> 00:08:15,000 +So basically what we will do is + +225 +00:08:15,000 --> 00:08:17,000 +let's have an index number here. + +226 +00:08:17,000 --> 00:08:22,000 +So this is index 0, index 1, index 2, and index 3, + +227 +00:08:22,000 --> 00:08:25,000 +same index which we have used for the area on the top. + +228 +00:08:25,000 --> 00:08:27,000 +And now when you want to insert, + +229 +00:08:27,000 --> 00:08:30,000 +basically we need those variables, right, + +230 +00:08:30,000 --> 00:08:31,000 +the rear and the front. + +231 +00:08:31,000 --> 00:08:34,000 +So what we can do is I can make this as front, + +232 +00:08:34,000 --> 00:08:35,000 +and this is rear. + +233 +00:08:35,000 --> 00:08:38,000 +Both are pointing to the same value now. + +234 +00:08:38,000 --> 00:08:39,000 +So let's say, okay, + +235 +00:08:39,000 --> 00:08:41,000 +actually I'm pointing to the wrong point. + +236 +00:08:41,000 --> 00:08:43,000 +So this variable basically should point here. + +237 +00:08:43,000 --> 00:08:46,000 +So this is your front + +238 +00:08:46,000 --> 00:08:49,000 +and this is your rear, right? + +239 +00:08:49,000 --> 00:08:51,000 +And now when you want to insert the value, + +240 +00:08:51,000 --> 00:08:53,000 +the first value will insert is at the rear. + +241 +00:08:53,000 --> 00:08:56,000 +So let's say we have a 5 here. + +242 +00:08:56,000 --> 00:09:01,000 +And then let's say again you're inserting 8, 3, + +243 +00:09:03,000 --> 00:09:05,000 +and then now let's say you have also inserted 1. + +244 +00:09:05,000 --> 00:09:06,000 +I think those are the values, + +245 +00:09:06,000 --> 00:09:08,000 +doesn't matter actually, 2 or 3. + +246 +00:09:08,000 --> 00:09:10,000 +But let's say we have this value here, + +247 +00:09:10,000 --> 00:09:12,000 +and now when I say dequeue, + +248 +00:09:12,000 --> 00:09:14,000 +now from where you will remove the value? + +249 +00:09:14,000 --> 00:09:16,000 +So you will remove the value + +250 +00:09:16,000 --> 00:09:18,000 +from, okay, so when you're inserting all the value, + +251 +00:09:18,000 --> 00:09:22,000 +you have to make sure one more thing, your rear goes here + +252 +00:09:22,000 --> 00:09:23,000 +when you inserted all the values. + +253 +00:09:23,000 --> 00:09:26,000 +And now when you say you want to dequeue, + +254 +00:09:26,000 --> 00:09:28,000 +basically what we can do is + +255 +00:09:28,000 --> 00:09:32,000 +we can remove this particular 5 value from here. + +256 +00:09:32,000 --> 00:09:34,000 +That goes out. Now this is empty, right? + +257 +00:09:34,000 --> 00:09:36,000 +And of course you will move your front to the next location. + +258 +00:09:36,000 --> 00:09:39,000 +Front is here. Ignore this arrow. + +259 +00:09:39,000 --> 00:09:41,000 +I can't change the location of it. Doesn't matter. + +260 +00:09:41,000 --> 00:09:44,000 +So your front is here, but then we have a empty space here, + +261 +00:09:44,000 --> 00:09:46,000 +so we just have to make this circular. + +262 +00:09:46,000 --> 00:09:48,000 +So when you insert a new value, which is 9, + +263 +00:09:48,000 --> 00:09:49,000 +your rear goes here, + +264 +00:09:49,000 --> 00:09:52,000 +and that's where we can achieve a circular queue. + +265 +00:09:52,000 --> 00:09:54,000 +Now, basically to do this, + +266 +00:09:54,000 --> 00:09:56,000 +of course, if you want to increment the value of rear, + +267 +00:09:56,000 --> 00:09:57,000 +we can do that, right? + +268 +00:09:57,000 --> 00:10:01,000 +So we can say rear +1, it will increment the value. + +269 +00:10:01,000 --> 00:10:02,000 +But if you want to go in circle, + +270 +00:10:02,000 --> 00:10:06,000 +what we can do is we can use something called mod of 5, + +271 +00:10:06,000 --> 00:10:09,000 +or sorry, mod of size of your array, which is 4. + +272 +00:10:09,000 --> 00:10:11,000 +So you can say mod of 4. + +273 +00:10:11,000 --> 00:10:12,000 +Now what it will do is + +274 +00:10:12,000 --> 00:10:14,000 +let's say your rear value is initially 0. + +275 +00:10:14,000 --> 00:10:18,000 +So we know that 0 mod 4 is 0. + +276 +00:10:18,000 --> 00:10:19,000 +Mod is basically remainder. + +277 +00:10:19,000 --> 00:10:21,000 +1 mod 4 is 1, + +278 +00:10:21,000 --> 00:10:24,000 +2 mod 4 is 2. + +279 +00:10:24,000 --> 00:10:26,000 +And then let me just write it here. + +280 +00:10:26,000 --> 00:10:28,000 +3 mod 4 is 3. + +281 +00:10:28,000 --> 00:10:31,000 +4 mod 4 is zero. + +282 +00:10:31,000 --> 00:10:33,000 +So after 3, it will come back to 0. + +283 +00:10:33,000 --> 00:10:35,000 +That's how we can perform this thing. + +284 +00:10:35,000 --> 00:10:37,000 +We can use this thing to achieve that, + +285 +00:10:37,000 --> 00:10:39,000 +you know, the circular thing. + +286 +00:10:39,000 --> 00:10:41,000 +Now, how we are going to do it in the code? + +287 +00:10:41,000 --> 00:10:43,000 +That we'll see in the next video, + +288 +00:10:43,000 --> 00:10:46,000 +but point to remember, queue is first in, first out + +289 +00:10:46,000 --> 00:10:49,000 +and it is getting used in most of the places. + +290 +00:10:49,000 --> 00:10:52,000 +Example, let's say if you want to schedule a task. + +291 +00:10:52,000 --> 00:10:53,000 +So which task should happen first? + +292 +00:10:53,000 --> 00:10:55,000 +You know, you maintain that to-do list. + +293 +00:10:55,000 --> 00:10:56,000 +So the first task you mention, + +294 +00:10:56,000 --> 00:10:58,000 +that should be the first one to complete. + +295 +00:10:58,000 --> 00:11:00,000 +But there's multiple types as well for the queue. + +296 +00:11:00,000 --> 00:11:02,000 +There's one more thing called priority queue, + +297 +00:11:02,000 --> 00:11:05,000 +where even if you mention the task in sequence, + +298 +00:11:05,000 --> 00:11:07,000 +you can mention the priority for it. + +299 +00:11:07,000 --> 00:11:09,000 +So the highest priority element + +300 +00:11:09,000 --> 00:11:10,000 +of the task will be the first to pick it up. + +301 +00:11:10,000 --> 00:11:12,000 +But then we'll not be talking about priority queue. + +302 +00:11:12,000 --> 00:11:15,000 +Let's say we have a simple queue and a circular queue. + +303 +00:11:15,000 --> 00:11:17,000 +The implementation we'll see in the next video. + diff --git a/28 - DSA (Optional)/026 Queue Code Enqueue And Dequeue_en.srt b/28 - DSA (Optional)/026 Queue Code Enqueue And Dequeue_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..f7bed4f6578cea8d7daa0a84044164028a116a5a --- /dev/null +++ b/28 - DSA (Optional)/026 Queue Code Enqueue And Dequeue_en.srt @@ -0,0 +1,1024 @@ +1 +00:00:00,000 --> 00:00:03,000 +-: So now let's try to implement queue in Java. + +2 +00:00:03,000 --> 00:00:06,000 +Now basically we do have the inbuilt interface of queue. + +3 +00:00:06,000 --> 00:00:07,000 +We will not be using that. + +4 +00:00:07,000 --> 00:00:10,000 +We can directly create a queue class for us. + +5 +00:00:10,000 --> 00:00:14,000 +And let's say this is Queue queue = new Queue();. + +6 +00:00:14,000 --> 00:00:17,000 +But as you can see, we don't have the class. + +7 +00:00:17,000 --> 00:00:19,000 +So what we can do is we can just go back here + +8 +00:00:19,000 --> 00:00:20,000 +and say more action, + +9 +00:00:20,000 --> 00:00:23,000 +I want to create my own class called Queue + +10 +00:00:23,000 --> 00:00:24,000 +in the same package, + +11 +00:00:24,000 --> 00:00:27,000 +which should go somewhere here. + +12 +00:00:27,000 --> 00:00:29,000 +Okay, so this is your main method + +13 +00:00:29,000 --> 00:00:30,000 +and on the right hand side + +14 +00:00:30,000 --> 00:00:32,000 +you can see we have a Queue. + +15 +00:00:32,000 --> 00:00:34,000 +So in Queue, basically what we can do, + +16 +00:00:34,000 --> 00:00:35,000 +so we want to insert value, + +17 +00:00:35,000 --> 00:00:36,000 +we want to delete value. + +18 +00:00:36,000 --> 00:00:38,000 +So let's try with inserting first. + +19 +00:00:38,000 --> 00:00:39,000 +So with the help of Queue, + +20 +00:00:39,000 --> 00:00:41,000 +I should be able to add value. + +21 +00:00:41,000 --> 00:00:44,000 +So let's say I want to queue.enqueue() a value. + +22 +00:00:44,000 --> 00:00:46,000 +So of course we can use ADD or other methods, + +23 +00:00:46,000 --> 00:00:49,000 +but let's stick to the conventions that we have, + +24 +00:00:49,000 --> 00:00:49,000 +queue.enqueue(). + +25 +00:00:49,000 --> 00:00:51,000 +Enqueue basically means inserting the value. + +26 +00:00:51,000 --> 00:00:53,000 +Let's say I want to queue.enqueue(10); + +27 +00:00:53,000 --> 00:00:55,000 +but as you can see it's not working. + +28 +00:00:55,000 --> 00:00:57,000 +The reason? We don't have that method. + +29 +00:00:57,000 --> 00:00:58,000 +So let's create that. + +30 +00:00:58,000 --> 00:01:00,000 +So I would say public void enqueue(), + +31 +00:01:00,000 --> 00:01:01,000 +and it should take a parameter, + +32 +00:01:01,000 --> 00:01:03,000 +so I will say public void enqueue(int data) + +33 +00:01:03,000 --> 00:01:05,000 +and that's how we are inserting it. + +34 +00:01:05,000 --> 00:01:07,000 +So let's see, how do we define this? + +35 +00:01:07,000 --> 00:01:10,000 +Now we know that whenever you work with the queue, + +36 +00:01:10,000 --> 00:01:11,000 +we need to use certain variables, + +37 +00:01:11,000 --> 00:01:13,000 +we have talked about that in theory as well. + +38 +00:01:13,000 --> 00:01:15,000 +So the first variable we need here is, + +39 +00:01:15,000 --> 00:01:18,000 +let's say private int front;, + +40 +00:01:18,000 --> 00:01:20,000 +that's what we need. + +41 +00:01:20,000 --> 00:01:22,000 +And then private int rear;, + +42 +00:01:22,000 --> 00:01:24,000 +that's the variable we need. + +43 +00:01:24,000 --> 00:01:27,000 +And also we need to know + +44 +00:01:27,000 --> 00:01:30,000 +the current size of the array, or the queue. + +45 +00:01:30,000 --> 00:01:31,000 +So let's say private int size;, + +46 +00:01:31,000 --> 00:01:33,000 +and of course the array itself. + +47 +00:01:33,000 --> 00:01:35,000 +So let's say private int arr. + +48 +00:01:35,000 --> 00:01:36,000 +This should be an array, + +49 +00:01:36,000 --> 00:01:38,000 +so I will say private int[] arr;. + +50 +00:01:38,000 --> 00:01:41,000 +So we've got four variables and let's try to use it. + +51 +00:01:41,000 --> 00:01:44,000 +Initially, the value of front which I want is 0. + +52 +00:01:44,000 --> 00:01:47,000 +I mean there are different ways of defining these values. + +53 +00:01:47,000 --> 00:01:48,000 +We can also use constructor, + +54 +00:01:48,000 --> 00:01:50,000 +but let me just do that here itself. + +55 +00:01:50,000 --> 00:01:51,000 +Size by default will be zero, + +56 +00:01:51,000 --> 00:01:53,000 +because that's a size default, + +57 +00:01:53,000 --> 00:01:54,000 +private int size=zero;. + +58 +00:01:54,000 --> 00:01:58,000 +And then I want to create the array of size let's say 4. + +59 +00:01:58,000 --> 00:01:58,000 +So the size, + +60 +00:01:58,000 --> 00:02:00,000 +maximum size, private int[] arr = new int[4];. + +61 +00:02:00,000 --> 00:02:02,000 +And yeah, let's try to use it now. + +62 +00:02:02,000 --> 00:02:05,000 +So every time you want to insert data, + +63 +00:02:05,000 --> 00:02:06,000 +the only thing you will do is, + +64 +00:02:06,000 --> 00:02:08,000 +you will insert data in the queue, + +65 +00:02:08,000 --> 00:02:09,000 +right? + +66 +00:02:09,000 --> 00:02:10,000 +So you will say, + +67 +00:02:10,000 --> 00:02:11,000 +I'm in the array, + +68 +00:02:11,000 --> 00:02:12,000 +so you will say array, + +69 +00:02:12,000 --> 00:02:14,000 +okay, what should be the variable? + +70 +00:02:14,000 --> 00:02:15,000 +So should I insert at arr[0] = data;? + +71 +00:02:15,000 --> 00:02:16,000 +Yes, we can do that. + +72 +00:02:16,000 --> 00:02:18,000 +So I can, whatever data I receive, + +73 +00:02:18,000 --> 00:02:19,000 +I will insert that at arr[0] = data;. + +74 +00:02:19,000 --> 00:02:21,000 +But don't you think every time I add a new value, + +75 +00:02:21,000 --> 00:02:22,000 +it'll go to the same location? + +76 +00:02:22,000 --> 00:02:24,000 +So this is not a correct way. + +77 +00:02:24,000 --> 00:02:25,000 +I will use a variable called rear. + +78 +00:02:25,000 --> 00:02:28,000 +So rear will define where to insert data, okay? + +79 +00:02:28,000 --> 00:02:30,000 +Because you insert data from the rear end, + +80 +00:02:30,000 --> 00:02:32,000 +but initially private int rear=-1;. + +81 +00:02:32,000 --> 00:02:33,000 +We don't want it, + +82 +00:02:33,000 --> 00:02:36,000 +we want to insert at the 0 location. + +83 +00:02:36,000 --> 00:02:37,000 +So before you do that, + +84 +00:02:37,000 --> 00:02:39,000 +before you assign the value, + +85 +00:02:39,000 --> 00:02:41,000 +you will simply say rear++;. + +86 +00:02:41,000 --> 00:02:42,000 +Our job is done. + +87 +00:02:42,000 --> 00:02:43,000 +Okay? + +88 +00:02:43,000 --> 00:02:46,000 +And let's see if that works. + +89 +00:02:46,000 --> 00:02:48,000 +Even if it works, how will we know? + +90 +00:02:48,000 --> 00:02:49,000 +So what we will do is, + +91 +00:02:49,000 --> 00:02:50,000 +let me also create a method + +92 +00:02:50,000 --> 00:02:51,000 +called print all values. + +93 +00:02:51,000 --> 00:02:55,000 +I will say private or maybe I can say public void show(). + +94 +00:02:55,000 --> 00:02:58,000 +It should print all the values, nothing much. + +95 +00:02:58,000 --> 00:03:01,000 +Not an ideal way of printing this size, + +96 +00:03:01,000 --> 00:03:02,000 +but let's say, + +97 +00:03:02,000 --> 00:03:03,000 +let's do that. + +98 +00:03:03,000 --> 00:03:04,000 +So I would say for(int n : arr), + +99 +00:03:06,000 --> 00:03:09,000 +I want to print but not here onto the same line basically, + +100 +00:03:09,000 --> 00:03:11,000 +so I will just System.out.println(n + " "), + +101 +00:03:11,000 --> 00:03:14,000 +and at the end I want System.out.println(). + +102 +00:03:14,000 --> 00:03:15,000 +So I will do that. + +103 +00:03:15,000 --> 00:03:16,000 +And from this let me print, + +104 +00:03:16,000 --> 00:03:17,000 +not this, + +105 +00:03:17,000 --> 00:03:18,000 +I want to call the show method. + +106 +00:03:18,000 --> 00:03:20,000 +So it's a queue.show();, + +107 +00:03:20,000 --> 00:03:21,000 +it will print all the values. + +108 +00:03:21,000 --> 00:03:23,000 +Right click and run. + +109 +00:03:23,000 --> 00:03:24,000 +And you can see we got 10 0 0 0. + +110 +00:03:24,000 --> 00:03:28,000 +This is 0 0 because the size of this array is four. + +111 +00:03:28,000 --> 00:03:31,000 +So basically it will print all the values. + +112 +00:03:31,000 --> 00:03:33,000 +So maybe this is not a correct way of doing this. + +113 +00:03:33,000 --> 00:03:35,000 +What if we can use + +114 +00:03:35,000 --> 00:03:40,000 +for(int i=0;i 00:03:42,000 +Okay, so basically we want to print + +116 +00:03:42,000 --> 00:03:44,000 +til the size of the queue, + +117 +00:03:44,000 --> 00:03:46,000 +and then I will say i++. + +118 +00:03:46,000 --> 00:03:49,000 +Okay, and then I'm going to print + +119 +00:03:49,000 --> 00:03:51,000 +System.out.println(arr[i]). + +120 +00:03:51,000 --> 00:03:52,000 +So what it will do is, + +121 +00:03:52,000 --> 00:03:54,000 +it will only print those elements + +122 +00:03:54,000 --> 00:03:56,000 +which are there in the list. + +123 +00:03:56,000 --> 00:03:57,000 +If I run this code, + +124 +00:03:57,000 --> 00:03:58,000 +it's printing nothing. + +125 +00:03:58,000 --> 00:04:01,000 +Reason, when you inserted the data in the queue, + +126 +00:04:01,000 --> 00:04:04,000 +don't you think we have to also increment the size? + +127 +00:04:04,000 --> 00:04:07,000 +So now initially before running the queue.enqueue(), + +128 +00:04:07,000 --> 00:04:10,000 +the size of this queue is zero. + +129 +00:04:10,000 --> 00:04:12,000 +But then when you insert the value, + +130 +00:04:12,000 --> 00:04:13,000 +we got a new element which is one. + +131 +00:04:13,000 --> 00:04:15,000 +I mean we got 1 element which is 10. + +132 +00:04:15,000 --> 00:04:18,000 +And if I run this, you will only see one element. + +133 +00:04:18,000 --> 00:04:20,000 +Yes, if you do this two times now, + +134 +00:04:20,000 --> 00:04:22,000 +let's try to do queue.enqueue() again + +135 +00:04:22,000 --> 00:04:23,000 +with a different value. + +136 +00:04:23,000 --> 00:04:24,000 +Let's say queue.enqueue(data:20) in this case. + +137 +00:04:24,000 --> 00:04:27,000 +Now what will happen is it will print two values. + +138 +00:04:27,000 --> 00:04:30,000 +So even if your array size is four, + +139 +00:04:30,000 --> 00:04:33,000 +the queue in that array is still two. + +140 +00:04:33,000 --> 00:04:35,000 +So the size of the queue is two. + +141 +00:04:35,000 --> 00:04:38,000 +So that's how we can basically use the queue.enqueue(). + +142 +00:04:38,000 --> 00:04:39,000 +But what about dequeue? + +143 +00:04:39,000 --> 00:04:40,000 +Let's try to do that. + +144 +00:04:40,000 --> 00:04:43,000 +So what I will do is I will say public void. + +145 +00:04:43,000 --> 00:04:46,000 +Okay, the dequeue will not be void, dequeue will be int, + +146 +00:04:46,000 --> 00:04:48,000 +because you are getting the value as well. + +147 +00:04:48,000 --> 00:04:52,000 +So I will say public int dequeue() + +148 +00:04:52,000 --> 00:04:53,000 +okay, when you say dequeue, + +149 +00:04:53,000 --> 00:04:54,000 +you don't just insert the value. + +150 +00:04:54,000 --> 00:04:57,000 +You just get the first element, right? + +151 +00:04:57,000 --> 00:04:58,000 +Now of course you'll write how you will, + +152 +00:04:58,000 --> 00:05:00,000 +what you will return. + +153 +00:05:00,000 --> 00:05:01,000 +It's very simple actually. + +154 +00:05:01,000 --> 00:05:05,000 +What you return is return arr[front], + +155 +00:05:05,000 --> 00:05:06,000 +but the variable will be front, + +156 +00:05:06,000 --> 00:05:07,000 +which will be referring it. + +157 +00:05:07,000 --> 00:05:10,000 +Our job is done, we are returning the front. + +158 +00:05:10,000 --> 00:05:13,000 +And if you want to see that, what we can do is we can print + +159 +00:05:13,000 --> 00:05:16,000 +by saying System.out.println(queueu.dequeue()). + +160 +00:05:16,000 --> 00:05:17,000 +So queue.dequeue() will give you the value, + +161 +00:05:17,000 --> 00:05:19,000 +the first value from this queue. + +162 +00:05:19,000 --> 00:05:20,000 +Now if I run this, + +163 +00:05:20,000 --> 00:05:22,000 +you will see it is printing 10. + +164 +00:05:22,000 --> 00:05:24,000 +Reason, because that's the first value we have inserted. + +165 +00:05:24,000 --> 00:05:26,000 +But also when you say queue.dequeue(), + +166 +00:05:26,000 --> 00:05:28,000 +don't you think we have to remove this 20? + +167 +00:05:28,000 --> 00:05:29,000 +Or remove this 10, sorry? + +168 +00:05:29,000 --> 00:05:31,000 +So the output of this queue.show() should be only 20. + +169 +00:05:31,000 --> 00:05:32,000 +It's not working. + +170 +00:05:32,000 --> 00:05:35,000 +Okay, reason, we have not done that call in dequeue. + +171 +00:05:35,000 --> 00:05:38,000 +So when you say you want to reduce it, + +172 +00:05:38,000 --> 00:05:39,000 +so how do we reduce it? + +173 +00:05:39,000 --> 00:05:42,000 +It's very simple, you simply say front++;. + +174 +00:05:42,000 --> 00:05:44,000 +Now, when you do front++;, + +175 +00:05:44,000 --> 00:05:45,000 +what we are doing is, + +176 +00:05:45,000 --> 00:05:46,000 +when you have this added, + +177 +00:05:46,000 --> 00:05:48,000 +let me just draw that here. + +178 +00:05:48,000 --> 00:05:52,000 +So this is here basically a queue which you have created + +179 +00:05:52,000 --> 00:05:53,000 +with four values, + +180 +00:05:53,000 --> 00:05:54,000 +and in fact four, + +181 +00:05:54,000 --> 00:05:56,000 +I mean the array size is four, + +182 +00:05:56,000 --> 00:05:57,000 +but by default the queue size is zero. + +183 +00:05:57,000 --> 00:06:00,000 +But when you insert 10, the queue size becomes 1. + +184 +00:06:00,000 --> 00:06:04,000 +When you insert 20, the queue size becomes 2. + +185 +00:06:04,000 --> 00:06:05,000 +Now what we are doing is, + +186 +00:06:05,000 --> 00:06:07,000 +here at this point we are saying queue.dequeue(), + +187 +00:06:07,000 --> 00:06:11,000 +which means we have to take this 10 out. + +188 +00:06:11,000 --> 00:06:14,000 +So if you take this 10 out, the size should be only 20. + +189 +00:06:14,000 --> 00:06:17,000 +I mean size should be value 1, the value should be 20. + +190 +00:06:17,000 --> 00:06:18,000 +How do we remove this 10? + +191 +00:06:18,000 --> 00:06:21,000 +See, initially the front was here, right? + +192 +00:06:21,000 --> 00:06:22,000 +Front was here. + +193 +00:06:22,000 --> 00:06:24,000 +What if instead of removing the value, + +194 +00:06:24,000 --> 00:06:25,000 +just move your front. + +195 +00:06:25,000 --> 00:06:27,000 +If you do that your job is done, right? + +196 +00:06:27,000 --> 00:06:28,000 +And so that's what we're doing. + +197 +00:06:28,000 --> 00:06:29,000 +We are saying front++;, + +198 +00:06:29,000 --> 00:06:31,000 +but then where should we do it? + +199 +00:06:31,000 --> 00:06:32,000 +Should we do front++; before? + +200 +00:06:32,000 --> 00:06:35,000 +Of course it should be after this, right? + +201 +00:06:36,000 --> 00:06:37,000 +But then there will be confusion. + +202 +00:06:37,000 --> 00:06:38,000 +So what we can do is + +203 +00:06:38,000 --> 00:06:41,000 +we can take a temporary variable called data, + +204 +00:06:41,000 --> 00:06:44,000 +and in this data you store your value first. + +205 +00:06:44,000 --> 00:06:47,000 +So you simply say int data = arr[front]. + +206 +00:06:47,000 --> 00:06:48,000 +So you store your value in data, + +207 +00:06:48,000 --> 00:06:49,000 +then you front++; + +208 +00:06:49,000 --> 00:06:53,000 +and return data, not the array or the array element. + +209 +00:06:53,000 --> 00:06:55,000 +Because the array element is stored inside data. + +210 +00:06:55,000 --> 00:06:57,000 +Then you do front++ so that you will get data. + +211 +00:06:57,000 --> 00:06:58,000 +Now if you run this code, + +212 +00:06:58,000 --> 00:07:01,000 +you can see 10 is here, okay? + +213 +00:07:01,000 --> 00:07:02,000 +But there's one problem, + +214 +00:07:02,000 --> 00:07:03,000 +it is printing 10 as well. + +215 +00:07:03,000 --> 00:07:05,000 +Actually in queue we only have one, + +216 +00:07:05,000 --> 00:07:08,000 +but still it is printing 10 as well. + +217 +00:07:08,000 --> 00:07:12,000 +Reason, we are printing all the values of the array. + +218 +00:07:12,000 --> 00:07:13,000 +It should not start with 0, + +219 +00:07:13,000 --> 00:07:14,000 +it should start with front, + +220 +00:07:14,000 --> 00:07:16,000 +then it will make sense, right? + +221 +00:07:16,000 --> 00:07:18,000 +So run, and you can see we only got 20. + +222 +00:07:18,000 --> 00:07:20,000 +So this 10 is actually dequeue printing. + +223 +00:07:20,000 --> 00:07:22,000 +So this System.out.println(queue.dequeue()); is printing 10. + +224 +00:07:22,000 --> 00:07:23,000 +This 20 is actually printed + +225 +00:07:23,000 --> 00:07:24,000 +because of the queue.enqueue(data:20). + +226 +00:07:24,000 --> 00:07:25,000 +Let me insert some more values here + +227 +00:07:25,000 --> 00:07:28,000 +then it will make much more sense. + +228 +00:07:28,000 --> 00:07:32,000 +Let's say I'm inserting 5 and 99. + +229 +00:07:32,000 --> 00:07:34,000 +So we got four values now, + +230 +00:07:34,000 --> 00:07:37,000 +and when you say run this, this you got four values. + +231 +00:07:37,000 --> 00:07:38,000 +But now when you say queue.dequeue(), + +232 +00:07:38,000 --> 00:07:41,000 +it will remove the first value which is 10, + +233 +00:07:41,000 --> 00:07:43,000 +leaving the values 20, 5 and 9. + +234 +00:07:43,000 --> 00:07:45,000 +So that's how basically it works, right? + +235 +00:07:45,000 --> 00:07:47,000 +So we are able to queue.dequeue() as well. + +236 +00:07:47,000 --> 00:07:48,000 +But the problem is + +237 +00:07:48,000 --> 00:07:51,000 +what happens when you do queue.enqueue(), + +238 +00:07:51,000 --> 00:07:55,000 +not four times or maybe something like this. + +239 +00:07:55,000 --> 00:07:56,000 +You are inserting four values + +240 +00:07:56,000 --> 00:07:57,000 +and then you are saying queue.dequeue(). + +241 +00:07:57,000 --> 00:07:58,000 +After queue.dequeue(), + +242 +00:07:58,000 --> 00:08:00,000 +how many elements are there in the queue? + +243 +00:08:00,000 --> 00:08:02,000 +Only three, right? + +244 +00:08:02,000 --> 00:08:03,000 +You can see here, + +245 +00:08:03,000 --> 00:08:04,000 +but then that also means that + +246 +00:08:04,000 --> 00:08:07,000 +if I do queue.enqueue() once again + +247 +00:08:07,000 --> 00:08:08,000 +after doing the enqueue.dequeue(), + +248 +00:08:08,000 --> 00:08:10,000 +let's say the element which I'm adding here is 12, + +249 +00:08:10,000 --> 00:08:11,000 +it should work, right? + +250 +00:08:11,000 --> 00:08:14,000 +It should print 20 5 99 12, + +251 +00:08:14,000 --> 00:08:16,000 +but it's not working, + +252 +00:08:16,000 --> 00:08:18,000 +it has given you the error + +253 +00:08:18,000 --> 00:08:20,000 +which is java.lang.ArrayIndexOutOfBoundsException. + +254 +00:08:20,000 --> 00:08:23,000 +That means we have not implemented the circular queue. + +255 +00:08:23,000 --> 00:08:24,000 +How do we do that? + +256 +00:08:24,000 --> 00:08:27,000 +So let's try to understand that in the next video. + diff --git a/28 - DSA (Optional)/027 Circular Queue Code_en.srt b/28 - DSA (Optional)/027 Circular Queue Code_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..93904236fcc507081c6674104e82e931976dd218 --- /dev/null +++ b/28 - DSA (Optional)/027 Circular Queue Code_en.srt @@ -0,0 +1,700 @@ +1 +00:00:00,000 --> 00:00:01,000 +-: So let's try to solve this problem + +2 +00:00:01,000 --> 00:00:04,000 +of array index out of bounds exception. + +3 +00:00:04,000 --> 00:00:05,000 +So why we are getting this? + +4 +00:00:05,000 --> 00:00:08,000 +Because even if your size of your queue + +5 +00:00:08,000 --> 00:00:10,000 +is less than the size of the array, + +6 +00:00:10,000 --> 00:00:12,000 +it is still giving you that problem, right? + +7 +00:00:12,000 --> 00:00:15,000 +So we have inserted four values, that's right, + +8 +00:00:15,000 --> 00:00:16,000 +but then we have also done the dequeue. + +9 +00:00:16,000 --> 00:00:17,000 +So when you say dequeue, + +10 +00:00:17,000 --> 00:00:19,000 +the size of the queue is only three now. + +11 +00:00:19,000 --> 00:00:20,000 +I should be able to add this, + +12 +00:00:20,000 --> 00:00:23,000 +and it can only be added in a circular way, right? + +13 +00:00:23,000 --> 00:00:25,000 +So it should be added at the end. + +14 +00:00:25,000 --> 00:00:26,000 +How will you do that? + +15 +00:00:26,000 --> 00:00:30,000 +So what we can do is, when you're doing the enqueue, + +16 +00:00:30,000 --> 00:00:32,000 +instead of using rear plus plus, + +17 +00:00:32,000 --> 00:00:34,000 +we can say rear is equal to rear plus one. + +18 +00:00:34,000 --> 00:00:36,000 +but you will say that's same thing, right? + +19 +00:00:36,000 --> 00:00:37,000 +But by doing this, what we can also do is, + +20 +00:00:37,000 --> 00:00:39,000 +we can do a mod operation. + +21 +00:00:39,000 --> 00:00:41,000 +Remember in the theory we have talked about? + +22 +00:00:41,000 --> 00:00:45,000 +If I do a mod of four, our job will be done, I think. + +23 +00:00:45,000 --> 00:00:46,000 +Let's try. + +24 +00:00:46,000 --> 00:00:48,000 +Let me just rerun this, + +25 +00:00:48,000 --> 00:00:49,000 +and okay, now again we got the problem, + +26 +00:00:49,000 --> 00:00:52,000 +but this time, I think the problem is with this show, + +27 +00:00:52,000 --> 00:00:56,000 +because in show, we are not doing the mod operation, + +28 +00:00:56,000 --> 00:01:00,000 +so what I will do is, I will start from front. + +29 +00:01:00,000 --> 00:01:04,000 +I can't go to size because size is higher now. + +30 +00:01:04,000 --> 00:01:09,000 +When I do dequeue, I had to also decrease the size, right? + +31 +00:01:10,000 --> 00:01:11,000 +So let's run, + +32 +00:01:11,000 --> 00:01:13,000 +and this is actually not a good way of printing. + +33 +00:01:13,000 --> 00:01:16,000 +It will not print the way we should be getting the output, + +34 +00:01:16,000 --> 00:01:18,000 +but at least we will get the output. + +35 +00:01:18,000 --> 00:01:21,000 +Okay, we are adding 12. + +36 +00:01:22,000 --> 00:01:24,000 +It's going one less than the size. + +37 +00:01:24,000 --> 00:01:27,000 +Okay, this is not actually the right way of printing it, + +38 +00:01:27,000 --> 00:01:29,000 +so let's ignore the printing part at this point. + +39 +00:01:29,000 --> 00:01:31,000 +Let's skip this show, and let's run this. + +40 +00:01:31,000 --> 00:01:33,000 +So now you can see there's no problem, it is working. + +41 +00:01:33,000 --> 00:01:36,000 +Basically, I can do dequeue multiple times now. + +42 +00:01:36,000 --> 00:01:38,000 +Let me just do the dequeue here. + +43 +00:01:38,000 --> 00:01:40,000 +So after setting these four values, + +44 +00:01:40,000 --> 00:01:42,000 +when you say dequeue, the 10 goes, right, + +45 +00:01:42,000 --> 00:01:46,000 +but then we have 20, five, 99, and 12. + +46 +00:01:46,000 --> 00:01:49,000 +Let's try to do dequeue here, so then we will know, + +47 +00:01:49,000 --> 00:01:50,000 +what are the elements we have? + +48 +00:01:50,000 --> 00:01:53,000 +So one, two, three, and four. + +49 +00:01:53,000 --> 00:01:55,000 +So again, I'm doing dequeue four times, + +50 +00:01:55,000 --> 00:01:58,000 +run this, and we got an error. + +51 +00:01:58,000 --> 00:02:00,000 +Okay, anyway, so we got 10, we got 20, + +52 +00:02:00,000 --> 00:02:03,000 +because that was the first element, then five, then 99, + +53 +00:02:03,000 --> 00:02:05,000 +and then dequeue is giving a problem, + +54 +00:02:05,000 --> 00:02:08,000 +because what we have done is in the enqueue, right? + +55 +00:02:08,000 --> 00:02:09,000 +This thing. + +56 +00:02:09,000 --> 00:02:12,000 +The same thing need to be done in the front. + +57 +00:02:12,000 --> 00:02:17,000 +So I have to say front is equal to front plus one, + +58 +00:02:17,000 --> 00:02:20,000 +and then what if I just mod it by four? + +59 +00:02:20,000 --> 00:02:21,000 +Again, I'm using the hardware values + +60 +00:02:21,000 --> 00:02:24,000 +because I know the size of the array, + +61 +00:02:24,000 --> 00:02:25,000 +but if you want to make it dynamic, + +62 +00:02:25,000 --> 00:02:27,000 +if you want this value to be changed, + +63 +00:02:27,000 --> 00:02:30,000 +maybe you can just take one more variable called max size, + +64 +00:02:30,000 --> 00:02:31,000 +and we can put it here. + +65 +00:02:31,000 --> 00:02:33,000 +Okay, let's see if this works. + +66 +00:02:33,000 --> 00:02:35,000 +Restart or rerun, and now it is working. + +67 +00:02:35,000 --> 00:02:37,000 +You can see we were able to add the values, + +68 +00:02:37,000 --> 00:02:39,000 +and we were able to delete the values. + +69 +00:02:39,000 --> 00:02:40,000 +Yeah, so let just make it floating + +70 +00:02:40,000 --> 00:02:42,000 +so that I can see all the outputs. + +71 +00:02:42,000 --> 00:02:46,000 +Yeah, so you can see we got 10, 20, five, 99, and 12. + +72 +00:02:46,000 --> 00:02:49,000 +So we were able to go into that circular array, + +73 +00:02:49,000 --> 00:02:50,000 +and it's not like when we have added 12, + +74 +00:02:50,000 --> 00:02:52,000 +so it will be first. + +75 +00:02:52,000 --> 00:02:54,000 +Once you remove 10, then 20 becomes the first element, + +76 +00:02:54,000 --> 00:02:56,000 +and you're able to do that here. + +77 +00:02:56,000 --> 00:02:58,000 +We can actually use two more methods here + +78 +00:02:58,000 --> 00:03:00,000 +for full and empty. + +79 +00:03:00,000 --> 00:03:04,000 +If you want to check if your queue is full or it is empty, + +80 +00:03:04,000 --> 00:03:05,000 +so I can create a Boolean method + +81 +00:03:05,000 --> 00:03:07,000 +or the method which returns a Boolean value, + +82 +00:03:07,000 --> 00:03:09,000 +I would say is full. + +83 +00:03:09,000 --> 00:03:11,000 +Is it full? Let's check. + +84 +00:03:11,000 --> 00:03:12,000 +How do you know if it is full or not? + +85 +00:03:12,000 --> 00:03:13,000 +It's very simple. + +86 +00:03:13,000 --> 00:03:18,000 +If your size is equal to four, it is full, right? + +87 +00:03:19,000 --> 00:03:22,000 +And how do you know if it is empty? + +88 +00:03:22,000 --> 00:03:24,000 +In fact, you know why we should use this method? + +89 +00:03:24,000 --> 00:03:25,000 +Let me show you something. + +90 +00:03:25,000 --> 00:03:28,000 +What if I want to add one more value here? + +91 +00:03:28,000 --> 00:03:32,000 +So I'm inserting five six times and removing only once. + +92 +00:03:32,000 --> 00:03:34,000 +It should give a problem, + +93 +00:03:34,000 --> 00:03:38,000 +but unfortunately, if I rerun this, look at this. + +94 +00:03:38,000 --> 00:03:40,000 +It's not working the way I want it. + +95 +00:03:40,000 --> 00:03:41,000 +Okay, let's have a different value, + +96 +00:03:41,000 --> 00:03:43,000 +because it's giving 12 two times. + +97 +00:03:43,000 --> 00:03:47,000 +Let's rerun, and you can see this is not what expected. + +98 +00:03:47,000 --> 00:03:49,000 +Where is 20? 20 just went off. + +99 +00:03:49,000 --> 00:03:51,000 +So we have some bugs here, + +100 +00:03:51,000 --> 00:03:53,000 +so don't you think this should not work? + +101 +00:03:53,000 --> 00:03:58,000 +32 should not be added because the queue was full. + +102 +00:03:58,000 --> 00:04:01,000 +So we can implement this method, is full, + +103 +00:04:01,000 --> 00:04:06,000 +and before you do this enqueue, we can check if is full. + +104 +00:04:07,000 --> 00:04:10,000 +In fact, if it is not full, then do this. + +105 +00:04:10,000 --> 00:04:12,000 +Otherwise, don't do those things. + +106 +00:04:12,000 --> 00:04:13,000 +Else if you want to do something, + +107 +00:04:13,000 --> 00:04:16,000 +you can throw the exception, or you can print if you want, + +108 +00:04:16,000 --> 00:04:20,000 +or maybe you can say "queue is full," okay? + +109 +00:04:20,000 --> 00:04:21,000 +So now, what will happen with this is, + +110 +00:04:21,000 --> 00:04:24,000 +if you run this now, it will not add the last value. + +111 +00:04:24,000 --> 00:04:27,000 +You can say it says queue is full. + +112 +00:04:27,000 --> 00:04:29,000 +So you cannot add 32, and that's why the only elements + +113 +00:04:29,000 --> 00:04:32,000 +you have is 20, five, 99, and 12. + +114 +00:04:33,000 --> 00:04:35,000 +The same thing should be done for dequeue. + +115 +00:04:35,000 --> 00:04:37,000 +What if your array itself is empty? + +116 +00:04:37,000 --> 00:04:39,000 +Now, let's say if I want to dequeue once again here, + +117 +00:04:39,000 --> 00:04:41,000 +it should give you a problem. + +118 +00:04:41,000 --> 00:04:43,000 +You can see it will... + +119 +00:04:43,000 --> 00:04:44,000 +Okay, it's not doing anything. + +120 +00:04:44,000 --> 00:04:46,000 +It's just that it's printing 20 again. + +121 +00:04:46,000 --> 00:04:47,000 +I don't wanna do that. + +122 +00:04:47,000 --> 00:04:49,000 +I want to say the queue is empty. + +123 +00:04:49,000 --> 00:04:52,000 +So what we can do is we can have one more method here, + +124 +00:04:52,000 --> 00:04:55,000 +which is public Boolean is empty. + +125 +00:04:55,000 --> 00:04:57,000 +We can check if the size is equal to zero, + +126 +00:04:57,000 --> 00:04:59,000 +that means it's empty, and then that means, + +127 +00:04:59,000 --> 00:05:00,000 +before doing the dequeue part, + +128 +00:05:00,000 --> 00:05:04,000 +we can check if it's not empty, + +129 +00:05:04,000 --> 00:05:07,000 +then only you have to do this part, + +130 +00:05:07,000 --> 00:05:08,000 +and I should return this data. + +131 +00:05:08,000 --> 00:05:11,000 +I will take this data variable outside. + +132 +00:05:11,000 --> 00:05:13,000 +There's one more way you can do that + +133 +00:05:13,000 --> 00:05:16,000 +instead of sending dummy data, because if I do this, + +134 +00:05:16,000 --> 00:05:17,000 +it will send dummy data. + +135 +00:05:17,000 --> 00:05:19,000 +What I can also do is, if it is empty, + +136 +00:05:19,000 --> 00:05:21,000 +I can simply throw an exception, + +137 +00:05:21,000 --> 00:05:23,000 +or maybe you can also print something, + +138 +00:05:23,000 --> 00:05:25,000 +but throwing exception will also make sure + +139 +00:05:25,000 --> 00:05:27,000 +that it will stop the execution, right? + +140 +00:05:27,000 --> 00:05:32,000 +So I can print "queue is empty." + +141 +00:05:32,000 --> 00:05:33,000 +Okay, let me just move it down. So queue is empty. + +142 +00:05:33,000 --> 00:05:37,000 +Now if you run this, it will give you an error, + +143 +00:05:37,000 --> 00:05:40,000 +but the error which we have created, it says queue is empty. + +144 +00:05:40,000 --> 00:05:41,000 +Yeah, that's how basically we can do this. + +145 +00:05:41,000 --> 00:05:45,000 +We can also use the exception part here + +146 +00:05:45,000 --> 00:05:48,000 +for the queue is full if you want. + +147 +00:05:48,000 --> 00:05:50,000 +Again, that's the way we can implement. + +148 +00:05:50,000 --> 00:05:53,000 +One more, you can also use something called peek, + +149 +00:05:53,000 --> 00:05:57,000 +but peek matches with dequeue, so let me write it here. + +150 +00:05:57,000 --> 00:05:59,000 +Now, in peek, what you do is you only return the value, + +151 +00:05:59,000 --> 00:06:01,000 +but you don't decrement the value + +152 +00:06:01,000 --> 00:06:03,000 +or increment the front value. + +153 +00:06:03,000 --> 00:06:06,000 +So what you will do is you will have the same code here. + +154 +00:06:06,000 --> 00:06:10,000 +The difference is, you don't remove the size + +155 +00:06:10,000 --> 00:06:12,000 +and you don't do plus plus. + +156 +00:06:12,000 --> 00:06:14,000 +This is not there, it's just that get the data and return. + +157 +00:06:14,000 --> 00:06:16,000 +In fact, you're not performing the operation. + +158 +00:06:16,000 --> 00:06:19,000 +You can simply write the array here + +159 +00:06:19,000 --> 00:06:22,000 +so that you can reduce one line of code, + +160 +00:06:22,000 --> 00:06:24,000 +and since in the if condition, you have only one statement, + +161 +00:06:24,000 --> 00:06:26,000 +we can remove that curly brackets. + +162 +00:06:26,000 --> 00:06:27,000 +This code looks shorter. + +163 +00:06:27,000 --> 00:06:28,000 +So that's how we can use peek. + +164 +00:06:28,000 --> 00:06:31,000 +Let's see if I can use peek here. + +165 +00:06:31,000 --> 00:06:36,000 +so maybe I want to print peek plus queue.peek, + +166 +00:06:36,000 --> 00:06:38,000 +and let's see if this works. + +167 +00:06:38,000 --> 00:06:41,000 +Run, and yeah, peek was 20. + +168 +00:06:41,000 --> 00:06:42,000 +So yeah, it is not removing the value, + +169 +00:06:42,000 --> 00:06:44,000 +but it gives you the value. + +170 +00:06:44,000 --> 00:06:46,000 +So this is your queue, + +171 +00:06:46,000 --> 00:06:47,000 +so the logic which you have implemented here + +172 +00:06:47,000 --> 00:06:49,000 +is actually circular queue, okay? + +173 +00:06:49,000 --> 00:06:50,000 +So that's it from queue. + +174 +00:06:50,000 --> 00:06:53,000 +I hope you enjoyed this entire queue concept. + +175 +00:06:53,000 --> 00:06:55,000 +See you in the next topic. + diff --git a/28 - DSA (Optional)/028 Tree Data Structure_en.srt b/28 - DSA (Optional)/028 Tree Data Structure_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..64a188b45918bc4c4fbf1634a66d752645ba148b --- /dev/null +++ b/28 - DSA (Optional)/028 Tree Data Structure_en.srt @@ -0,0 +1,728 @@ +1 +00:00:00,000 --> 00:00:03,000 +-: In this video, we'll talk about tree data structure. + +2 +00:00:03,000 --> 00:00:05,000 +The thing is, we have talked about LinkedList, right? + +3 +00:00:05,000 --> 00:00:08,000 +And now we know that how do you connect different nodes? + +4 +00:00:08,000 --> 00:00:12,000 +So basically node is somewhere you will put your data + +5 +00:00:12,000 --> 00:00:14,000 +and also it'll have a reference for the next element. + +6 +00:00:14,000 --> 00:00:15,000 +So basically you can create a LinkedList, + +7 +00:00:15,000 --> 00:00:16,000 +something like this. + +8 +00:00:16,000 --> 00:00:18,000 +So this is a linear structure, right? + +9 +00:00:18,000 --> 00:00:21,000 +It's because we have everything in one line. + +10 +00:00:21,000 --> 00:00:22,000 +I mean, of course, you can change the shape of it, + +11 +00:00:22,000 --> 00:00:24,000 +but ultimately it'll be a line, right? + +12 +00:00:24,000 --> 00:00:26,000 +Referring to the next point, + +13 +00:00:26,000 --> 00:00:28,000 +the other type we have is hierarchical structure. + +14 +00:00:28,000 --> 00:00:31,000 +Now, basically there's a concept of tree. + +15 +00:00:31,000 --> 00:00:33,000 +I mean something like a real life tree, which we have. + +16 +00:00:33,000 --> 00:00:36,000 +So in this you have a root, you have a branch, + +17 +00:00:36,000 --> 00:00:38,000 +and you have leaf or leaves. + +18 +00:00:38,000 --> 00:00:41,000 +So we can do the same thing in data structure. + +19 +00:00:41,000 --> 00:00:44,000 +So basically what we have is concept of tree. + +20 +00:00:44,000 --> 00:00:46,000 +So what we do is we basically create different nodes + +21 +00:00:46,000 --> 00:00:48,000 +or nodes with tree structure. + +22 +00:00:48,000 --> 00:00:50,000 +So something like this, we have a node here. + +23 +00:00:50,000 --> 00:00:52,000 +Let's say this is node A, + +24 +00:00:52,000 --> 00:00:56,000 +and then this particular node will have sub nodes, + +25 +00:00:56,000 --> 00:00:57,000 +or we can say child nodes. + +26 +00:00:57,000 --> 00:00:59,000 +So here we can have another node, + +27 +00:00:59,000 --> 00:01:00,000 +here we can have another node. + +28 +00:01:00,000 --> 00:01:01,000 +Again, a node. + +29 +00:01:01,000 --> 00:01:04,000 +And even this can have different nodes. + +30 +00:01:04,000 --> 00:01:06,000 +One or many or zero doesn't matter. + +31 +00:01:06,000 --> 00:01:09,000 +So this is how restructure looks like. + +32 +00:01:09,000 --> 00:01:10,000 +So you can have values as well. + +33 +00:01:10,000 --> 00:01:12,000 +So let's say instead of A, I have value, + +34 +00:01:12,000 --> 00:01:17,000 +which is 5, 6, 8, 2, 1, 8 again and 3. + +35 +00:01:19,000 --> 00:01:21,000 +Now this is a tree structure, okay? + +36 +00:01:21,000 --> 00:01:23,000 +So it is not linear, it's hierarchical + +37 +00:01:23,000 --> 00:01:25,000 +because we are grad in hierarchy here. + +38 +00:01:25,000 --> 00:01:27,000 +To understand this mode, first we have to understand + +39 +00:01:27,000 --> 00:01:29,000 +are different terms, which we're going to use. + +40 +00:01:29,000 --> 00:01:32,000 +So if you see here, we have different terms, + +41 +00:01:32,000 --> 00:01:33,000 +which you have to remember. + +42 +00:01:33,000 --> 00:01:34,000 +The first term here is node. + +43 +00:01:34,000 --> 00:01:36,000 +So what exactly node is? + +44 +00:01:36,000 --> 00:01:40,000 +So every element or a part where you save data, + +45 +00:01:40,000 --> 00:01:42,000 +these are your nodes, okay? + +46 +00:01:42,000 --> 00:01:44,000 +And the same thing we have done in LinkedList as well. + +47 +00:01:44,000 --> 00:01:46,000 +Next we have something called root. + +48 +00:01:46,000 --> 00:01:49,000 +Now, every tree in real life also we have a root. + +49 +00:01:49,000 --> 00:01:54,000 +So in this structure, the node, which is on the top level, + +50 +00:01:54,000 --> 00:01:56,000 +is your root node. + +51 +00:01:56,000 --> 00:01:57,000 +So we have one more concept + +52 +00:01:57,000 --> 00:02:00,000 +or two more concepts here, which is parent and child. + +53 +00:02:00,000 --> 00:02:01,000 +So example, we have a node here. + +54 +00:02:01,000 --> 00:02:06,000 +Now this B is actually a child of A, okay? + +55 +00:02:06,000 --> 00:02:09,000 +Same goes for C, C's also child of A. + +56 +00:02:09,000 --> 00:02:12,000 +And of course, by doing this, we are saying A is a parent. + +57 +00:02:12,000 --> 00:02:13,000 +I mean, it's almost similar + +58 +00:02:13,000 --> 00:02:15,000 +to your family tree, which you create. + +59 +00:02:15,000 --> 00:02:18,000 +We have grandparents, then we have our parents, + +60 +00:02:18,000 --> 00:02:19,000 +and then the leaves goes on, right? + +61 +00:02:19,000 --> 00:02:21,000 +So same goes here, we have a parent, + +62 +00:02:21,000 --> 00:02:22,000 +and then we have a child here. + +63 +00:02:22,000 --> 00:02:25,000 +So every node will have a parent, of course, + +64 +00:02:25,000 --> 00:02:27,000 +and a node can have a child as well. + +65 +00:02:27,000 --> 00:02:29,000 +Again, it's not compulsory, but a node can have a child. + +66 +00:02:29,000 --> 00:02:32,000 +So if you talk about B, the parent of B is A. + +67 +00:02:32,000 --> 00:02:33,000 +But what about A? + +68 +00:02:33,000 --> 00:02:34,000 +Where's the parent for A? + +69 +00:02:34,000 --> 00:02:36,000 +So there's no parent for A, right? + +70 +00:02:36,000 --> 00:02:38,000 +So the nodes, which has no parent, + +71 +00:02:38,000 --> 00:02:40,000 +that's becomes your root node, + +72 +00:02:40,000 --> 00:02:41,000 +because that's where this thing starts. + +73 +00:02:41,000 --> 00:02:42,000 +So that's your root. + +74 +00:02:42,000 --> 00:02:43,000 +Next we have is edge. + +75 +00:02:43,000 --> 00:02:44,000 +What is edge? + +76 +00:02:44,000 --> 00:02:49,000 +So a line which connects your parent and a child. + +77 +00:02:49,000 --> 00:02:50,000 +It's your edge. + +78 +00:02:50,000 --> 00:02:52,000 +So this line here is edge. + +79 +00:02:52,000 --> 00:02:54,000 +So we have talked about node. + +80 +00:02:54,000 --> 00:02:56,000 +So node is something where you save data. + +81 +00:02:56,000 --> 00:02:58,000 +Root is the top most parent node. + +82 +00:02:58,000 --> 00:03:01,000 +We have edge, which connects different nodes. + +83 +00:03:01,000 --> 00:03:03,000 +We have talked about parent and child as well, + +84 +00:03:03,000 --> 00:03:05,000 +which is A is a parent node here, + +85 +00:03:05,000 --> 00:03:07,000 +because it has two nodes here. + +86 +00:03:07,000 --> 00:03:09,000 +B and C, which are child nodes. + +87 +00:03:09,000 --> 00:03:11,000 +Then we have something called a leaf node. + +88 +00:03:11,000 --> 00:03:11,000 +Now, what is leaf node? + +89 +00:03:11,000 --> 00:03:14,000 +A child node, which will be only child, + +90 +00:03:14,000 --> 00:03:15,000 +it's not a parent. + +91 +00:03:15,000 --> 00:03:16,000 +Example, if you talk about B here, + +92 +00:03:16,000 --> 00:03:18,000 +B is child, of course, of A, + +93 +00:03:18,000 --> 00:03:21,000 +but it's also a parent of D and E, right? + +94 +00:03:21,000 --> 00:03:25,000 +If you talk about D here, D is not having any child, right? + +95 +00:03:25,000 --> 00:03:27,000 +So there's no child here. + +96 +00:03:27,000 --> 00:03:28,000 +So this is called a leaf node. + +97 +00:03:28,000 --> 00:03:30,000 +Now, this is not the only leaf node we have. + +98 +00:03:30,000 --> 00:03:34,000 +So we got D, E, G, this are your leaf nodes. + +99 +00:03:34,000 --> 00:03:36,000 +Next we have a concept of depth. + +100 +00:03:36,000 --> 00:03:37,000 +So node is depth. + +101 +00:03:37,000 --> 00:03:40,000 +So let's say if you talk about any particular node, + +102 +00:03:40,000 --> 00:03:42,000 +so let's say, let's talk about E here. + +103 +00:03:42,000 --> 00:03:45,000 +Now, to reach E from the root node, + +104 +00:03:45,000 --> 00:03:47,000 +how many edges we have to pass, okay? + +105 +00:03:47,000 --> 00:03:49,000 +So example, if you want to reach E, + +106 +00:03:49,000 --> 00:03:52,000 +basically from A, we have to go to B. + +107 +00:03:52,000 --> 00:03:53,000 +From B, we have to go to E. + +108 +00:03:53,000 --> 00:03:56,000 +So if we have to do two passing, + +109 +00:03:56,000 --> 00:03:58,000 +so we have different levels here, okay? + +110 +00:03:58,000 --> 00:04:01,000 +So the root node is at zero level, then B becomes one, + +111 +00:04:01,000 --> 00:04:03,000 +and B becomes two, okay? + +112 +00:04:03,000 --> 00:04:04,000 +So that's your depth. + +113 +00:04:04,000 --> 00:04:06,000 +So depth for this E node is two. + +114 +00:04:06,000 --> 00:04:08,000 +Next we have is height. + +115 +00:04:08,000 --> 00:04:09,000 +So now we talk about height. + +116 +00:04:09,000 --> 00:04:12,000 +We're talking about the the height of a tree, okay? + +117 +00:04:12,000 --> 00:04:13,000 +So what's the height of this tree? + +118 +00:04:13,000 --> 00:04:17,000 +So we have to go till the last leaf node, + +119 +00:04:17,000 --> 00:04:19,000 +which has the highest depth. + +120 +00:04:19,000 --> 00:04:21,000 +Example, if we talk about this D, + +121 +00:04:21,000 --> 00:04:23,000 +the depth for D is two, right? + +122 +00:04:23,000 --> 00:04:25,000 +So from A to B, B to D. + +123 +00:04:25,000 --> 00:04:27,000 +What about G here? + +124 +00:04:27,000 --> 00:04:30,000 +So for G, it is from A to C, which is, + +125 +00:04:30,000 --> 00:04:34,000 +so this is depth at zero, one, two, and three. + +126 +00:04:34,000 --> 00:04:36,000 +So the height of this tree is three. + +127 +00:04:36,000 --> 00:04:39,000 +So we have to go till the highest depth + +128 +00:04:39,000 --> 00:04:42,000 +of a node in the entire tree, which is three in this case. + +129 +00:04:42,000 --> 00:04:43,000 +So that's your height. + +130 +00:04:43,000 --> 00:04:44,000 +What about the subtree? + +131 +00:04:44,000 --> 00:04:45,000 +So we have a concept of subtree. + +132 +00:04:45,000 --> 00:04:47,000 +Now of course, this entire is one tree, right? + +133 +00:04:47,000 --> 00:04:50,000 +So the root of this entire tree is A, + +134 +00:04:50,000 --> 00:04:52,000 +but then we can also have subtrees. + +135 +00:04:52,000 --> 00:04:56,000 +Example, if you talk about A, we have two childhoods, right? + +136 +00:04:56,000 --> 00:04:57,000 +We have B and C. + +137 +00:04:57,000 --> 00:04:58,000 +So let's say, let's talk about B here. + +138 +00:04:58,000 --> 00:05:03,000 +Now, if you consider this as your tree, of course, right? + +139 +00:05:03,000 --> 00:05:05,000 +This is a subtree. + +140 +00:05:05,000 --> 00:05:07,000 +Now, in this subtree, if you're focusing + +141 +00:05:07,000 --> 00:05:09,000 +only on this subtree, + +142 +00:05:09,000 --> 00:05:11,000 +B becomes your root node. + +143 +00:05:11,000 --> 00:05:13,000 +Okay, so B here is a root node, + +144 +00:05:13,000 --> 00:05:16,000 +and then these are your child, of course. + +145 +00:05:16,000 --> 00:05:19,000 +So when you look at the entire tree, of course, + +146 +00:05:19,000 --> 00:05:20,000 +A is a root node. + +147 +00:05:20,000 --> 00:05:22,000 +But if you talk about this tree, subtree, + +148 +00:05:22,000 --> 00:05:24,000 +which is B is a root node. + +149 +00:05:24,000 --> 00:05:27,000 +If you talk about this subtree, C is a root node. + +150 +00:05:27,000 --> 00:05:30,000 +If we talk about this subtree, F is a root node. + +151 +00:05:30,000 --> 00:05:32,000 +So basically if you create subtrees, + +152 +00:05:32,000 --> 00:05:34,000 +or if you consider subtrees, + +153 +00:05:34,000 --> 00:05:35,000 +then we have a different root node + +154 +00:05:35,000 --> 00:05:38,000 +compared to the entire tree, okay? + +155 +00:05:38,000 --> 00:05:40,000 +So those are the terms which we have to remember. + +156 +00:05:40,000 --> 00:05:42,000 +Now, if you observe one more thing here, + +157 +00:05:42,000 --> 00:05:44,000 +when you look at this tree, so maximum number + +158 +00:05:44,000 --> 00:05:48,000 +of child nodes we have for each node is only two. + +159 +00:05:48,000 --> 00:05:50,000 +So it's either zero, one or two. + +160 +00:05:50,000 --> 00:05:52,000 +So when you have a tree, something like this, + +161 +00:05:52,000 --> 00:05:56,000 +where you only have maximum two child nodes for each node, + +162 +00:05:56,000 --> 00:06:01,000 +we call this type of tree as a binary tree. + +163 +00:06:01,000 --> 00:06:04,000 +So it's a type of tree where the maximum number + +164 +00:06:04,000 --> 00:06:06,000 +of elements or maximum number of child node + +165 +00:06:06,000 --> 00:06:08,000 +for a particular node is two. + +166 +00:06:08,000 --> 00:06:12,000 +So of course, a child can be zero, one or two. + +167 +00:06:12,000 --> 00:06:14,000 +Example, if we talk about this A, + +168 +00:06:14,000 --> 00:06:17,000 +the number of child nodes it has is B and C, + +169 +00:06:17,000 --> 00:06:19,000 +which are two. + +170 +00:06:19,000 --> 00:06:21,000 +If we talk about F, we only have one child node. + +171 +00:06:21,000 --> 00:06:23,000 +Even for C, we have only one child node. + +172 +00:06:23,000 --> 00:06:24,000 +But if we talk about D and E, + +173 +00:06:24,000 --> 00:06:26,000 +they don't have any child nodes here, even G. + +174 +00:06:26,000 --> 00:06:27,000 +So still it's a binary tree + +175 +00:06:27,000 --> 00:06:28,000 +because the maximum number + +176 +00:06:28,000 --> 00:06:30,000 +of child nodes we have is only two. + +177 +00:06:30,000 --> 00:06:32,000 +So basically we talk about binary tree that different types + +178 +00:06:32,000 --> 00:06:35,000 +of it, and one type, which we're gonna talk about, + +179 +00:06:35,000 --> 00:06:37,000 +which is called a binary search tree. + +180 +00:06:37,000 --> 00:06:39,000 +Because the example which we're going to code + +181 +00:06:39,000 --> 00:06:40,000 +is based on this. + +182 +00:06:40,000 --> 00:06:43,000 +So we'll understand this concept in the next video. + diff --git a/28 - DSA (Optional)/029 Binary Search Tree Theory_en.srt b/28 - DSA (Optional)/029 Binary Search Tree Theory_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..e6146548fbe028a7c6a85db2616287100a24fa95 --- /dev/null +++ b/28 - DSA (Optional)/029 Binary Search Tree Theory_en.srt @@ -0,0 +1,260 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: Okay, let's talk about the binary search tree. + +2 +00:00:02,000 --> 00:00:05,000 +The thing is, when you want to search a particular element + +3 +00:00:05,000 --> 00:00:06,000 +from the entire tree, + +4 +00:00:06,000 --> 00:00:09,000 +it is better if you can save that in a binary search tree. + +5 +00:00:09,000 --> 00:00:12,000 +Now, how is different from binary tree? + +6 +00:00:12,000 --> 00:00:14,000 +The thing is, in binary tree, + +7 +00:00:14,000 --> 00:00:16,000 +there's one thing we have discussed, + +8 +00:00:16,000 --> 00:00:20,000 +which is every node will have maximum two charts, right? + +9 +00:00:20,000 --> 00:00:22,000 +But in binary search tree we have the same thing. + +10 +00:00:22,000 --> 00:00:24,000 +But additional condition is this tree. + +11 +00:00:24,000 --> 00:00:29,000 +The left sub tree will have the values less than the root + +12 +00:00:29,000 --> 00:00:32,000 +node, and on the right sub tree, + +13 +00:00:32,000 --> 00:00:34,000 +you'll be having the values greater than the root node, + +14 +00:00:34,000 --> 00:00:36,000 +something like this. + +15 +00:00:36,000 --> 00:00:38,000 +So let's say if I want to draw a tree here. + +16 +00:00:38,000 --> 00:00:40,000 +So let's say we have five, + +17 +00:00:40,000 --> 00:00:42,000 +and then on the left hand side, now we can have a smaller + +18 +00:00:42,000 --> 00:00:44,000 +value right inside we can have a bigger value. + +19 +00:00:44,000 --> 00:00:47,000 +Let's say we have five again, let's say we have a new value, + +20 +00:00:47,000 --> 00:00:52,000 +which is three, and then we can have six and then eight. + +21 +00:00:52,000 --> 00:00:53,000 +Now this is a binary tree, + +22 +00:00:53,000 --> 00:00:55,000 +but it's also a binary search tree. + +23 +00:00:55,000 --> 00:00:57,000 +Okay? So this is how it looks like. + +24 +00:00:57,000 --> 00:01:01,000 +So the left sub tree here, as you can see here, + +25 +00:01:01,000 --> 00:01:04,000 +these values are smaller than the root node + +26 +00:01:04,000 --> 00:01:06,000 +and right sub tree, + +27 +00:01:06,000 --> 00:01:08,000 +the values are bigger than the root node. + +28 +00:01:08,000 --> 00:01:11,000 +But if you consider a sub tree itself, then again, + +29 +00:01:11,000 --> 00:01:14,000 +if you can see the left sub tree of this, which is six, + +30 +00:01:14,000 --> 00:01:16,000 +is smaller than the seven, + +31 +00:01:16,000 --> 00:01:18,000 +and eight is greater than the seven. + +32 +00:01:18,000 --> 00:01:21,000 +So let's try to understand this more by drawing something + +33 +00:01:21,000 --> 00:01:22,000 +or by taking some values. + +34 +00:01:22,000 --> 00:01:24,000 +Okay, so let's say we have values something like this. + +35 +00:01:24,000 --> 00:01:29,000 +We have 8, 7, 12, 15, 2, and 5. + +36 +00:01:33,000 --> 00:01:35,000 +So let's say we have these values, + +37 +00:01:35,000 --> 00:01:37,000 +and I want to store this in a tree structure. + +38 +00:01:37,000 --> 00:01:38,000 +So how will you do it? + +39 +00:01:38,000 --> 00:01:41,000 +Initially, we'll start with the root node. + +40 +00:01:41,000 --> 00:01:44,000 +Now we are going for eight, so let's start with eight here. + +41 +00:01:44,000 --> 00:01:47,000 +So eight becomes a root node, the next is seven. + +42 +00:01:47,000 --> 00:01:48,000 +Now seven is less than eight + +43 +00:01:48,000 --> 00:01:50,000 +or greater than eight, it is less than, right? + +44 +00:01:50,000 --> 00:01:53,000 +So seven goes on this side, so seven goes here. + +45 +00:01:53,000 --> 00:01:55,000 +Okay, what about 12? + +46 +00:01:55,000 --> 00:01:58,000 +Now 12 is greater than eight. + +47 +00:01:58,000 --> 00:02:00,000 +So 12 will go here. + +48 +00:02:00,000 --> 00:02:02,000 +Next element 15. + +49 +00:02:02,000 --> 00:02:05,000 +15 is greater than eight, let's go on right hand side. + +50 +00:02:05,000 --> 00:02:07,000 +15 is also greater than 12. + +51 +00:02:07,000 --> 00:02:10,000 +So again, it'll go on the right hand side of 12, + +52 +00:02:10,000 --> 00:02:11,000 +which is 15 will come here. + +53 +00:02:11,000 --> 00:02:12,000 +Next we have two. + +54 +00:02:12,000 --> 00:02:16,000 +So two is less than eight, let's go to seven. + +55 +00:02:16,000 --> 00:02:19,000 +Two is also less than seven, so we can put two here. + +56 +00:02:19,000 --> 00:02:23,000 +Next we have is five, so five is less than eight. + +57 +00:02:23,000 --> 00:02:27,000 +Let's go on seven, five is also less than seven. + +58 +00:02:27,000 --> 00:02:31,000 +Let's go to two and five is greater than two. + +59 +00:02:31,000 --> 00:02:34,000 +So five goes on the right hand side, okay? + +60 +00:02:34,000 --> 00:02:36,000 +So when you have the element greater than the particular + +61 +00:02:36,000 --> 00:02:37,000 +element, so if we talk about + +62 +00:02:37,000 --> 00:02:39,000 +two, it goes on the right hand side. + +63 +00:02:39,000 --> 00:02:44,000 +So this is how you draw a binary search tree. + +64 +00:02:44,000 --> 00:02:45,000 +But the question is how you're going to write a code + +65 +00:02:45,000 --> 00:02:48,000 +for this, and that we'll see in the next video. + diff --git a/28 - DSA (Optional)/030 Tree Implementation_en.srt b/28 - DSA (Optional)/030 Tree Implementation_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..968df177ca37e2ef9335c59e04f9ccadc0f7973d --- /dev/null +++ b/28 - DSA (Optional)/030 Tree Implementation_en.srt @@ -0,0 +1,1960 @@ +1 +00:00:00,000 --> 00:00:04,000 +-: So let's implement the binary search tree, in Java. + +2 +00:00:04,000 --> 00:00:06,000 +Now, the thing is, we have talked about + +3 +00:00:06,000 --> 00:00:07,000 +different data structure. + +4 +00:00:07,000 --> 00:00:08,000 +One of it is linked list. + +5 +00:00:08,000 --> 00:00:10,000 +So, in linked list we have something called nodes, right? + +6 +00:00:10,000 --> 00:00:11,000 +So we connect different nodes. + +7 +00:00:11,000 --> 00:00:14,000 +And each node will be having data + +8 +00:00:14,000 --> 00:00:16,000 +and a reference for the next element. + +9 +00:00:16,000 --> 00:00:18,000 +Now, in the same way, in tree what we have, + +10 +00:00:18,000 --> 00:00:20,000 +is we have a node of course + +11 +00:00:20,000 --> 00:00:24,000 +and then each node will have two child nodes. + +12 +00:00:24,000 --> 00:00:26,000 +Now, when you talk about tree, + +13 +00:00:26,000 --> 00:00:28,000 +sometimes it is difficult to imagine + +14 +00:00:28,000 --> 00:00:30,000 +how will you code in Java? + +15 +00:00:30,000 --> 00:00:32,000 +Do we have something like one root + +16 +00:00:32,000 --> 00:00:34,000 +and multiple elements to it? + +17 +00:00:34,000 --> 00:00:36,000 +Now first of all, we don't have any Java syntax, + +18 +00:00:36,000 --> 00:00:37,000 +which will create a tree. + +19 +00:00:37,000 --> 00:00:39,000 +There might be some external classes, + +20 +00:00:39,000 --> 00:00:41,000 +but then internally, + +21 +00:00:41,000 --> 00:00:45,000 +we know that you can create a variable, or object, + +22 +00:00:45,000 --> 00:00:47,000 +but how will you create a tree structure? + +23 +00:00:47,000 --> 00:00:48,000 +Now to break it down, + +24 +00:00:48,000 --> 00:00:50,000 +if you try to understand this, + +25 +00:00:50,000 --> 00:00:53,000 +every node, so if you talk about this particular node here, + +26 +00:00:53,000 --> 00:00:55,000 +yes, that's a root node, + +27 +00:00:55,000 --> 00:00:57,000 +so do we have anything special about root node? + +28 +00:00:57,000 --> 00:00:58,000 +Not exactly. + +29 +00:00:58,000 --> 00:01:00,000 +So root node is also a node. + +30 +00:01:00,000 --> 00:01:04,000 +The difference is, the root node is not having any parent, + +31 +00:01:04,000 --> 00:01:05,000 +right? + +32 +00:01:05,000 --> 00:01:07,000 +So, that's one thing we have to remember. + +33 +00:01:07,000 --> 00:01:08,000 +Next, when you want to imagine tree, + +34 +00:01:08,000 --> 00:01:11,000 +don't imagine the entire tree in one go. + +35 +00:01:11,000 --> 00:01:14,000 +Imagine root node and root node has two child nodes. + +36 +00:01:14,000 --> 00:01:15,000 +That's it, your job is done here. + +37 +00:01:15,000 --> 00:01:16,000 +So, as for the root node, + +38 +00:01:16,000 --> 00:01:19,000 +the root node job is done by saying, + +39 +00:01:19,000 --> 00:01:19,000 +"Hey, I got two child nodes. + +40 +00:01:19,000 --> 00:01:22,000 +I don't know what is there after it." + +41 +00:01:22,000 --> 00:01:23,000 +Then if we talk about the left node, + +42 +00:01:23,000 --> 00:01:25,000 +or left child node of a root node, + +43 +00:01:25,000 --> 00:01:28,000 +it has one more node and that's it. + +44 +00:01:28,000 --> 00:01:30,000 +We don't have to worry about five or seven. + +45 +00:01:30,000 --> 00:01:32,000 +Seven only concerned about two. + +46 +00:01:32,000 --> 00:01:34,000 +Then on this side, 12 is only concerned about 15 + +47 +00:01:34,000 --> 00:01:36,000 +and then two is concerned about five. + +48 +00:01:36,000 --> 00:01:37,000 +That's it. + +49 +00:01:37,000 --> 00:01:40,000 +So, we have to focus on one node at a time. + +50 +00:01:40,000 --> 00:01:44,000 +And each node, maximum, they can have two child nodes. + +51 +00:01:44,000 --> 00:01:45,000 +Our job is done. + +52 +00:01:45,000 --> 00:01:49,000 +So what happens to those nodes which don't have any child? + +53 +00:01:49,000 --> 00:01:51,000 +In that case, both of these are null. + +54 +00:01:51,000 --> 00:01:53,000 +In this case, two, + +55 +00:01:53,000 --> 00:01:55,000 +we have only one child node, on the right-hand side. + +56 +00:01:55,000 --> 00:01:58,000 +What about the left one? + +57 +00:01:58,000 --> 00:01:59,000 +This is empty, this is null, right? + +58 +00:01:59,000 --> 00:02:02,000 +And that's how if you can implement this thing. + +59 +00:02:02,000 --> 00:02:04,000 +If you can only imagine about one node at a time, + +60 +00:02:04,000 --> 00:02:05,000 +your job is done. + +61 +00:02:05,000 --> 00:02:06,000 +So that means, if I want to code that, + +62 +00:02:06,000 --> 00:02:08,000 +first of all, we need, + +63 +00:02:08,000 --> 00:02:11,000 +for sure, we need a class called node, right? + +64 +00:02:11,000 --> 00:02:12,000 +In fact, I'm not writing this here. + +65 +00:02:12,000 --> 00:02:13,000 +Let me just remove it. + +66 +00:02:13,000 --> 00:02:16,000 +What I want is, I want, + +67 +00:02:16,000 --> 00:02:17,000 +from my main method, + +68 +00:02:17,000 --> 00:02:18,000 +basically I want to create a binary tree. + +69 +00:02:18,000 --> 00:02:20,000 +There are some in-built classes, + +70 +00:02:20,000 --> 00:02:21,000 +but we're not going to use it. + +71 +00:02:21,000 --> 00:02:22,000 +So let's say binary tree, + +72 +00:02:22,000 --> 00:02:23,000 +and we'll call this as a tree, + +73 +00:02:23,000 --> 00:02:25,000 +equal to new binary tree. + +74 +00:02:25,000 --> 00:02:28,000 +So we want to build a binary tree, okay? + +75 +00:02:28,000 --> 00:02:30,000 +You can see we have some in-built methods, + +76 +00:02:30,000 --> 00:02:31,000 +we don't want to use them. + +77 +00:02:31,000 --> 00:02:34,000 +So let's use a binary tree. We don't have this class yet. + +78 +00:02:34,000 --> 00:02:37,000 +So, what I will do is, I will just go back here + +79 +00:02:37,000 --> 00:02:39,000 +and you can see this importing this package. + +80 +00:02:39,000 --> 00:02:41,000 +We don't want any in-built things. + +81 +00:02:41,000 --> 00:02:42,000 +So if I go back here + +82 +00:02:42,000 --> 00:02:46,000 +and if I say, hey, I gimme solutions, more actions, + +83 +00:02:46,000 --> 00:02:47,000 +I want to create this class, + +84 +00:02:47,000 --> 00:02:50,000 +binary tree in the same package and you can see we got it. + +85 +00:02:50,000 --> 00:02:52,000 +I will just move this on this side. + +86 +00:02:52,000 --> 00:02:54,000 +I don't want to see the project structure. + +87 +00:02:54,000 --> 00:02:55,000 +So you can see on the left hand side we have a main, + +88 +00:02:55,000 --> 00:02:58,000 +on right hand side we have a binary tree, + +89 +00:02:58,000 --> 00:02:59,000 +so basically in this binary tree, + +90 +00:02:59,000 --> 00:03:00,000 +I want to do all those stuff. + +91 +00:03:00,000 --> 00:03:02,000 +Now when I say I wanna do stuff, + +92 +00:03:02,000 --> 00:03:05,000 +basically we have to say tree, dot. + +93 +00:03:05,000 --> 00:03:06,000 +I want to insert value in this tree. + +94 +00:03:06,000 --> 00:03:09,000 +So I can say insert and I can pass the value. + +95 +00:03:09,000 --> 00:03:10,000 +Let's say 10. + +96 +00:03:10,000 --> 00:03:11,000 +I don't know what values I'm passing here. + +97 +00:03:11,000 --> 00:03:12,000 +Or maybe I can for the same value + +98 +00:03:12,000 --> 00:03:15,000 +which we have in our notes. + +99 +00:03:15,000 --> 00:03:16,000 +So we have 8, 7, 12, 15, 2 and 5. + +100 +00:03:16,000 --> 00:03:18,000 +Let's add that. Okay. + +101 +00:03:18,000 --> 00:03:19,000 +But then unfortunately, + +102 +00:03:19,000 --> 00:03:21,000 +we don't have this insert, okay? + +103 +00:03:21,000 --> 00:03:22,000 +So, what we need to do is, + +104 +00:03:22,000 --> 00:03:23,000 +we need to create this method called insert + +105 +00:03:23,000 --> 00:03:25,000 +inside this binary tree. + +106 +00:03:25,000 --> 00:03:26,000 +And you can see we got insert method, okay? + +107 +00:03:26,000 --> 00:03:29,000 +But when you say insert, that means we have to make sure + +108 +00:03:29,000 --> 00:03:30,000 +that you're inserting a node. + +109 +00:03:30,000 --> 00:03:32,000 +Of course you're passing data here, + +110 +00:03:32,000 --> 00:03:33,000 +but it should be added to a node, right? + +111 +00:03:33,000 --> 00:03:36,000 +And we don't have a node implementation yet. + +112 +00:03:36,000 --> 00:03:39,000 +So again, create a node implementation by saying class node. + +113 +00:03:39,000 --> 00:03:41,000 +Now, what every node will have, + +114 +00:03:41,000 --> 00:03:42,000 +now if you go back to linked list, + +115 +00:03:42,000 --> 00:03:43,000 +a node will have two things, + +116 +00:03:43,000 --> 00:03:46,000 +the data and the next value. + +117 +00:03:46,000 --> 00:03:51,000 +Now, in terms of this node here, which is tree node, + +118 +00:03:51,000 --> 00:03:52,000 +it will have three things. + +119 +00:03:52,000 --> 00:03:53,000 +Data, the left node and the right node. + +120 +00:03:53,000 --> 00:03:58,000 +So I can say int data, then we have to say node + +121 +00:03:58,000 --> 00:03:59,000 +because when you say right node, left node, + +122 +00:03:59,000 --> 00:04:01,000 +they are itself nodes, right? + +123 +00:04:01,000 --> 00:04:03,000 +It's first right, left, and then right. + +124 +00:04:04,000 --> 00:04:05,000 +So yeah, so we got data, + +125 +00:04:05,000 --> 00:04:08,000 +we got left node and we got right node. + +126 +00:04:08,000 --> 00:04:11,000 +I think the only problem is, in some of the class, + +127 +00:04:11,000 --> 00:04:14,000 +I think in linked list we already have a node here. + +128 +00:04:14,000 --> 00:04:14,000 +That's the problem. + +129 +00:04:14,000 --> 00:04:16,000 +So what I will do is, + +130 +00:04:16,000 --> 00:04:17,000 +let me just move binary tree and this main, + +131 +00:04:17,000 --> 00:04:18,000 +to some other package. + +132 +00:04:18,000 --> 00:04:21,000 +I will just move the binary tree to other package. + +133 +00:04:21,000 --> 00:04:26,000 +Let's say package con.telusko.tree. + +134 +00:04:26,000 --> 00:04:28,000 +So sometime you have to solve the problems in a go. + +135 +00:04:28,000 --> 00:04:30,000 +To import the package from here now. + +136 +00:04:30,000 --> 00:04:31,000 +Importing done, + +137 +00:04:31,000 --> 00:04:32,000 +okay, so problem is, + +138 +00:04:32,000 --> 00:04:34,000 +all the problem was I was having two classes + +139 +00:04:34,000 --> 00:04:36,000 +with the same name, in the same package. + +140 +00:04:36,000 --> 00:04:38,000 +I just moved it to different package. Okay? + +141 +00:04:38,000 --> 00:04:39,000 +So now you don't have to do that + +142 +00:04:39,000 --> 00:04:40,000 +if you're just writing the binary code, + +143 +00:04:40,000 --> 00:04:42,000 +you can just skip this line. + +144 +00:04:42,000 --> 00:04:43,000 +Anyway, so we got a node + +145 +00:04:43,000 --> 00:04:45,000 +and then we are referring to left node and right node. + +146 +00:04:45,000 --> 00:04:46,000 +So that's node done. + +147 +00:04:46,000 --> 00:04:49,000 +But also what I wanna do is, maybe when I create a node, + +148 +00:04:49,000 --> 00:04:51,000 +I want to have a constructor here, + +149 +00:04:51,000 --> 00:04:55,000 +which will take the data for me and I will assign that data, + +150 +00:04:55,000 --> 00:04:59,000 +so I can say this.data, to data and that's it. + +151 +00:05:00,000 --> 00:05:02,000 +So, we are basically done with the node concept, + +152 +00:05:02,000 --> 00:05:03,000 +but how will you implement this tree? + +153 +00:05:03,000 --> 00:05:07,000 +So, every time you want to insert the data in a tree, + +154 +00:05:07,000 --> 00:05:08,000 +how will you do it? + +155 +00:05:08,000 --> 00:05:10,000 +So, if you say this is a tree, of course we don't, + +156 +00:05:10,000 --> 00:05:12,000 +let's say we don't have a tree here + +157 +00:05:12,000 --> 00:05:14,000 +and let's go with the same value. + +158 +00:05:14,000 --> 00:05:15,000 +I don't know what values we have, + +159 +00:05:15,000 --> 00:05:19,000 +I can just use this, + +160 +00:05:19,000 --> 00:05:20,000 +copy. + +161 +00:05:20,000 --> 00:05:21,000 +Okay, so we got this value. + +162 +00:05:21,000 --> 00:05:22,000 +So if I want to draw a tree, + +163 +00:05:22,000 --> 00:05:24,000 +so let's say someone is giving you the value 8 initially. + +164 +00:05:24,000 --> 00:05:26,000 +So what you do is, you create a root node. + +165 +00:05:26,000 --> 00:05:29,000 +Now we know that we don't have a node in the tree. + +166 +00:05:29,000 --> 00:05:30,000 +So basically the tree is empty. + +167 +00:05:30,000 --> 00:05:33,000 +In that case you will make this as a root node. Okay? + +168 +00:05:33,000 --> 00:05:34,000 +So how do we do that? + +169 +00:05:34,000 --> 00:05:35,000 +So when you say insert, + +170 +00:05:35,000 --> 00:05:37,000 +basically we have to create a root node. + +171 +00:05:37,000 --> 00:05:39,000 +That means every tree, + +172 +00:05:39,000 --> 00:05:41,000 +need to know about only one thing, + +173 +00:05:41,000 --> 00:05:42,000 +which is a root node, right? + +174 +00:05:42,000 --> 00:05:43,000 +As I mentioned before, + +175 +00:05:43,000 --> 00:05:44,000 +for a tree the most important thing is root + +176 +00:05:44,000 --> 00:05:46,000 +and then tree don't have to worry about other nodes. + +177 +00:05:46,000 --> 00:05:49,000 +The node root node will take care of the next two nodes. + +178 +00:05:49,000 --> 00:05:52,000 +So what I can do is, I can say node root + +179 +00:05:52,000 --> 00:05:54,000 +and then every time I create, insert, + +180 +00:05:54,000 --> 00:05:56,000 +and then imagine that this is the first time. + +181 +00:05:56,000 --> 00:05:57,000 +So what you will do is, + +182 +00:05:57,000 --> 00:05:59,000 +you will create your root node here. + +183 +00:05:59,000 --> 00:06:02,000 +So you can simply say root is equal to new node. + +184 +00:06:02,000 --> 00:06:03,000 +That's how you create a node. + +185 +00:06:03,000 --> 00:06:04,000 +Just create the object and pass the data. + +186 +00:06:04,000 --> 00:06:06,000 +And that's it, you've got your root node. + +187 +00:06:06,000 --> 00:06:08,000 +In fact, you know, instead of using this variable as i, + +188 +00:06:08,000 --> 00:06:11,000 +it should be data, it'll make much more sense. + +189 +00:06:11,000 --> 00:06:12,000 +So, now if you can see our code, + +190 +00:06:12,000 --> 00:06:13,000 +this is the thing we have done. + +191 +00:06:13,000 --> 00:06:16,000 +So, we created a root node called 8 + +192 +00:06:16,000 --> 00:06:20,000 +and that's your root node, which looks good, + +193 +00:06:20,000 --> 00:06:23,000 +but then what about if I insert one more data here? + +194 +00:06:23,000 --> 00:06:24,000 +So if I say tree insert, + +195 +00:06:24,000 --> 00:06:26,000 +I want to insert the next record, + +196 +00:06:26,000 --> 00:06:27,000 +what's the next record? It is 7. + +197 +00:06:27,000 --> 00:06:31,000 +So when I insert 7, where the 7 should go? + +198 +00:06:31,000 --> 00:06:33,000 +Now don't you think the 7 should go on this side + +199 +00:06:33,000 --> 00:06:36,000 +is because we are implementing one a search three. + +200 +00:06:36,000 --> 00:06:37,000 +Seven is less than eight, + +201 +00:06:37,000 --> 00:06:38,000 +so it should be on the left hand side. + +202 +00:06:38,000 --> 00:06:41,000 +So when I say seven, it should create a new node + +203 +00:06:41,000 --> 00:06:43,000 +and we are doing it. + +204 +00:06:43,000 --> 00:06:44,000 +You can see every time you say insert, + +205 +00:06:44,000 --> 00:06:45,000 +it creates a new node. + +206 +00:06:45,000 --> 00:06:47,000 +But don't you think this time it's not a root node, + +207 +00:06:47,000 --> 00:06:49,000 +it's a left node of the root node. + +208 +00:06:49,000 --> 00:06:50,000 +How will you do that + +209 +00:06:50,000 --> 00:06:52,000 +and how do we even know that this is not a root node? + +210 +00:06:52,000 --> 00:06:55,000 +So, in that case what we do is, + +211 +00:06:55,000 --> 00:06:59,000 +we to check if the root is null, + +212 +00:07:00,000 --> 00:07:02,000 +then you create a new node. + +213 +00:07:02,000 --> 00:07:03,000 +I mean then you create a root node. + +214 +00:07:03,000 --> 00:07:05,000 +Otherwise, you don't create root node every time. + +215 +00:07:05,000 --> 00:07:09,000 +So root node will created only once, if the root is null. + +216 +00:07:09,000 --> 00:07:11,000 +What else if it is not null? + +217 +00:07:11,000 --> 00:07:12,000 +Then we can check for something else. + +218 +00:07:12,000 --> 00:07:14,000 +Now see, if you talk about eight, + +219 +00:07:14,000 --> 00:07:15,000 +yes this satisfies it + +220 +00:07:15,000 --> 00:07:17,000 +because root is null till this point, + +221 +00:07:17,000 --> 00:07:19,000 +before exhibiting eight, + +222 +00:07:19,000 --> 00:07:20,000 +but when you insert seven, + +223 +00:07:20,000 --> 00:07:22,000 +we already have a root node, right? + +224 +00:07:22,000 --> 00:07:24,000 +Then we have to go for afls. + +225 +00:07:24,000 --> 00:07:25,000 +Now the question is, + +226 +00:07:25,000 --> 00:07:26,000 +now we know that this is not a root node. + +227 +00:07:26,000 --> 00:07:28,000 +We have to either go for left or right. + +228 +00:07:28,000 --> 00:07:31,000 +So what you do is, you basically check for data. + +229 +00:07:31,000 --> 00:07:34,000 +If the data is less than root.data, + +230 +00:07:34,000 --> 00:07:36,000 +then you go on the left-hand side. + +231 +00:07:36,000 --> 00:07:38,000 +So when you say you go for left-hand side, + +232 +00:07:38,000 --> 00:07:39,000 +what you simply do is, + +233 +00:07:39,000 --> 00:07:42,000 +you say route.left data, is equal to data. + +234 +00:07:42,000 --> 00:07:43,000 +That's what you do. + +235 +00:07:43,000 --> 00:07:44,000 +But then we have one little problem here + +236 +00:07:44,000 --> 00:07:48,000 +because see this will work for this data, okay? + +237 +00:07:48,000 --> 00:07:49,000 +So we will get new thing, + +238 +00:07:49,000 --> 00:07:52,000 +but we are not even creating a new node, okay? + +239 +00:07:52,000 --> 00:07:53,000 +So that problem is, + +240 +00:07:53,000 --> 00:07:55,000 +we need to modify this to something else. + +241 +00:07:55,000 --> 00:07:57,000 +Why something else? + +242 +00:07:57,000 --> 00:07:58,000 +Let's say we have, adding one more, + +243 +00:07:58,000 --> 00:08:01,000 +which is 12 goes here and then you add 15. + +244 +00:08:01,000 --> 00:08:05,000 +Now by our logic, if the value is greater than root node, + +245 +00:08:05,000 --> 00:08:06,000 +just add it to the right-hand side. + +246 +00:08:06,000 --> 00:08:07,000 +But tool is already there, right? + +247 +00:08:07,000 --> 00:08:10,000 +Then we have to move to 12 + +248 +00:08:10,000 --> 00:08:12,000 +and it becomes a right hand side of 12. + +249 +00:08:12,000 --> 00:08:15,000 +That means we have to jump from root to the next element + +250 +00:08:15,000 --> 00:08:18,000 +and then the next element, oh that's a equal process now. + +251 +00:08:18,000 --> 00:08:20,000 +So that means we have to add a recursion here. + +252 +00:08:20,000 --> 00:08:22,000 +That's one thing. + +253 +00:08:22,000 --> 00:08:24,000 +Second problem is, every time you insert data, + +254 +00:08:24,000 --> 00:08:25,000 +we need to get a hold on root. + +255 +00:08:26,000 --> 00:08:30,000 +You know, I think the logic here is not efficient way. + +256 +00:08:30,000 --> 00:08:31,000 +What we should be doing is, + +257 +00:08:31,000 --> 00:08:35,000 +create another method which is called, let's say insert rec. + +258 +00:08:35,000 --> 00:08:38,000 +And we'll say, this will take two things + +259 +00:08:38,000 --> 00:08:39,000 +because when you want to do recursion, + +260 +00:08:39,000 --> 00:08:42,000 +we have to get a hold on root element, or root node. + +261 +00:08:42,000 --> 00:08:46,000 +So, that means we cannot do that in this insert + +262 +00:08:46,000 --> 00:08:47,000 +because the moment you set recursion, + +263 +00:08:47,000 --> 00:08:48,000 +every time you call the method, + +264 +00:08:48,000 --> 00:08:51,000 +we need to get the hold on the node + +265 +00:08:51,000 --> 00:08:51,000 +where you're calling this. + +266 +00:08:51,000 --> 00:08:53,000 +So this will not work. + +267 +00:08:53,000 --> 00:08:54,000 +But that doesn't mean we have to remove this. + +268 +00:08:54,000 --> 00:08:56,000 +I will tell you in some time why. + +269 +00:08:56,000 --> 00:09:00,000 +So basically we have to say, node root, comma data + +270 +00:09:00,000 --> 00:09:03,000 +and here, okay, this should return, on node + +271 +00:09:03,000 --> 00:09:05,000 +because we are going for the question, + +272 +00:09:05,000 --> 00:09:06,000 +so it should return something. + +273 +00:09:06,000 --> 00:09:07,000 +And now here, instead of doing all this logic, + +274 +00:09:07,000 --> 00:09:11,000 +this logic should be a part of this, not this. + +275 +00:09:12,000 --> 00:09:14,000 +So here, we can simply say root is equal to insert record. + +276 +00:09:14,000 --> 00:09:17,000 +So every time you call insert, + +277 +00:09:17,000 --> 00:09:18,000 +it will call insert rec, + +278 +00:09:18,000 --> 00:09:22,000 +in which you're passing the root node and the data. + +279 +00:09:22,000 --> 00:09:24,000 +So when you say recursion, it'll go into recursion. + +280 +00:09:24,000 --> 00:09:27,000 +We will be using repercussion on insert rec, not on insert. + +281 +00:09:27,000 --> 00:09:30,000 +So we have to change some logic here. + +282 +00:09:30,000 --> 00:09:32,000 +So this is correct, when you say root, + +283 +00:09:32,000 --> 00:09:34,000 +I'm only concerned about this part. + +284 +00:09:34,000 --> 00:09:37,000 +So how do we implement this one? + +285 +00:09:37,000 --> 00:09:39,000 +So when I say, I want to add element on the left-hand side, + +286 +00:09:39,000 --> 00:09:40,000 +so if you have, let's say two, + +287 +00:09:40,000 --> 00:09:42,000 +now we know that two should go here, + +288 +00:09:42,000 --> 00:09:44,000 +but then we have to jump from eight to seven, seven to two. + +289 +00:09:44,000 --> 00:09:46,000 +That's because your process, right? + +290 +00:09:46,000 --> 00:09:49,000 +So what you will do is, let me un-command this now. + +291 +00:09:49,000 --> 00:09:50,000 +Now this is not how you do it. + +292 +00:09:50,000 --> 00:09:52,000 +So basically you say root.left + +293 +00:09:52,000 --> 00:09:55,000 +and then you call the function once again, + +294 +00:09:55,000 --> 00:09:57,000 +by passing the route.left + +295 +00:09:58,000 --> 00:09:59,000 +because now we need to follow the tree, right? + +296 +00:09:59,000 --> 00:10:02,000 +As I mentioned before, when you talk about the sub-trees, + +297 +00:10:02,000 --> 00:10:04,000 +if it is not eight left, + +298 +00:10:04,000 --> 00:10:08,000 +then we have to consider this sub tree. + +299 +00:10:08,000 --> 00:10:09,000 +Now this seven becomes your route. + +300 +00:10:09,000 --> 00:10:11,000 +So how will you send this as a route? + +301 +00:10:11,000 --> 00:10:13,000 +So you have to say, route.left. + +302 +00:10:13,000 --> 00:10:14,000 +Now this becomes a new route. + +303 +00:10:14,000 --> 00:10:17,000 +So when you call this function, which is insert rec here, + +304 +00:10:17,000 --> 00:10:18,000 +this becomes your new route + +305 +00:10:18,000 --> 00:10:20,000 +and then you have to pass data as well, + +306 +00:10:20,000 --> 00:10:21,000 +so you're passing data, + +307 +00:10:21,000 --> 00:10:23,000 +but what if this is not smaller? + +308 +00:10:23,000 --> 00:10:26,000 +Then you have to go on the right-hand side. + +309 +00:10:26,000 --> 00:10:27,000 +So you have to say route.data. + +310 +00:10:27,000 --> 00:10:30,000 +If data is greater than root.data, + +311 +00:10:30,000 --> 00:10:31,000 +then you do the same thing, but for the right side. + +312 +00:10:31,000 --> 00:10:33,000 +So I'll say paste here. + +313 +00:10:33,000 --> 00:10:38,000 +So, this should be right and this should be right. + +314 +00:10:39,000 --> 00:10:41,000 +And then at the end, you know your root node. + +315 +00:10:41,000 --> 00:10:43,000 +So just return root. That's it. + +316 +00:10:44,000 --> 00:10:47,000 +So this is the recursion call, which we are getting here. + +317 +00:10:47,000 --> 00:10:49,000 +So that's how basically you create a tree. + +318 +00:10:49,000 --> 00:10:50,000 +Now this is tree is created, + +319 +00:10:50,000 --> 00:10:52,000 +but how will we know that this tree is created? + +320 +00:10:52,000 --> 00:10:53,000 +We want to print also, right? + +321 +00:10:53,000 --> 00:10:56,000 +And the way you can print it, is like this. + +322 +00:10:56,000 --> 00:10:59,000 +So if you want to print this tree, + +323 +00:10:59,000 --> 00:11:00,000 +so let's say we have this tree here + +324 +00:11:00,000 --> 00:11:01,000 +and this tree is created + +325 +00:11:01,000 --> 00:11:03,000 +by the way we have defined this example here, + +326 +00:11:03,000 --> 00:11:05,000 +how will you print this? + +327 +00:11:05,000 --> 00:11:07,000 +Now to print, we basically have to traverse, + +328 +00:11:07,000 --> 00:11:11,000 +or we have to travel, to different elements here. + +329 +00:11:11,000 --> 00:11:12,000 +The way you do that, + +330 +00:11:12,000 --> 00:11:13,000 +is we have different traversal for tree. + +331 +00:11:13,000 --> 00:11:16,000 +So when you talk about tree traversal, + +332 +00:11:16,000 --> 00:11:18,000 +we have to follow certain things. + +333 +00:11:18,000 --> 00:11:21,000 +First, there are three options here. + +334 +00:11:21,000 --> 00:11:23,000 +We have in order traverse cell, + +335 +00:11:23,000 --> 00:11:25,000 +we have pre-order traverse cell + +336 +00:11:27,000 --> 00:11:28,000 +and we have post order. + +337 +00:11:29,000 --> 00:11:30,000 +The difference between these three, + +338 +00:11:30,000 --> 00:11:33,000 +is the way you print your root node, okay? + +339 +00:11:33,000 --> 00:11:35,000 +So when you talk about, in order, + +340 +00:11:35,000 --> 00:11:36,000 +basically what you do is, + +341 +00:11:36,000 --> 00:11:37,000 +you first go to the left, then you go to the root, + +342 +00:11:37,000 --> 00:11:39,000 +then you go to the right. + +343 +00:11:39,000 --> 00:11:41,000 +So, which is 7, 8, 12. + +344 +00:11:41,000 --> 00:11:42,000 +When you talk about pre-order, + +345 +00:11:42,000 --> 00:11:44,000 +here you first go to the root, which is eight, + +346 +00:11:44,000 --> 00:11:46,000 +so it'll print eight, + +347 +00:11:46,000 --> 00:11:49,000 +then seven which is left, and then 12, which is right. + +348 +00:11:49,000 --> 00:11:53,000 +When you talk about post, it means first it'll go for left, + +349 +00:11:53,000 --> 00:11:57,000 +then right, then root, which is seven, 12, and eight. + +350 +00:11:57,000 --> 00:11:59,000 +Okay? That's your post order driver. + +351 +00:12:00,000 --> 00:12:01,000 +Here we are going to go for, in order. + +352 +00:12:01,000 --> 00:12:04,000 +So in order simply means, first you print the left one, + +353 +00:12:04,000 --> 00:12:08,000 +then you print the, or not exactly printing, + +354 +00:12:08,000 --> 00:12:11,000 +but you go to the left one, then you go to the root node + +355 +00:12:12,000 --> 00:12:13,000 +and then you go for the right one. + +356 +00:12:13,000 --> 00:12:13,000 +So if that's your, in order. + +357 +00:12:13,000 --> 00:12:15,000 +Now the way you can do that, + +358 +00:12:15,000 --> 00:12:16,000 +of course this is also recursive, right? + +359 +00:12:16,000 --> 00:12:18,000 +So in order to do that, + +360 +00:12:18,000 --> 00:12:21,000 +what I will do is I will create a function called public, + +361 +00:12:21,000 --> 00:12:23,000 +white, in order. + +362 +00:12:23,000 --> 00:12:24,000 +Then you can work here. + +363 +00:12:24,000 --> 00:12:26,000 +But then since we know that in order, + +364 +00:12:26,000 --> 00:12:28,000 +is actually our traverse, + +365 +00:12:28,000 --> 00:12:30,000 +is basically a recursive operation + +366 +00:12:30,000 --> 00:12:32,000 +where you go to each sub tree and print it, + +367 +00:12:32,000 --> 00:12:33,000 +or do some operations. + +368 +00:12:33,000 --> 00:12:33,000 +For the recursion, + +369 +00:12:33,000 --> 00:12:35,000 +I will create another method which is public, + +370 +00:12:35,000 --> 00:12:37,000 +void is because I don't want to return anything. + +371 +00:12:37,000 --> 00:12:39,000 +So I'll say in order, rec. + +372 +00:12:41,000 --> 00:12:42,000 +So here we just want to print it. + +373 +00:12:42,000 --> 00:12:43,000 +Now when you say print it, + +374 +00:12:43,000 --> 00:12:46,000 +how exactly we are going to print, + +375 +00:12:46,000 --> 00:12:47,000 +how we are going to travel this? + +376 +00:12:47,000 --> 00:12:48,000 +Of course the output will not be similar + +377 +00:12:48,000 --> 00:12:49,000 +to the value we have inserted. + +378 +00:12:49,000 --> 00:12:50,000 +It'll have a different order. + +379 +00:12:50,000 --> 00:12:53,000 +So what it'll do is, first it'll go for, + +380 +00:12:53,000 --> 00:12:55,000 +it'll print the left one, right? + +381 +00:12:55,000 --> 00:12:57,000 +So when you start from here, it goes to eight, + +382 +00:12:57,000 --> 00:12:59,000 +but then we cannot work on the eight + +383 +00:12:59,000 --> 00:13:00,000 +because that's an in order. + +384 +00:13:00,000 --> 00:13:01,000 +Then you go to the left one + +385 +00:13:01,000 --> 00:13:03,000 +because in order you first go for left-hand side, + +386 +00:13:03,000 --> 00:13:05,000 +left sub-tree, + +387 +00:13:05,000 --> 00:13:06,000 +and then from seven also you can't print seven + +388 +00:13:06,000 --> 00:13:09,000 +because seven becomes a root node for the two. + +389 +00:13:09,000 --> 00:13:10,000 +So we have to go to two. + +390 +00:13:10,000 --> 00:13:12,000 +Now we don't have any left here, right? This is empty. + +391 +00:13:12,000 --> 00:13:16,000 +So in this case, you will first print two, okay? + +392 +00:13:16,000 --> 00:13:19,000 +So you will go for two, but then you go for, right, + +393 +00:13:19,000 --> 00:13:21,000 +since we don't have a left, we print the root node + +394 +00:13:21,000 --> 00:13:23,000 +because we are considering this sub three now. + +395 +00:13:23,000 --> 00:13:26,000 +So you'll print two and then you will print five. + +396 +00:13:26,000 --> 00:13:30,000 +So here we have five, then you go up, + +397 +00:13:30,000 --> 00:13:32,000 +then you print seven because we don't have anything. + +398 +00:13:32,000 --> 00:13:35,000 +So that's apparent for two. So we'll go for seven. + +399 +00:13:35,000 --> 00:13:36,000 +Then we print on the right-hand side, + +400 +00:13:36,000 --> 00:13:37,000 +we don't have anything right-hand inside, right? + +401 +00:13:37,000 --> 00:13:38,000 +So this is empty. + +402 +00:13:38,000 --> 00:13:39,000 +Then you go up, you print eight, + +403 +00:13:39,000 --> 00:13:41,000 +then you go for the right-hand side. + +404 +00:13:41,000 --> 00:13:44,000 +Right-hand side is 12, on 12 left, we don't have anything. + +405 +00:13:44,000 --> 00:13:46,000 +So it'll not print anything, but you can print 12 + +406 +00:13:46,000 --> 00:13:49,000 +and then you can go for right-hand side, which is 15. + +407 +00:13:49,000 --> 00:13:52,000 +And if you observe, you got assorted values. + +408 +00:13:52,000 --> 00:13:55,000 +So that's why we use BST, which is sorted values, + +409 +00:13:55,000 --> 00:13:56,000 +which is is easier to search as well. + +410 +00:13:56,000 --> 00:13:59,000 +Okay? This is the in order traversal. + +411 +00:13:59,000 --> 00:14:02,000 +Okay, so let's implement this. So how do we do it? + +412 +00:14:02,000 --> 00:14:04,000 +So first we'll start from the left, left-hand side, + +413 +00:14:04,000 --> 00:14:06,000 +but also we want to verify if the tree is empty. + +414 +00:14:06,000 --> 00:14:07,000 +If the tree is empty, how will you print something? + +415 +00:14:07,000 --> 00:14:09,000 +How will you travel? + +416 +00:14:09,000 --> 00:14:10,000 +And how do you know the tree is empty? + +417 +00:14:10,000 --> 00:14:11,000 +By using the root element. + +418 +00:14:11,000 --> 00:14:15,000 +So we can simply check, if root, + +419 +00:14:15,000 --> 00:14:16,000 +but how do you know the root? + +420 +00:14:16,000 --> 00:14:18,000 +Because root will keep changing depending on tree, right? + +421 +00:14:18,000 --> 00:14:20,000 +So we have to also accept root here. + +422 +00:14:20,000 --> 00:14:22,000 +So it says node is root + +423 +00:14:22,000 --> 00:14:26,000 +and then you check, if the root is not equal to null, + +424 +00:14:26,000 --> 00:14:27,000 +then only you do it. + +425 +00:14:27,000 --> 00:14:28,000 +Now how will you do it? + +426 +00:14:28,000 --> 00:14:30,000 +Now first of all, this is a recusal function, right? + +427 +00:14:30,000 --> 00:14:32,000 +So basically you have to call itself. + +428 +00:14:32,000 --> 00:14:33,000 +So we'll say in order rec, + +429 +00:14:33,000 --> 00:14:35,000 +and then you have to start from the left-hand side. + +430 +00:14:35,000 --> 00:14:38,000 +So always start from left and then keep going + +431 +00:14:38,000 --> 00:14:40,000 +till you find the last left element, okay? + +432 +00:14:40,000 --> 00:14:42,000 +And then once you do that, + +433 +00:14:42,000 --> 00:14:44,000 +then you have to go for the middle one. + +434 +00:14:44,000 --> 00:14:46,000 +Middle one is route itself, which we got here, right? + +435 +00:14:46,000 --> 00:14:51,000 +So we can print this value here. So I will say print. + +436 +00:14:51,000 --> 00:14:54,000 +And if you observe for every node, for every sub-tree, + +437 +00:14:54,000 --> 00:14:55,000 +there's always a root node. + +438 +00:14:55,000 --> 00:14:57,000 +If you observe, for eight, seven is left, right? + +439 +00:14:57,000 --> 00:15:01,000 +But don't you think seven in this sub-tree is a root node? + +440 +00:15:01,000 --> 00:15:02,000 +So that's the root we are printing. + +441 +00:15:02,000 --> 00:15:05,000 +When you go to two, two becomes a root node, right? + +442 +00:15:05,000 --> 00:15:07,000 +When you go five, five becomes a root node for their child, + +443 +00:15:07,000 --> 00:15:08,000 +which we don't have. + +444 +00:15:08,000 --> 00:15:09,000 +Then we can, so we can print data here. + +445 +00:15:09,000 --> 00:15:10,000 +So we can say root.data + +446 +00:15:10,000 --> 00:15:13,000 +and then we can print a space in between as well, + +447 +00:15:13,000 --> 00:15:15,000 +just to differentiate the values. + +448 +00:15:15,000 --> 00:15:19,000 +And also I can skip the new line. + +449 +00:15:19,000 --> 00:15:21,000 +Now, once you are done with the root element, + +450 +00:15:21,000 --> 00:15:23,000 +of course not just printing, + +451 +00:15:23,000 --> 00:15:24,000 +you can do some other operations as well. + +452 +00:15:24,000 --> 00:15:25,000 +Maybe you can add all the values if you want. + +453 +00:15:25,000 --> 00:15:29,000 +And then here, I can say in order, + +454 +00:15:30,000 --> 00:15:32,000 +but this time you will go for root.right. + +455 +00:15:33,000 --> 00:15:34,000 +That's it. + +456 +00:15:34,000 --> 00:15:36,000 +This is how basically you print the values, + +457 +00:15:36,000 --> 00:15:41,000 +but from in order we have to call in order rec, + +458 +00:15:41,000 --> 00:15:43,000 +by passing the root note. + +459 +00:15:43,000 --> 00:15:45,000 +So you will call this only once, in order. + +460 +00:15:45,000 --> 00:15:48,000 +So that means I can simply create a tree + +461 +00:15:49,000 --> 00:15:50,000 +by adding values here first of all. + +462 +00:15:50,000 --> 00:15:53,000 +Now what values we have, let me just do that quickly. + +463 +00:15:53,000 --> 00:15:55,000 +Okay, so you can see we have added all the values + +464 +00:15:55,000 --> 00:15:56,000 +and now it's time for traversal, + +465 +00:15:56,000 --> 00:16:01,000 +so I'll just print in order and it should do the job for us. + +466 +00:16:01,000 --> 00:16:02,000 +Let's try. + +467 +00:16:02,000 --> 00:16:03,000 +So I'll just run this + +468 +00:16:04,000 --> 00:16:06,000 +and you can see we got the values. + +469 +00:16:06,000 --> 00:16:09,000 +We got 2, 5, 7, 8, 12, and 15, + +470 +00:16:09,000 --> 00:16:11,000 +that's what expected, it is printing in order. + +471 +00:16:11,000 --> 00:16:14,000 +But let's say if you want to do post order. + +472 +00:16:14,000 --> 00:16:16,000 +So what I can do is I can just change their names, + +473 +00:16:16,000 --> 00:16:18,000 +or maybe I can say pre-order. + +474 +00:16:18,000 --> 00:16:21,000 +So I can say pre-order. + +475 +00:16:21,000 --> 00:16:23,000 +And of course everywhere we have to do that. + +476 +00:16:23,000 --> 00:16:24,000 +Now the only change you have to make, + +477 +00:16:24,000 --> 00:16:27,000 +is in pre-order, first you print the root. + +478 +00:16:27,000 --> 00:16:31,000 +So that means this line will go up. + +479 +00:16:32,000 --> 00:16:33,000 +That's the only change you have to make in pre order. + +480 +00:16:33,000 --> 00:16:36,000 +Okay, nothing much. + +481 +00:16:36,000 --> 00:16:37,000 +Let's run this and see if this works. + +482 +00:16:37,000 --> 00:16:40,000 +Save this code and run. + +483 +00:16:40,000 --> 00:16:42,000 +And you can see this is now pre order. + +484 +00:16:42,000 --> 00:16:44,000 +So root element is something which will get trained first. + +485 +00:16:44,000 --> 00:16:47,000 +I think this is a sequence in which we have added, + +486 +00:16:47,000 --> 00:16:48,000 +no, a different sequence. + +487 +00:16:48,000 --> 00:16:49,000 +But anyway, you got the point. + +488 +00:16:49,000 --> 00:16:52,000 +So this is pre order. You can also do it post order. + +489 +00:16:52,000 --> 00:16:53,000 +Let me in the comment section if you have done that. + +490 +00:16:53,000 --> 00:16:56,000 +And yeah, that's how you implement tree. +