Skip to content

Instantly share code, notes, and snippets.

@happyzleaf
Last active March 25, 2022 22:12
Show Gist options
  • Save happyzleaf/25b73fd34b6f864ad1e35ca9a844a048 to your computer and use it in GitHub Desktop.
Save happyzleaf/25b73fd34b6f864ad1e35ca9a844a048 to your computer and use it in GitHub Desktop.
QuickSort with Lomuto partition
#include <stdio.h>
/**
* [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
* [ 8, 29, 60, 6, 4, 93, 85, 5, 26, 29, 2, 69, 83, 72, 43, 50]
* Pivot 50 (a[r])
* I 0
* Leftwall 0
*
* Step 1)
* [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
* [ 8, 29, 60, 6, 4, 93, 85, 5, 26, 29, 2, 69, 83, 72, 43, 50]
* I 0
* Leftwall 1
*
* Step 2)
* [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
* [ 8, 29, 60, 6, 4, 93, 85, 5, 26, 29, 2, 69, 83, 72, 43, 50]
* I 1
* Leftwall 2
*
* Step 3)
* [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
* [ 8, 29, 6, 60, 4, 93, 85, 5, 26, 29, 2, 69, 83, 72, 43, 50]
* I 3
* Leftwall 3
*
* Step 4)
* [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
* [ 8, 29, 6, 4, 60, 93, 85, 5, 26, 29, 2, 69, 83, 72, 43, 50]
* I 4
* Leftwall 4
*
* Step 5)
* [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
* [ 8, 29, 6, 4, 5, 93, 85, 60, 26, 29, 2, 69, 83, 72, 43, 50]
* I 7
* Leftwall 5
*
* Step 6)
* [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
* [ 8, 29, 6, 4, 5, 26, 85, 60, 93, 29, 2, 69, 83, 72, 43, 50]
* I 8
* Leftwall 6
*
* Step 7)
* [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
* [ 8, 29, 6, 4, 5, 26, 29, 60, 93, 85, 2, 69, 83, 72, 43, 50]
* I 9
* Leftwall 7
*
* Step 8)
* [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
* [ 8, 29, 6, 4, 5, 26, 29, 2, 93, 85, 60, 69, 83, 72, 43, 50]
* I 10
* Leftwall 8
*
* Step 9)
* [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
* [ 8, 29, 6, 4, 5, 26, 29, 2, 43, 85, 60, 69, 83, 72, 93, 50]
* I 14
* Leftwall 9
*
* Step Finale)
*
* leftwall r
* | |
* | |
* v v
* [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
* [ 8, 29, 6, 4, 5, 26, 29, 2, 43, 85, 60, 69, 83, 72, 93, 50]
*
* Scambio pivot con leftwall
* [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
* [ 8, 29, 6, 4, 5, 26, 29, 2, 43, 50, 60, 69, 83, 72, 93, 85]
*
* Array sinistro: (tutti minori di 50)
*
* l leftwall - 1
* | |
* | |
* v v
* [ 8, 29, 6, 4, 5, 26, 29, 2, 43]
*
*
* Array destro: (tutti maggiori di 50)
*
* leftwall + 1 r
* | |
* | |
* v v
* [60, 69, 83, 72, 93, 85]
*
* Valore di ritorno (pivot):
*
* leftwall
* |
* |
* v
* [50]
*/
int partition(int *A, int l, int r) {
int pivot = A[r];
int leftWall = l;
int tmp;
for (int i = l; i < r; ++i) {
if (A[i] <= pivot) {
if (i != leftWall) {
tmp = A[i];
A[i] = A[leftWall];
A[leftWall] = tmp;
}
++leftWall;
}
}
if (r != leftWall) {
tmp = A[r];
A[r] = A[leftWall];
A[leftWall] = tmp;
}
return leftWall;
}
void quickSort(int *A, int l, int r) {
if (l >= r) {
return;
}
int q = partition(A, l, r);
quickSort(A, l, q - 1);
quickSort(A, q + 1, r);
}
int main() {
int A[] = {
8, 29, 60, 6, 4, 93, 85, 5, 26, 29, 2, 69, 83, 72, 43, 50
};
int length = sizeof(A) / sizeof(A[0]);
quickSort(A, 0, length - 1);
printf("A[%d] = {\n\t", length);
for (int i = 0; i < length; ++i) {
if (i != 0) {
printf(", ");
}
printf("%2d", A[i]);
}
printf("\n}\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment