Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C#

Iterative Quick Sort

Rate me:
Please Sign up or sign in to vote.
3.61/5 (7 votes)
25 Jan 2008CPOL3 min read 60.6K   281   14  
An iterative implementation of Quicksort
using System;
using System.Text;
using System.Windows.Forms;

namespace IterativeQuickSort
{
    public partial class frmSort : Form
    {
        private int nDataSz = 100000;
        private int[] TestData;
        private bool bSortUp;

        public frmSort()
        {
            InitializeComponent();

            bSortUp = optSortUp.Checked = true;
            btnDoSort.Enabled = false;

            numUpDnSz.Minimum = 2;
            numUpDnSz.Maximum = 1000000;
            numUpDnSz.Increment = 100000;
            numUpDnSz.Value = nDataSz;
        }

        private void GenerateExampleData()
        {
            int i;
            Random PRNG = new Random();

            btnDoSort.Enabled = false;

            Cursor Csr = this.Cursor;
            this.Cursor = Cursors.WaitCursor;

            nDataSz = (int)numUpDnSz.Value;
            TestData = new int[nDataSz];

            for (i = 0; i < nDataSz; i++)
                TestData[i] = PRNG.Next();

            DisplayDataToTextBox(tbxInput, TestData);
            tbxOutput.Text = "";
            btnDoSort.Enabled = true;

            this.Cursor = Csr;
        }

        private void btnDoSort_Click(object sender, EventArgs e)
        {
            Cursor Csr = this.Cursor;
            this.Cursor = Cursors.WaitCursor;

            IttvQuickSort iqs = new IttvQuickSort();
            iqs.SortData = TestData; // NB this is copied, ie by value, not by reference

            double Tmr = 0.0;
            Stopwatch.Start();

            if (!iqs.PerformSort(bSortUp))
                MessageBox.Show("Unknown error occurred in execution of sort");

            Stopwatch.Stop();
            Tmr = Stopwatch.time;
            lblTiming.Text = String.Format("{0:F} msec", Tmr);

            DisplayDataToTextBox(tbxOutput, iqs.SortData);

            this.Cursor = Csr;
        }

        private void DisplayDataToTextBox(TextBox tbx, int[] Data)
        {
            int i;
            StringBuilder sbData = new StringBuilder();

            for (i = 0; i < nDataSz; i++)
                sbData.AppendLine(Data[i].ToString());

            tbx.Text = sbData.ToString();
        }

        private void btnGenData_Click(object sender, EventArgs e)
        {
            GenerateExampleData();
        }

        private void optSortUp_CheckedChanged(object sender, EventArgs e)
        {
            if (!bSortUp)
            {
                bSortUp = true;
                tbxOutput.Text = "";
            }
        }

        private void optSortDn_CheckedChanged(object sender, EventArgs e)
        {
            if (bSortUp)
            {
                bSortUp = false;
                tbxOutput.Text = "";
            }
        }

        private void numUpDnSz_ValueChanged(object sender, EventArgs e)
        {
            btnDoSort.Enabled = false;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions