שטות

mail@pastecode.io avatar
unknown
java
a year ago
1.8 kB
2
Indexable
Never
internal class Program
{
    private static void Main(string[] args)
    {
        int length = 10;
        int[] preSortArray = FillArrayFromUser(length);
        int[] sortedArray = BubbleSortArray(preSortArray);
        PrintArray(sortedArray);
    }

    private static int[] FillArrayFromUser(int length)
    {
        int[] userArray = new int[length];
        for (int i = 0; i < length; i++)
        {
            Console.WriteLine("Would you be so kind to enter a number between 1 and 250 right here:");
            int userInput = int.Parse(Console.ReadLine());
            Console.Clear();
            userArray[i] = userInput;
        }
        return userArray;
    }

    private static int[] BubbleSortArray(int[] preSortArray)
    {
        bool sorted = false;
        int noSwapNum = 0;
        while (!sorted)
        {
            SwitchOrCount(preSortArray, sorted, noSwapNum);
        }
        return preSortArray;
    }

    private static bool SwitchOrCount(int[] preSortArray, bool sorted, int noSwitchNum) 
    {
        for (int j = 0; j < preSortArray.Length - 1; j++)
        {
            if (preSortArray[j] > preSortArray[j + 1])
            {
                (preSortArray[j], preSortArray[j + 1]) = (preSortArray[j + 1], preSortArray[j]);
                noSwitchNum = 0;
            }
            else
            {
                noSwitchNum++;
            }
            if (noSwitchNum == preSortArray.Length - 1)
            {
                sorted = true;
            }
        }
        return sorted;
    }

    private static void PrintArray(int[] myArray)
    {
        Console.WriteLine("ze haPitaron Kapara");
        for (int i = 0; i < myArray.Length; i++)
        {
            Console.WriteLine(myArray[i]);
        }
    }
}