noor52 / Algorithm

1 stars 0 forks source link

QuickSort #5

Open noor52 opened 4 years ago

noor52 commented 4 years ago

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

namespace DataStructure { class QuickSort {

    public int[] GetInput()
    {
        Console.Write("Enter number of elements\n : ");
        int num = int.Parse(Console.ReadLine());

        int[] array = new int[num];

        Console.Write("Enter  " + num + " number Array :\n ");
        for (int i = 0; i < num; i++)
        {

            array[i] = int.Parse(Console.ReadLine());
        }

        return array;
    }

    public int[] GetSort(int[] array, int low, int high)
    {

        if (low < high)
        {
            int pi = partition(array, low, high);
            GetSort(array, low, pi - 1);
            GetSort(array, pi + 1, high);
        }
        return array;
    }
    int partition(int[] arr, int low, int high)
    {
        int temp, sample;
        int pivot = arr[high];    // pivot 
        int i = (low - 1);  // index of smaller element 
        for (int j = low; j <= high - 1; j++)
        {
            // if current element is smaller than the pivot 
            if (arr[j] < pivot)
            {
                i++;    // increment index of smaller element 
                temp = arr[j];
                arr[j] = arr[i];
                arr[i] = temp;
            }
        }

        sample = arr[i + 1];
        arr[i + 1] = arr[high];
        arr[high] = sample;
        return (i + 1);
    }

    public void GetSortedOutput(int[] output)
    {

        Console.WriteLine("Sorted Array");
        for (int i = 0; i < output.Length; i++)
        {
            Console.WriteLine(i + 1 + ":\t" + output[i] + "\n");
        }

        Console.ReadKey();
    }

}

}

noor52 commented 4 years ago

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

namespace DataStructure { class UserInput { static void Main(string[] args) { SelectionSort obj = new SelectionSort(); int[] returnInput = new int[100]; returnInput= obj.GetInput(); int[] output = new int[100]; output = obj.GetSort(returnInput, 0, returnInput.Length-1 ); obj.GetSortedOutput(output); } }

}

noor52 commented 4 years ago

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

namespace DataStructure { class UserInput { static void Main(string[] args) { QuickSort obj = new QuickSort(); int[] returnInput = new int[100]; returnInput= obj.GetInput(); int[] output = new int[100]; output = obj.GetSort(returnInput, 0, returnInput.Length-1 ); obj.GetSortedOutput(output); } }

}