From fb08343f8c866dea2ec5e86a0dca8d091e33d146 Mon Sep 17 00:00:00 2001 From: ergz Date: Thu, 17 Aug 2023 23:20:31 -0700 Subject: [PATCH] implement quicksort in c again to practice and in oding to see what its like --- c/qs.c | 2 +- c/quicksort.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ odin/main.odin | 46 +++++++++++++++++++++++++++++++++++++++------ 3 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 c/quicksort.c diff --git a/c/qs.c b/c/qs.c index 54a8efe..32fbfb4 100644 --- a/c/qs.c +++ b/c/qs.c @@ -38,7 +38,7 @@ void quick_sort(int arr[], int lo, int hi) { int main() { int array_len = 5; - int arr[5] = {5, 4, 3, 2, 1}; + int arr[5] = {0, 2, 5, 1, 6}; quick_sort(arr, 0, array_len - 1); printf("[ "); diff --git a/c/quicksort.c b/c/quicksort.c new file mode 100644 index 0000000..119a899 --- /dev/null +++ b/c/quicksort.c @@ -0,0 +1,51 @@ +#include + +// the partition will weakly sort the array and return the index of +// the pivot +int partition(int arr[], int lo, int hi) { + int pivot_value = arr[hi]; + int current_index = lo - 1; + int temp; + for (int i = lo; i < hi; i++) { + if (arr[i] <= pivot_value) { + current_index++; + + temp = arr[i]; + arr[i] = arr[current_index]; + arr[current_index] = temp; + } + } + + // once done with loop the current_index will hold + // the location where we need to place the pivot_value + current_index++; + arr[hi] = arr[current_index]; + arr[current_index] = pivot_value; + + return (current_index); +} + +void qs(int arr[], int lo, int hi) { + if (lo < hi) { + int current_pivot = partition(arr, lo, hi); + qs(arr, lo, current_pivot - 1); + qs(arr, current_pivot + 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] = {0, 2, 5, 1, 6}; + print_array(arr, 5); + qs(arr, 0, 4); + print_array(arr, 5); + + return (0); +} \ No newline at end of file diff --git a/odin/main.odin b/odin/main.odin index a71ac09..c1008a6 100644 --- a/odin/main.odin +++ b/odin/main.odin @@ -5,16 +5,50 @@ import "core:fmt" linear_search :: proc(arr: []int, len: int, needle: int) -> bool { for index, val in arr { if val == needle { - return true; + return true } } - return false; + return false +} + +partition :: proc(arr: ^[]int, lo: int, hi: int) -> int { + pivot_value := arr[hi] + current_index := lo - 1 + + for i := lo; i < hi; i +=1 { + if arr[i] <= pivot_value { + current_index += 1 + tmp := arr[i] + arr[i] = arr[current_index] + arr[current_index] = tmp + } + } + + current_index += 1 + arr[hi] = arr[current_index] + arr[current_index] = pivot_value + + return(current_index) +} + +quick_sort :: proc(arr: ^[]int, lo: int, hi: int) { + if lo < hi { + current_pivot := partition(arr, lo, hi); + quick_sort(arr, lo, current_pivot - 1); + quick_sort(arr, current_pivot + 1, hi); + } } main :: proc() { - fmt.println("hello there here is a for loop"); + fmt.println("hello there here is a for loop") - values: []int = {1, 2, 3, 4, 5}; - res: bool = linear_search(values, 5, 2); - fmt.println(res); + values: []int = {1, 2, 3, 4, 5} + res: bool = linear_search(values, 5, 2) + fmt.println(res) + + fmt.println("quicksort ----------------") + arr: []int = {0, 2, 5, 1, 6} + fmt.println(arr); + quick_sort(&arr, 0, 4); + fmt.println(arr); }