Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# PROGRAMMING 1.Write a C# Bubble Sort and Quick Sort into two methods. Make su

ID: 3886625 • Letter: C

Question

C# PROGRAMMING

1.Write a C# Bubble Sort and Quick Sort into two methods. Make sure that both of the methods work correctly. Show the testing results for each of the two methods using a randomly created integer array of 50 elements.

2. Write a C# main program to compare the time spans for running 1,000 times on both sorting methods above (Bubble Sort and QuickSort) for randomly created 1,000 integers ranging from 100 to 1000.   

OUTPUT SHOULD LOOK LIKE BELOW:

=================C# code and testing result for Sort Method>

individual C# main program includes only Sort One on 50-element array

=================C# code and testing result for Sort Method Two=================

individual C# main program includes Only Sort Two  on 50-element array

=================C# code and running time spans for the two methods ==================

Include both Sorting methods in the program and run 1,000 times on 1000-element array

Explanation / Answer

Quicksort

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

namespace qs
{
    class quickSort
    {

        private int[] a;
        private int size;
       
        public void QuickSort()
        {
            size = 0;
        }
       
        public void sort(int left, int right)
        {
            int pivot, lefte, righte;

            lefte = left;
            righte = right;
            pivot = a[left];

            while (left < right)
            {
                while ((a[right] >= pivot) && (left < right))
                {
                    right--;
                }

                if (left != right)
                {
                    a[left] = a[right];
                    left++;
                }

                while ((a[left] <= pivot) && (left < right))
                {
                    left++;
                }

                if (left != right)
                {
                    a[right] = a[left];
                    right--;
                }
            }

            a[left] = pivot;
            pivot = left;
            left = lefte;
            right = righte;

            if (left < pivot)
            {
                sort(left, pivot - 1);
            }

            if (right > pivot)
            {
                sort(pivot + 1, right);
            }
        }

        public static void Main()
        {
            quickSort q = new quickSort();

            int[] arr = { 10, 2, 19, 5, 1 };
            int[] arr1 = new int[1000];
            Console.WriteLine("The Array is : ");
            foreach (int a in arr)
               Console.Write(a + " ");
            Console.WriteLine();
            q.a = arr;
            q.size = q.a.Length;

            q.QuickSort();
            Console.WriteLine("The Sorted Array :");
            foreach (int a in arr)
                Console.Write(a + " ");
            Console.WriteLine();
            Random random = new Random();
            for (int i = 0; i < 100; i++)
            {
                arr1[i] = random.Next(100, 1000);
            }
            q.a = arr1;
            q.size = q.a.Length;
            Console.WriteLine("Sorting 1000 integers starts.....");
            var watch = System.Diagnostics.Stopwatch.StartNew();
            q.QuickSort();
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
            Console.WriteLine("Sorting 1000 integers completed.....");
            Console.WriteLine("Elapsed Time " + elapsedMs + " milliseconds");

            Console.ReadKey();
        }
    }
}

Bubblesort

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

namespace BubbleSort
{
    class bubblesort
    {
        static void Main(string[] args)
        {
            int[] arr = { 10, 2, 19, 5, 1 };
            int[] arr1 = new int[1000];
            int temp;
           
            Console.WriteLine("The Array is : ");
            foreach (int a in arr)
                Console.Write(a + " ");
            Console.WriteLine();
            for (int j = 0; j <= arr.Length - 2; j++)
            {
                for (int i = 0; i <= arr.Length - 2; i++)
                {
                    if (arr[i] > arr[i + 1])
                    {
                        temp = arr[i + 1];
                        arr[i + 1] = arr[i];
                        arr[i] = temp;
                    }
                }
            }
            Console.WriteLine("The Sorted Array :");
            foreach (int a in arr)
                Console.Write(a + " ");
            Random random = new Random();
            for (int i = 0; i < 100; i++)
            {
                arr1[i] = random.Next(100, 1000);
            }
            Console.WriteLine();
            Console.WriteLine("Sorting 1000 integers starts.....");
            var watch = System.Diagnostics.Stopwatch.StartNew();
            for (int j = 0; j <= arr1.Length - 2; j++)
            {
                for (int i = 0; i <= arr1.Length - 2; i++)
                {
                    if (arr1[i] > arr1[i + 1])
                    {
                        temp = arr1[i + 1];
                        arr1[i + 1] = arr1[i];
                        arr1[i] = temp;
                    }
                }
            }
           
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
            Console.WriteLine("Sorting 1000 integers completed.....");
            Console.WriteLine("Elapsed Time " + elapsedMs + " milliseconds");
            Console.ReadLine();
        }
    }
}