• API
• FAQ
• Tools
• Archive
daily pastebin goal
49%
SHARE
TWEET

# Untitled

a guest Jun 18th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. const int SIZE = 10; //the size of the array
2.         static void Sort()
3.         {
4.             int[] numbers = new int[SIZE];
5.             //input
6.             int input;
7.             for (int i = 0; i < numbers.Length; i++)
8.             {
9.                 Console.WriteLine("Enter a number: ");
10.                 input = int.Parse(Console.ReadLine());
11.                 //sort
12.                 InsertionSort(input, numbers, i);
13.                 for (int j = 0; j < numbers.Length; j++)
14.                 {
15.                     Console.Write(numbers[i] + " ");
16.                 }
17.             }
18.             //print
19.             for (int i = 0; i < numbers.Length; i++)
20.             {
21.                 Console.WriteLine(numbers[i]);
22.             }
23.         }
24.         static void InsertionSort(int value, int[] numbers, int count)
25.         {
26.             /*
27.              * Insertion Sort algorithm:
28.              *
29.              * Takes three parameters - value (the number to be inserted), numbers (the array being sorted), and count (the number of items already inserted).
30.              *
31.              * The algorithm searches through the array for a number that's bigger than the value being inserted. Once the number is found, that number
32.              * and all the numbers after it are shifted to the right (there's no need to check for overflow; the last element is only filled when the array is
33.              * completely filled in, so it's never shifted), and the value is inserted into the number's position.
34.              *
35.              * If the algorithm is unable to find
36.              * a number bigger than the value, it means that the number being inserted is the biggest number in the array, meaning it needs to be inserted right after
37.              * all the numbers. This is where the count parameter comes in; the number is inserted at index count.
38.              *
39.             */
40.
41.             bool inserted = false;
42.             for (int i = 0; (i < numbers.Length) && (!inserted); i++)
43.             {
44.                 if (numbers[i] > value)
45.                 {
46.                     ShiftRight(numbers, i);
47.                     numbers[i] = value;
48.                     inserted = true; //number has been found (this also entails exiting the loop and the method)
49.                 }
50.             }
51.             if (!inserted) //the number has not been inserted, meaning no value in the array exists that is bigger than the number
52.             {
53.                 numbers[count] = value;
54.             }
55.         }
56.         static void ShiftRight(int[] numbers, int index)
57.         {
58.             for (int i = numbers.Length - 1; i > index; i--)
59.             {
60.                 numbers[i] = numbers[i - 1];
61.             }
62.             numbers[index] = 0;
63.         }
64.         static void Main(string[] args)
65.         {
66.             Sort();
67.         }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top