Added sort functions

This commit is contained in:
2019-03-10 11:49:24 -04:00
parent e951c2d9a0
commit a494acf34f
2 changed files with 111 additions and 0 deletions

View File

@@ -27,6 +27,7 @@
#include <stdlib.h>
#include <inttypes.h>
#include <stdbool.h>
struct DynamicInt64Array{
@@ -221,4 +222,80 @@ uint64_t allocatedDynamicInt64Array(struct DynamicInt64Array* ary){
return ary->allocated;
}
//This is a function that performs a bubble sort on a DynamicInt64Array
void bubbleSortDynamicInt64Array(struct DynamicInt64Array* ary){
//Keep track of the elements that have been sorted
for(int64_t sorted = 0;sorted < ary->size;++sorted){
//Look at every element in the array, moving the largest to the back
for(int64_t location = 1;location < (ary->size - sorted);++location){
//If the current element is smaller than the last swap them
if(ary->ptr[location] < ary->ptr[location - 1]){
int64_t temp = ary->ptr[location];
ary->ptr[location] = ary->ptr[location - 1];
ary->ptr[location - 1] = temp;
}
}
}
}
//This is a helper function of quickSortInt64Array. It chooses a pivot element and sorts everything to larger and smaller sides
uint64_t partitionInt64Array(int64_t* ary, uint64_t bottom, uint64_t top){
int64_t pivot = ary[top]; //Choose a pivot element
int64_t smaller = bottom - 1; //Keep track of the location of all elements smaller than pivot
//Loop through the array, looking for elements that are smaller than pivot and move them to the front of the array
for(uint64_t location = bottom;location < top;++location){
//If the current element is smaller than the pivot move it to the front of the array and move the tracker
if(ary[location] < pivot){
++smaller; //Increment the smaller than location tracker
//Swap the element to the correct location
int64_t temp = ary[location];
ary[location] = ary[smaller];
ary[smaller] = temp;
}
}
//Move the pivot element to the corrent location
++smaller;
int64_t temp = ary[smaller];
ary[smaller] = ary[top];
ary[top] = temp;
//Return the location of the pivot element
return smaller;
}
//This function helps quickSort a DynamicInt64Array
void quickSortInt64Array(int64_t* ary, uint64_t bottom, uint64_t top){
//Make sure you are working on a valid section of the array
if(bottom < top){
//Get the pivot location
uint64_t pivot = partitionInt64Array(ary, bottom, top);
//Sort everything smaller than the pivot
quickSortInt64Array(ary, bottom, pivot - 1);
//Sort everything larger than the pivot
quickSortInt64Array(ary, pivot + 1, top);
}
}
//This is a function that performs a quick sort on a DynamicInt64Array
void quickSortDynamicInt64Array(struct DynamicInt64Array* ary){
quickSortInt64Array(ary->ptr, 0, ary->size - 1);
}
//This is a function to determine if an array is already sorted correctly
bool isSortedDynamicInt64Array(struct DynamicInt64Array* ary){
//Look at every element in the array looking for elements that are out of order
for(int cnt = 1;cnt < ary->size;++cnt){
//If the current element is smaller than the last element return false
if(ary->ptr[cnt] < ary->ptr[cnt - 1]){
return false;
}
}
//If you found no elements out of order then the array is sorted. return true
return true;
}
#endif //DYNAMIC_ARRAY_H