| 
 | 1 | + | 
 | 2 | +#include <time.h> //for random testing numbers  | 
 | 3 | +#include <assert.h> //for assert testing  | 
 | 4 | +#include <stdlib.h> //for rand()  | 
 | 5 | + | 
 | 6 | +/**  | 
 | 7 | + *  @brief Moves elements in array one to the right,   | 
 | 8 | + *  overriding the element to the right and leaving  | 
 | 9 | + *  a copy of the first element. Returns replaced int  | 
 | 10 | + *  @param Array array to be sorted  | 
 | 11 | + *  @param startIndex left most element being moved right  | 
 | 12 | + *  @param endIndex Position that elements will move towards  | 
 | 13 | + *  @returns [int] integer that was overwritten  | 
 | 14 | + */  | 
 | 15 | +int moveRight(int Array[], int startIndex, int endIndex)  | 
 | 16 | +{  | 
 | 17 | +    int erasedInteger = Array[endIndex];  | 
 | 18 | +    for (int index = endIndex; index >= startIndex; --index)  | 
 | 19 | +    {  | 
 | 20 | +        Array[index] = Array[index-1];     | 
 | 21 | +    }     | 
 | 22 | +    return erasedInteger;  | 
 | 23 | +}  | 
 | 24 | + | 
 | 25 | +/**  | 
 | 26 | + *  @brief Moves elements in array one to the left,   | 
 | 27 | + *  overriding the element to the left and leaving  | 
 | 28 | + *  a copy of the first element. Returns replaced int  | 
 | 29 | + *  @param Array array to be sorted  | 
 | 30 | + *  @param startIndex Position that elements will move towards  | 
 | 31 | + *  @param endIndex right most element being moved left  | 
 | 32 | + *  @returns [int] integer that was overriden  | 
 | 33 | + */  | 
 | 34 | +int moveLeft(int Array[], int startIndex, int endIndex)  | 
 | 35 | +{  | 
 | 36 | +    int erasedInteger = Array[endIndex];  | 
 | 37 | +    for (int index = startIndex; index <= endIndex; ++index)  | 
 | 38 | +    {  | 
 | 39 | +        Array[index] = Array[index+1];     | 
 | 40 | +    }     | 
 | 41 | +    return erasedInteger;  | 
 | 42 | +}  | 
 | 43 | + | 
 | 44 | +/**  | 
 | 45 | + *  @brief Using binarySearch to find position of where element should be  | 
 | 46 | + *  @param array array to be searched  | 
 | 47 | + *  @param sortedListStartIndex starting index of the area to be searched  | 
 | 48 | + *  @param sortedListEndIndex ending index of the area to be searched  | 
 | 49 | + *  @returns [int] position of element or where it should be if not in array  | 
 | 50 | + */  | 
 | 51 | +int binarySearchToFindPosition(int array[], int element,  | 
 | 52 | +        int sortedListStartIndex, int sortedListEndIndex)  | 
 | 53 | +{  | 
 | 54 | +    int low = sortedListStartIndex;  | 
 | 55 | +    int max = sortedListEndIndex;  | 
 | 56 | +    int mid = (max - low) / 2 + low;  | 
 | 57 | +      | 
 | 58 | +    //loops through elements until it finds either the same element or  | 
 | 59 | +    //it finds the two elements it should be between and returns index of larger one  | 
 | 60 | +    while ( !(element >= array[mid-1] && element <= array[mid]) )  | 
 | 61 | +    {  | 
 | 62 | +        mid = (max - low) / 2 + low;  | 
 | 63 | +        if (element > array[mid])  | 
 | 64 | +        {  | 
 | 65 | +            low = mid + 1;  | 
 | 66 | +        }  | 
 | 67 | +        else if (element < array[mid])  | 
 | 68 | +        {  | 
 | 69 | +            max = mid - 1;  | 
 | 70 | +        }  | 
 | 71 | +        else  | 
 | 72 | +        {  | 
 | 73 | +            return mid;  | 
 | 74 | +        }  | 
 | 75 | +          | 
 | 76 | +    }  | 
 | 77 | +    return mid;  | 
 | 78 | +}  | 
 | 79 | + | 
 | 80 | +/**  | 
 | 81 | + *  @brief Sorting algorithm that starts with   | 
 | 82 | + *  middle elements and sorts out from the center  | 
 | 83 | + *  @param Array Array that is to be sorted  | 
 | 84 | + *  @param sizeOfArray size of array  | 
 | 85 | + */  | 
 | 86 | +void MiddleSort(int Array[], int sizeOfArray)  | 
 | 87 | +{  | 
 | 88 | +    int currentSortedSize = (sizeOfArray % 2 == 0) ? 1 : 0;  | 
 | 89 | +      | 
 | 90 | +    int startIndex = (sizeOfArray / 2) - currentSortedSize;  | 
 | 91 | +    int endIndex = startIndex + currentSortedSize;  | 
 | 92 | +      | 
 | 93 | +    int replacePosition;  | 
 | 94 | +    int currentLarge;  | 
 | 95 | +    int currentLow;  | 
 | 96 | + | 
 | 97 | +    //create initial state with sorted one or two elements  | 
 | 98 | +    if (Array[startIndex] > Array[endIndex])  | 
 | 99 | +    {  | 
 | 100 | +        currentLarge = Array[startIndex];  | 
 | 101 | +        currentLow = Array[endIndex];  | 
 | 102 | +    }  | 
 | 103 | +    else  | 
 | 104 | +    {  | 
 | 105 | +        currentLow = Array[startIndex];  | 
 | 106 | +        currentLarge = Array[endIndex];  | 
 | 107 | +    }  | 
 | 108 | + | 
 | 109 | +    //set array preloop state  | 
 | 110 | +    Array[startIndex] = currentLow;  | 
 | 111 | +    Array[endIndex] = currentLarge;  | 
 | 112 | + | 
 | 113 | +    startIndex -= 1;  | 
 | 114 | +    endIndex += 1;  | 
 | 115 | + | 
 | 116 | +    //loop until end of array is passed  | 
 | 117 | +    while (startIndex >= 0 && endIndex < sizeOfArray)  | 
 | 118 | +    {  | 
 | 119 | + | 
 | 120 | +        //checks which of the two picks is larger/smaller  | 
 | 121 | +        if (Array[startIndex] > Array[endIndex])  | 
 | 122 | +        {  | 
 | 123 | +            currentLarge = Array[startIndex];  | 
 | 124 | +            currentLow = Array[endIndex];  | 
 | 125 | +        }  | 
 | 126 | +        else  | 
 | 127 | +        {  | 
 | 128 | +            currentLow = Array[startIndex];  | 
 | 129 | +            currentLarge = Array[endIndex];  | 
 | 130 | +        }  | 
 | 131 | + | 
 | 132 | +        //if block to determine how elements are moved around and places  | 
 | 133 | +            //if low is lowest  | 
 | 134 | +        if (currentLow < Array[startIndex+1])  | 
 | 135 | +        {  | 
 | 136 | +            Array[startIndex] = currentLow;  | 
 | 137 | + | 
 | 138 | +            //check where largeest should go  | 
 | 139 | +            if (currentLarge < Array[endIndex-1])  | 
 | 140 | +            {  | 
 | 141 | +                replacePosition = binarySearchToFindPosition(Array, currentLarge,  | 
 | 142 | +                                  startIndex, endIndex-1);  | 
 | 143 | +                moveRight(Array, replacePosition, endIndex);  | 
 | 144 | +                Array[replacePosition] = currentLarge;  | 
 | 145 | +            }  | 
 | 146 | +            else  | 
 | 147 | +            {  | 
 | 148 | +                Array[endIndex] = currentLarge;  | 
 | 149 | +            }  | 
 | 150 | +        }  | 
 | 151 | +        //if the lowest and larger are the biggest so far  | 
 | 152 | +        else if (currentLow > Array[endIndex-1])  | 
 | 153 | +        {  | 
 | 154 | +            moveLeft(Array, startIndex, endIndex-1);  | 
 | 155 | +            Array[endIndex-1] = currentLow;  | 
 | 156 | +            Array[endIndex] = currentLarge;  | 
 | 157 | +        }  | 
 | 158 | +        //if low tested value is not the largest or smallest  | 
 | 159 | +        else  | 
 | 160 | +        {  | 
 | 161 | +            replacePosition = binarySearchToFindPosition(Array, currentLow,  | 
 | 162 | +                              startIndex+1, endIndex-1);  | 
 | 163 | +            moveLeft(Array, startIndex, replacePosition-1);  | 
 | 164 | +            Array[replacePosition-1] = currentLow;  | 
 | 165 | + | 
 | 166 | +            //check where largest should go, either at end or in sorted list  | 
 | 167 | +            if (currentLarge < Array[endIndex-1])  | 
 | 168 | +            {  | 
 | 169 | +                replacePosition = binarySearchToFindPosition(Array, currentLarge,  | 
 | 170 | +                                startIndex, endIndex-1);  | 
 | 171 | +                moveRight(Array, replacePosition, endIndex);  | 
 | 172 | +                Array[replacePosition] = currentLarge;   | 
 | 173 | +            }  | 
 | 174 | +            else  | 
 | 175 | +            {  | 
 | 176 | +                Array[endIndex] = currentLarge;  | 
 | 177 | +            }  | 
 | 178 | +        }  | 
 | 179 | +          | 
 | 180 | +        //increment sorted array  | 
 | 181 | +        --startIndex;  | 
 | 182 | +        ++endIndex;  | 
 | 183 | +    }  | 
 | 184 | +}  | 
 | 185 | + | 
 | 186 | + | 
 | 187 | +/**  | 
 | 188 | + * @brief tests sorting middle sort algorithm  | 
 | 189 | + * @param array array to be filled and tested  | 
 | 190 | + * @param arraySize size of array  | 
 | 191 | + */  | 
 | 192 | +static void test(int array[], int arraySize)  | 
 | 193 | +{  | 
 | 194 | +    for (int index = 0; index < arraySize; ++index)  | 
 | 195 | +    {  | 
 | 196 | +        array[index] = (rand() % 100);  | 
 | 197 | +    }  | 
 | 198 | + | 
 | 199 | +    MiddleSort(array, arraySize);  | 
 | 200 | + | 
 | 201 | +    for (int index = 1; index < arraySize; ++index)  | 
 | 202 | +    {  | 
 | 203 | +        assert(array[index-1] <= array[index]);  | 
 | 204 | +    }  | 
 | 205 | +}  | 
 | 206 | + | 
 | 207 | +/** Main function  | 
 | 208 | + *  @brief Runs main and creates test array that will be tested  | 
 | 209 | + *  @returns an integer 0  | 
 | 210 | + */  | 
 | 211 | +int main()  | 
 | 212 | +{  | 
 | 213 | +    srand(time(NULL));  | 
 | 214 | +    int size = 50;  | 
 | 215 | +    int array[size];  | 
 | 216 | + | 
 | 217 | +    test(array, size);  | 
 | 218 | + | 
 | 219 | +    return 0;  | 
 | 220 | +}  | 
0 commit comments