Click here to Skip to main content
12,623,027 members (30,412 online)
Click here to Skip to main content

Stats

37.7K views
237 downloads
14 bookmarked
Posted

Iterative Quick Sort

, 25 Jan 2008 CPOL
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)

Share

About the Author

Pete Goodsall
Software Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161128.1 | Last Updated 25 Jan 2008
Article Copyright 2008 by Pete Goodsall
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid