MHB Function of Partition: Understanding Array Partitioning Process

AI Thread Summary
The discussion centers on understanding the function of the partition algorithm in sorting an array using pseudocode. Participants emphasize the importance of tracing the algorithm step-by-step, noting the values of variables i and j, and the state of the array at each step. They conclude that elements smaller than the pivot (x) will end up on the left, while larger elements will be on the right, with x positioned correctly in between. There is a consensus on the need to include all cases during the tracing process, including scenarios where elements are equal to x. The conversation also touches on the time complexity of the partition function, with participants agreeing that it operates in Θ(n) time due to the linear execution of the for loop. They discuss the implications of calling the partition function on a single element and clarify that the algorithm's efficiency is based on the length of the section of the array being processed. The importance of precise language in explaining the algorithm's behavior is highlighted, ensuring clarity in communication.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Wave)

The following pseudocode is given:

Code:
partition(A,p,r){
  x<-A[r]
  i<-p-1
  for j<-p to r-1
       if A[j]<=x then
          i<-i+1
          swap(A[i],A[j])
  swap(A[i+1],A[r])
  return i+1
I have to describe the function of [m] partition [/m] at the array: $A= \langle 13, 19, 9, 5, 12, 8, 7, 4, 21, 2, 6, 11 \rangle$.I found the following:
View attachment 4066

So the final form of the array is:View attachment 4067

But how could I describe the function of [m] partition [/m] at the array? (Thinking)
 

Attachments

  • array5.png
    array5.png
    4.6 KB · Views: 121
  • end.png
    end.png
    1.5 KB · Views: 110
Technology news on Phys.org
I have to make a step-by-step trace and write at each step what the algorithm does.So do I have to write at each step the value that [m] i [/m] gets and the swap that is done?
 
evinda said:
I have to make a step-by-step trace and write at each step what the algorithm does.

So do I have to write at each step the value that [m] i [/m] gets and the swap that is done?

I suggest writing at each step the value of [m]i[/m], [m]j[/m], and the full contents of [m]A[/m]. (Wasntme)
 
I like Serena said:
I suggest writing at each step the value of [m]i[/m], [m]j[/m], and the full contents of [m]A[/m]. (Wasntme)

So should I do it in the way as follows? (Thinking)

View attachment 4078

Or should I write it in an other way?
 

Attachments

  • look.png
    look.png
    12.1 KB · Views: 107
evinda said:
So should I do it in the way as follows? (Thinking)

Or should I write it in an other way?

Looks good. (Smile)

Do you already have an idea what is happening? (Thinking)

Where do the elements that are smaller than $x$ end up? (Wondering)
 
I like Serena said:
Looks good. (Smile)

Do you already have an idea what is happening? (Thinking)

Where do the elements that are smaller than $x$ end up? (Wondering)

$x$ ends up at its right position and at its left there will be all the elements that are smaller than $x$ and at its right there will be all the elements that are greater than $x$, right?So after drawing 11 times the array, should I write this conclusion? (Wasntme)
 
evinda said:
$x$ ends up at its right position and at its left there will be all the elements that are smaller than $x$ and at its right there will be all the elements that are greater than $x$, right?

Yep.

So after drawing 11 times the array, should I write this conclusion? (Wasntme)

If it were me, I'd skip a couple of steps, since I'd already know what was happening.
And then I'd write down the last 2 steps or so, followed by the conclusion. (Wasntme)
 
I like Serena said:
Yep.
Nice!
I like Serena said:
If it were me, I'd skip a couple of steps, since I'd already know what was happening.
And then I'd write down the last 2 steps or so, followed by the conclusion. (Wasntme)

So could I for example write the first three steps and then the last two ones? (Thinking)
 
evinda said:
Nice!So could I for example write the first three steps and then the last two ones? (Thinking)

Sounds good. (Smile)

The important part is that all cases are included.
That is a case that a number is less than x and also a case that a number is more than x. (Nerd)

Btw, what would happen if one of the numbers were equal to x? (Wondering)
 
  • #10
I like Serena said:
Sounds good. (Smile)

The important part is that all cases are included.
That is a case that a number is less than x and also a case that a number is more than x. (Nerd)

A ok... (Nerd)

I like Serena said:
Btw, what would happen if one of the numbers were equal to x? (Wondering)

Then [m]i[/m] would be incremented and we would make a swap, right? (Smile)
 
  • #11
evinda said:
hen [m]i[/m] would be incremented and we would make a swap, right? (Smile)

Yes.
I'd say that an element equal to x=A[r] will end up to the left of the position where A[r] will end up at the end. (Nerd)
 
  • #12
I like Serena said:
Yes.
I'd say that an element equal to x=A[r] will end up to the left of the position where A[r] will end up at the end. (Nerd)

Nice!
I want to explain why the execution time of [m] partition [/m] at an array of length $n$ is $\Theta(n)$.
Could I say the following? (Thinking)

We assume that assignments, accesses at elements of array, call of swap, additions, comparisons between numbers and return of value require constant time.
The for loop is executed $r-1-p+1=r-p=n-1$ times.
So the time execution is $b+d(n-1)+e \in \Theta(n)$
where $b,d,e$ constants.
 
  • #13
evinda said:
Nice!
I want to explain why the execution time of [m] partition [/m] at an array of length $n$ is $\Theta(n)$.
Could I say the following? (Thinking)

We assume that assignments, accesses at elements of array, call of swap, additions, comparisons between numbers and return of value require constant time.
The for loop is executed $r-1-p+1=r-p=n-1$ times.
So the time execution is $b+d(n-1)+e \in \Theta(n)$
where $b,d,e$ constants.

What if I call [m]partition(A, 1, 1)[/m], where A is an array with n=100000 elements?
Does it take ~100000 operations? (Wondering)
 
  • #14
I like Serena said:
What if I call [m]partition(A, 1, 1)[/m], where A is an array with n=100000 elements?
Does it take ~100000 operations? (Wondering)

I am asked to explain why the execution time of [m] partition [/m] at an array of length $n$ is $\Theta(n)$.
So don't we suppose that $r-p+1=n$? Or am I wrong? (Thinking)
 
  • #15
evinda said:
I am asked to explain why the execution time of [m] partition [/m] at an array of length $n$ is $\Theta(n)$.
So don't we suppose that $r-p+1=n$? Or am I wrong? (Thinking)

In an answer I would say: "If we apply the algorithm at a section of an array of length $n=r-p+1$, then...".
Perhaps I'm a bit of a nitpicker, but I prefer to be accurate. (Wasntme)
 
  • #16
I like Serena said:
In an answer I would say: "If we apply the algorithm at a section of an array of length $n=r-p+1$, then...".
Perhaps I'm a bit of a nitpicker, but I prefer to be accurate. (Wasntme)

I prefer it too... (Nod) Thanks a lot! (Smile)
 

Similar threads

Back
Top