Last active
December 28, 2020 12:42
-
-
Save YonLiud/53c9f45c89ef49504a0e9596e5cfea21 to your computer and use it in GitHub Desktop.
Injection Array Sorting Method - C#
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
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