Function of Partition: Understanding Array Partitioning Process

In summary: What if I call partition(A, 1, 1), where A is an array with n=100000 elements?It would take about 100,000 operations.
  • #1
evinda
Gold Member
MHB
3,836
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: 80
  • end.png
    end.png
    1.5 KB · Views: 74
Technology news on Phys.org
  • #2
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?
 
  • #3
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)
 
  • #4
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: 75
  • #5
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)
 
  • #6
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)
 
  • #7
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)
 
  • #8
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)
 
  • #9
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)
 

FAQ: Function of Partition: Understanding Array Partitioning Process

What is partitioning in arrays?

Partitioning in arrays is the process of dividing a large array into smaller, more manageable sub-arrays. This allows for easier data processing and retrieval.

Why is partitioning important in array processing?

Partitioning is important in array processing because it allows for more efficient use of memory and processing power. By dividing a large array into smaller ones, it is easier to manipulate and analyze the data, resulting in faster and more accurate results.

How does the partitioning process work?

The partitioning process involves selecting a pivot element, which is used to divide the array into two sub-arrays. Elements smaller than the pivot are placed in one sub-array, while elements larger than the pivot are placed in the other sub-array. This process is repeated until the entire array is partitioned.

What are the benefits of using partitioning in array processing?

Using partitioning in array processing has several benefits, including improved data organization, faster processing times, and better memory management. It also allows for easier implementation of sorting and searching algorithms, as well as more efficient use of parallel processing.

Are there any drawbacks to using partitioning in array processing?

While partitioning can greatly improve the efficiency of array processing, it does have some drawbacks. One potential issue is the added complexity and overhead involved in implementing partitioning algorithms. Additionally, if the pivot element is not chosen carefully, it can result in unbalanced sub-arrays and inefficient processing.

Similar threads

Replies
1
Views
1K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
1
Views
909
Replies
2
Views
1K
Replies
1
Views
2K
Replies
4
Views
1K
Back
Top