Function of Partition: Understanding Array Partitioning Process

  • Context:
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Function Partition
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
15 replies · 4K views
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: 160
  • end.png
    end.png
    1.5 KB · Views: 144
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: 145
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)
 
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)
 
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)
 
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.
 
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)
 
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)
 
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)
 
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)