Jump to content

C# example of sorting and search algorithms.


axdbapowerXD

Recommended Posts

Hi!

I create this topic just for C# example of sorting and search algorithms. For your information I'm just a student. I'm from Sweden, so I write in swedish in WriteLine.

I hope that you enjoy! :)

 

static void Main(string[] args)
        {
            while (true)
            {
                //Creates an Array with value 100
                int[] myArray = new int[100];

               

                //Creates randomizer to make random numbers
                Random randomizer = new Random();

                for (int i = 0; i < myArray.Length; i++)
                {
                    myArray = randomizer.Next(1000);
                }

                //May choose among various sorting algorithms for sorting number (for example Bubble sort)
                SelectionSort(myArray);

                //Makes it "fancier"
                Console.Write("{ ");
                for (int i = 0; i < myArray.Length; i++)
                {
                    Console.Write(myArray);
                    if (i < myArray.Length - 1)
                    {
                        Console.Write(", ");
                    }
                }

                Console.Write(" }\n");
                Console.Write("Mata in talet som du vill hitta: ");
                int searchNumber = int.Parse(Console.ReadLine());

                //Choose between Linear and Binary search:
                int pos = LinearSearch(searchNumber, myArray);

               
                if (pos == -1)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Talet finns inte.\nTryck 'ENTER' för att försöka igen.");
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine("Talet finns på position: "+ pos);
                }

                Console.ReadLine();
            }
        }
        //Here is four methods for example (two sorting and two search algorithms)
        private static void SelectionSort(int[] myArray)
        {
            //Going through all non-sorted elements and find the smallest
            for (int i = 0; i < myArray.Length; i++)
            {
                //Assume that the first unsorted position contains the smallest value
                int minPos = i;
                for (int j = i; j < i; j++)
                {
                    //If we find a smaller element, update minPos
                    if (myArray[j] < myArray[minPos])
                    {
                        minPos = j;
                    }
                }
                //byt plats
                int tmp = myArray;
                myArray = myArray[minPos];
                myArray[minPos] = tmp;
            }
        }

        private static void BubbleSort(int[] myArray)
        {
            for (int i = myArray.Length - 1; i > 0; i--)
            {
                for (int j = 0; j < i; j++)
                {
                    if (myArray[j] > myArray[j + 1])
                    {
                        int tmp = myArray[j];
                        myArray[j] = myArray[j + 1];
                        myArray[j + 1] = tmp;
                    }
                }
            }
        }

        private static int BinarySearch(int searchNumber, int[] myArray)
        {
            int min = 0;
            int max = myArray.Length - 1;
            while (min < max)
            {
                int mid = (min + max) / 2;
                if (searchNumber == myArray[mid])
                {
                    return mid;
                }
                else if (searchNumber < myArray[mid])
                {
                    max = mid - 1;
                }
                else
                {
                    min = mid + 1;
                }
            }

            return -1;
        }

        private static int LinearSearch(int searchNumber, int[] searchArray)
        {
            int position = -1;

            for (int i = 0; i < searchArray.Length; i++)
            {
                if (searchNumber == searchArray)
                {
                    position = i;
                    break;
                }
            }


            return position;
        }

Link to comment
  • 1 year later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...