Skip to playerSkip to main content
  • 9 minutes ago
Transcript
00:00I have already written my array, with that I have a function called print array which will print my array.
00:06Let's write a recursive function, the name is public static, the return type is void and the name is quicksort.
00:14In the quicksort, I will have my array, what will be my starting index and my ending index.
00:20In the main function, I know that if I call the quicksort, my starting index will be 0 and my
00:25ending index will be as my array length-1.
00:31What was the step 1 in my quicksort?
00:33The step 1 was to find out the pivot.
00:35So I have found out the pivot as my last element.
00:40Now I have to partition on the basis of pivot.
00:43I believe that I have a different function that gives me partition.
00:46In this I will pass my array, starting index and ending index.
00:52I assume that partition function will be my magic.
00:56What will I do?
00:57Partition function will take pivot.
00:59First I will take small elements and then I will take big elements.
01:03I will take pivot to the right place and give me the right index.
01:06Partition function will give me the pivot index.
01:10I will make an integer variable, p-index, pivot-index,
01:14which will give me partition function.
01:16How will partition function?
01:18Partition function will be something like this.
01:19Public static void, partition, int array.
01:24I have a starting index and ending index.
01:27And here I will put return type in int.
01:32So this is my partition.
01:34Now I will do the third step.
01:36I will call quicksort for array, starting index and partition index minus 1.
01:44Which is the partition in the index?
01:47The pivot is on the right index.
01:49What should I do?
01:49I have to sort the left part.
01:51So I will take pivot index minus 1 to the left part.
01:56Where is my right part?
01:57Where is my pivot index?
01:58The right part will take pivot index plus 1 to my ending index.
02:03The right part will take pivot index plus 1 to my ending index.
02:06This will be my right part.
02:08What will be the base case?
02:10The base case is that if the starting index value is greater than or equal to ending index.
02:14In that case, I have to simply return.
02:17So this is my whole recursion.
02:19Now I have to solve my most important problem which is to write the partition function.
02:23What will be the partition function?
02:24First of all, we will take pivot.
02:26What will be the pivot?
02:27The pivot will be my array last element.
02:31That is ending index.
02:32Now what will I do?
02:34I will make two variables.
02:35One is my i.
02:36I will initialize the starting index minus 1.
02:38This i will make a place to make place for elements smaller than pivot.
02:51What will I do?
02:52I will start a loop.
02:54The loop will start from j is equal to 0.
02:56Where will the loop go?
02:57The value is less than ending index.
02:59I mean, the pivot will be less than values.
03:02J++.
03:03I will compare every time.
03:06My array of j is less than or equal to my pivot.
03:11If it is less than or equal to, then I have to make a place.
03:14And how do I make a place?
03:15First we do iq++.
03:17Then we do swap.
03:18How do we do a temporary variable?
03:20We will store array of j to the value of j to the array of j.
03:24Then I store array of j i to the array of j to the value of j.
03:28After that we store our temporary variable of j.
03:33I have to compare the current elements, which I have to be less than a current element.
03:36If it is less then I need to make a place.
03:38Then I will swap to I++ and then I will swap.
03:42I mean the value of I and the value of J is both swap.
03:46I have analyzed all the elements which are small from my pivot.
03:50Now let's put the pivot to the right place.
03:53I have to do this work.
03:54I have to do this last one.
03:56I will do the same work here.
03:59I++.
04:01Then I will swap the logic.
04:04What will I store here?
04:06I will store the pivot value here.
04:09What is the pivot?
04:10The pivot is my array of ending index.
04:12I will store the array of I.
04:15The array of I is temporary.
04:17What is the right index?
04:18The pivot is the right index of I.
04:20I have stored the pivot in I.
04:23The pivot is the right index of I which will return the partition function.
04:28Who will return it?
04:29My quicksort function.
04:30And here we have to focus on the wrong way.
04:34I don't want to write the pivot is equal to array of I.
04:37Why?
04:37Because if I write the pivot, it is a variable.
04:39I have to change the array in index.
04:42I have to change the array of variables.
04:43It does not reflect the change of variables.
04:44Because there is a call by value in the functions.
04:46Call by reference is indirectly implemented in Java.
04:49How is it?
04:50Only in the arrays.
04:51There are objects in here.
04:53But here we are talking about array.
04:54So I have to change the array of index through the pivot index.
04:58I have to change the array of pivot variables.
05:00So my partition function is complete.
05:02This is my quicksort is complete.
05:04Now I put the main function to the call from the main function.
05:07Then I put the array of print array.
05:09Let's do it.
05:10Save and run.
05:11Where is the error?
05:12The error came in line number 29.
05:1529 is the error here.
05:18What error came in J.
05:19What error came in J?
05:21I started this loop from 0.
05:23But I should start from 0.
05:25I should start from 0.
05:25I should start from starting index.
05:27Because the problem is when it is divided.
05:29So what will happen?
05:30My starting index is 0, 1, 5.
05:34So it will always start from 0.
05:35It will start from starting index.
05:36Let's do it.
05:37Save.
05:38Let's run again.
05:40Now what happened?
05:41My final array is sorted.
05:44This array has completely sorted.
05:46My quicksort has been sorted.
05:48And we also know that if there are exceptions and errors,
05:51then how do we find out?
05:53I had an exception that my index is out of bounds.
05:57The size of my array is more than I am accessing the index.
06:00So I have seen that J is equal to 0 was wrong.
06:03The starting index should start from starting index.
06:05In this way, there is also a line number.
06:07If we are coding in this code or not,
06:09if we are coding in any other code,
06:10then many editors tell us a line number.
06:12Where is the error?
06:13I have also done an even length here.
06:15I have done an even length here.
06:16If you think that it was minus 5,
06:17then how do the answer for odd length?
06:19I have done an even length for odd length.
06:21I have done a quicksort.
06:22Sort.
06:22I have done a quicksort.
06:22I have done an even length for odd length.

Recommended