Skip to content

Instantly share code, notes, and snippets.

@YonLiud
Last active December 28, 2020 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YonLiud/53c9f45c89ef49504a0e9596e5cfea21 to your computer and use it in GitHub Desktop.
Save YonLiud/53c9f45c89ef49504a0e9596e5cfea21 to your computer and use it in GitHub Desktop.
Injection Array Sorting Method - C#
static int[] InsertionSort(int[] inputArray)
{
int[] outputArray = inputArray;
int tmp;
bool swapped;
int count = 0;
for (int i = 0; i < ARRAY_SIZE - 1; i++) // checking all numbers
{
swapped = false;
for (int j = i + 1; j > 0; j--) // initializing the next number
{
count++;
if (inputArray[j - 1] > inputArray[j]) // checking thats the number after the current number IS smaller,
// so we swap between them, and sort them in the correct order
{
tmp = inputArray[j - 1];
inputArray[j - 1] = inputArray[j]; //swapping if true
inputArray[j] = tmp;
swapped = true;
}
}
if (i == ARRAY_SIZE - 1 && !swapped)
{
//break;
}
}
Console.WriteLine(count);
return outputArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment