Created
January 27, 2020 16:43
-
-
Save bpmct/84b777d849593486d5af582e0dabe6f1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace NumberSort | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
double[] aUnsorted; | |
double[] aSorted; | |
start: | |
Console.WriteLine("Enter some numbers: "); | |
string sNumbers = Console.ReadLine(); | |
string[] sNumber = sNumbers.Split(' '); | |
int nUnsortedLength = 0; | |
double nThisNumber; | |
foreach (string sThisNumber in sNumber) | |
{ | |
if (sThisNumber.Length == 0) | |
{ | |
continue; | |
} | |
try | |
{ | |
nThisNumber = double.Parse(sThisNumber); | |
++nUnsortedLength; | |
} | |
catch | |
{ | |
Console.WriteLine($"Number #{nUnsortedLength + 1} is not a valid number."); | |
goto start; | |
} | |
} | |
aUnsorted = new double[nUnsortedLength]; | |
nUnsortedLength = 0; | |
foreach (string sThisNumber in sNumber) | |
{ | |
if (sThisNumber.Length == 0) | |
{ | |
continue; | |
} | |
nThisNumber = double.Parse(sThisNumber); | |
aUnsorted[nUnsortedLength++] = nThisNumber; | |
} | |
aSorted = new double[nUnsortedLength]; | |
int nSortedLength = 0; | |
aSorted[nSortedLength] = FindLowestValue(aUnsorted); | |
RemoveUnsortedValue(aSorted[nSortedLength], ref aUnsorted); | |
} | |
static double FindLowestValue(double[] array) | |
{ | |
double returnVal; | |
returnVal = array[0]; | |
foreach (double thisNum in array) | |
{ | |
if (thisNum < returnVal) | |
returnVal = thisNum; | |
} | |
return returnVal; | |
} | |
static void RemoveUnsortedValue(double removeValue, ref double[] array) | |
{ | |
double[] newArray = new double[array.Length - 1]; | |
int dest = 0; | |
bool bAlreadyRemoved = false; | |
foreach (double srcNumber in array) | |
{ | |
if (srcNumber == removeValue && !bAlreadyRemoved) | |
{ | |
bAlreadyRemoved = true; | |
continue; | |
} | |
newArray[dest] = srcNumber; | |
++dest; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment