Last active
May 14, 2016 18:27
-
-
Save dvtate/c9e6cd1cb8b068aacdc8f093f8b6beb0 to your computer and use it in GitHub Desktop.
A lame sorting algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
template<class T> | |
void sortArray(T* arr, size_t length){ | |
//for each element of the array... | |
for (length; length > 0; length--, arr++) { | |
T maxValue = *arr; | |
T* swapVal = arr; | |
//find the largest remaining number | |
for (size_t i = 0; i < length; i++) | |
if (maxValue < *(arr + i)) { | |
maxValue = *(arr + i); | |
swapVal = arr + i; | |
} | |
// switch elements' locations in the array | |
T temp = *arr; | |
*arr = *swapVal; | |
*swapVal = temp; | |
} | |
} | |
int main(){ | |
std::cout <<"How many numbers to sort?\n"; | |
size_t length; // NOTE: size_t = long int | |
std::cin >>length; | |
// allocate some memory to hold our array | |
long double userArray[length]; | |
std::cout <<"Enter " <<length <<" numbers:\n"; | |
for (size_t i = 0; i < length; i++) | |
std::cin >>userArray[i]; | |
//sort the array | |
sortArray(userArray, length); | |
// print the array | |
std::cout <<"\nin numerical order:\n"; | |
std::cout <<userArray[0]; | |
for (size_t i = 1; i < length; i++) | |
std::cout <<", " <<userArray[i]; | |
// terminating newlines are nice | |
std::cout <<std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment