Merge branch 'main' of github.com:ergz/tlacyl
This commit is contained in:
@@ -91,6 +91,7 @@ void remove_at_end(LinkedList* list) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int get_length(LinkedList *list) {
|
||||
return (list->length);
|
||||
}
|
||||
@@ -187,3 +188,4 @@ int main() {
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
4
c/qs.c
4
c/qs.c
@@ -5,9 +5,9 @@
|
||||
// of it and everything greater than it on the right
|
||||
int partition(int arr[], int lo, int hi) {
|
||||
int pivot_value = arr[hi];
|
||||
int idx = -1; // this will keep track of current location of values less then the pivot
|
||||
int idx = lo - 1; // this will keep track of current location of values less then the pivot
|
||||
|
||||
for (int i = 0; i < hi; i++) {
|
||||
for (int i = lo; i < hi; i++) {
|
||||
if (arr[i] <= pivot_value) {
|
||||
idx++;
|
||||
|
||||
|
||||
69
c/qs2.cpp
Normal file
69
c/qs2.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include <stdio.h>
|
||||
|
||||
/* a rewrite to make sure I can do this without reading */
|
||||
|
||||
/*
|
||||
partition
|
||||
|
||||
Modifies array by moving all values less then a pivot point
|
||||
to the left of it and all value greater to the right. Returns
|
||||
the index of the pivot at the end of the process.
|
||||
|
||||
@param int arr[] array to partition
|
||||
@param int lo low value to start iteration on
|
||||
@param int hi high value to end iteration on
|
||||
*/
|
||||
int partition(int arr[], int lo, int hi) {
|
||||
int pindex = lo - 1; // where pivot will end up
|
||||
int pivot_value = arr[hi]; // can be different
|
||||
|
||||
for (int i = lo; i < hi; i++) {
|
||||
if (arr[i] <= pivot_value) {
|
||||
pindex++;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[pindex];
|
||||
arr[pindex] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
// after iterating through array, we place pivot in correct
|
||||
// position
|
||||
pindex++;
|
||||
arr[hi] = arr[pindex];
|
||||
arr[pindex] = pivot_value;
|
||||
|
||||
return (pindex);
|
||||
}
|
||||
|
||||
/*
|
||||
quick_sort
|
||||
|
||||
@param int arr[] array to be sorted
|
||||
@param int lo starting point for iteration
|
||||
@param int hi ending point for itereation
|
||||
*/
|
||||
void quick_sort(int arr[], int lo, int hi) {
|
||||
if (lo < hi) { // base case is lo == hi
|
||||
int pivot_index = partition(arr, lo, hi);
|
||||
|
||||
quick_sort(arr, lo, pivot_index - 1);
|
||||
quick_sort(arr, pivot_index + 1, hi);
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(int arr[], int len) {
|
||||
printf("[ ");
|
||||
for (int i = 0; i < len; i++) {
|
||||
printf("%d ", arr[i]);
|
||||
}
|
||||
printf("]\n");
|
||||
}
|
||||
|
||||
int main() {
|
||||
int arr[5] = {5, 4, 3, 2, 1};
|
||||
printf("before ");
|
||||
print_array(arr, 5);
|
||||
quick_sort(arr, 0, 4);
|
||||
printf("after ");
|
||||
print_array(arr, 5);
|
||||
}
|
||||
Reference in New Issue
Block a user