Take off C# exam

Programming question 2: Use C# to implement insertion sort:

Sorting Thought Demonstration:



Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace direct insertion sort
{
    class Program
    {
        static void Main(string[] args)
        {
			//Initialization data
            int[] NumArray = new int[10] { 1, 4, 2, 8, 0, 7, 3, 5, 9, 6 };
			//The 0th element is sorted by default
			// start with the first element
            for (int i = 1; i < NumArray.Length; i++)
            {
				//if the current element is smaller than the previous element
                if (NumArray[i] < NumArray[i - 1])
                {
					//Create a temporary variable to store the data
                    int temp = NumArray[i];
                    int j;
					//It is convenient to move forward from the current data position. If it is smaller than the previous data, then move the previous data backward
					//until a position larger than the next position is found, put the temporary data in
                    for (j = i - 1; j >= 0 && NumArray[j] > temp; j--)
                    {
                        NumArray[j + 1] = NumArray[j];
                    }
                    NumArray[j + 1] = temp;
                }
            }
			// output
            foreach (int num in NumArray)
            {
                Console.WriteLine(num);
            }
        }
    }
}

Question 4: Design a stack class, implement stacking, popping, get the top element of the stack, get the amount of data in the stack, determine whether it is empty, and clear the data in the stack.

1. Create a stack interface: IStack.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace stack
{
    interface IStack<T>
    {
		// pop the stack
        T Pop();
		// get the top element of the stack
        T Peek();
		// push the stack
        void Push(T item);
		// clear
        void Clear();
		//get the number
        int Count { get; }
		//get the number
        int GetLength();
		// judged to be empty
        bool IsEmpty();
    }
}

Create stack class StuStack.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace stack
{
    class StuStack<T> : IStack<T>
    {
        private T[] Data;
        private int top = -1;
//There is space for parameter initialization stack
        public StuStack(int size)
        {
            Data = new T[size];
        }
//default constructor
//initialize its size to 10
        public StuStack():this(10)
        {

        }

//Get the number of data in the stack
        public int Count
        {
            get { return top+1; }
        }
// clear stack
        public void Clear()
        {
            top = -1;
        }
//get length
        public int GetLength()
        {
            return Count;
        }
// determine if it is empty
        public bool IsEmpty()
        {
            return Count == 0;
        }
// get the top element of the stack
        public T Peek()
        {
            return Data[top];
        }
// pop the stack
        public T Pop()
        {
            T cur = Data[top];
            --top;
            return cur;
        }
// push the stack
        public void Push(T item)
        {
			if(Data.length==Count)
			{
				// stack is full, throw exception
				throw new Exception("Not enough space");
			}
			else
			{	
				Data[top + 1] = item;
				++top;
			}
        }
    }
}

For various sorting algorithms, move to the following post:

https://blog.csdn.net/u013284706/article/details/73740924

If you have any questions, please ask:



TonyChen

2018.4.22


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324658716&siteId=291194637