#Partition sorting algo

4 messages · Page 1 of 1 (latest)

rancid star
#

Hello, I'm having some trouble understanding the partition sort algorithmn, (see pic from my Universities slides),

The array we saw in class is as such;
A= [42, 89, 63, 12, 94, 27, 78, 3, 50, 36]

I understand the Idea of a partition sorting is to separate every thing less than X to one side and greater to the other, but I can't seem to understand the process. (By partition sorting I am referring to the quicksort substep)

here is the code for a closer glance (there are no errors within)

QuickSort(int left, int right){
    if(right <= left) return;
    else {
      pivot = A[right]; 
      pivotIndex = partition(left, right, pivot);
      QuickSort(left, pivotIndex-1);
      QuickSort(pivotIndex+1, right); }
}


int partition(int left, int right, int pivot){
  int i = left-1; 
  int j = right; 
  while(true) { 
    while(A[++i] < pivot); 
    while(A[--j] > pivot);
    if(i >= j) break;
    else swapReferences(A,i,j) 
  } 
  swapReferences(A,i,right); // put pivot in correct spot 
  return i;                  // return new pivot location
}
thorny foxBOT
#

This post has been reserved for your question.

Hey @rancid star! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

rancid star
#

Hello, so this video aided my understanding, my friend found it! I guess I was simply searching the wrong things
https://youtu.be/Hoixgm4-P4M?si=o72uT4xXQ-bBG69o

Step by step instructions showing how to run quick sort.

Code: https://github.com/msambol/dsa/blob/master/sort/quick_sort.py (different than video, I added this retroactively)

Sources:

  1. Data Structures and Abstractions with Java by Frank M. Carrano [https://www.amazon.com/Structures-Abstractions-Whats-Computer-Science/dp/0134831691]
  2. http...
▶ Play video