Sort Algorithms

 

#region  Copyright (c) 2005 Noble Spirit Corporation.
/*
{*******************************************************************}
{                                                                   }
{                Test Sort Algorithms DEVELOPER NOTE                    }
{                                                                

    }
{        Author            :        Anhr                                }
{       Created Time    :        10/20/2005                            }
{       Last Modify Time:        10/23/2005                            }
{       This File Memo    :                                            }
{                                                                  

  }
{                                                                   }
{                                                                

    }
{       The Modification Record :                                    }
{                                                                   }
{                                                                   }
{                                                                

    }
{                                                                   }
{*******************************************************************}
*/
#endregion        

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace T_Thread
{
    
/// <summary>
    
/// Algorithms 的摘要说明。
    
/// </summary>
    public class Algorithms : System.Windows.Forms.Form
    {
        
private System.Windows.Forms.TextBox tbxInput;
        
private System.Windows.Forms.Button btnSort;
        
private System.Windows.Forms.Label lblOutput;
        
private System.Windows.Forms.GroupBox groupBox1;
        
private System.Windows.Forms.RadioButton rbBubble;
        
private System.Windows.Forms.RadioButton rbSelection;
        
private System.Windows.Forms.RadioButton rbInsert;
        
private System.Windows.Forms.Button btnClear;
        
private System.Windows.Forms.RadioButton rbShell;
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>
        private System.ComponentModel.Container components = null;

        
public Algorithms()
        {
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();
            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }

        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>
        protected override void Dispose( bool disposing )
        {
            
if( disposing )
            {
                
if(components != null)
                {
                    components.Dispose();
                }
            }
            
base.Dispose( disposing );
        }

        
#region Windows 窗体设计器生成的代码
        
/// <summary>
        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
        
/// 此方法的内容。
        
/// </summary>
        private void InitializeComponent()
        {
            
this.tbxInput = new System.Windows.Forms.TextBox();
            
this.btnSort = new System.Windows.Forms.Button();
            
this.lblOutput = new System.Windows.Forms.Label();
            
this.groupBox1 = new System.Windows.Forms.GroupBox();
            
this.rbBubble = new System.Windows.Forms.RadioButton();
            
this.rbSelection = new System.Windows.Forms.RadioButton();
            
this.rbInsert = new System.Windows.Forms.RadioButton();
            
this.btnClear = new System.Windows.Forms.Button();
            
this.rbShell = new System.Windows.Forms.RadioButton();
            
this.groupBox1.SuspendLayout();
            
this.SuspendLayout();
            
// 
            
// tbxInput
            
// 
            this.tbxInput.Location = new System.Drawing.Point(168);
            
this.tbxInput.Name = "tbxInput";
            
this.tbxInput.Size = new System.Drawing.Size(21621);
            
this.tbxInput.TabIndex = 0;
            
this.tbxInput.Text = "";
            
// 
            
// btnSort
            
// 
            this.btnSort.Location = new System.Drawing.Point(24112);
            
this.btnSort.Name = "btnSort";
            
this.btnSort.Size = new System.Drawing.Size(8823);
            
this.btnSort.TabIndex = 1;
            
this.btnSort.Text = "Sort";
            
this.btnSort.Click += new System.EventHandler(this.btnSort_Click);
            
// 
            
// lblOutput
            
// 
            this.lblOutput.Location = new System.Drawing.Point(16144);
            
this.lblOutput.Name = "lblOutput";
            
this.lblOutput.Size = new System.Drawing.Size(21624);
            
this.lblOutput.TabIndex = 2;
            
this.lblOutput.Text = "Result";
            
// 
            
// groupBox1
            
// 
            this.groupBox1.Controls.Add(this.rbShell);
            
this.groupBox1.Controls.Add(this.rbSelection);
            
this.groupBox1.Controls.Add(this.rbBubble);
            
this.groupBox1.Controls.Add(this.rbInsert);
            
this.groupBox1.Location = new System.Drawing.Point(1632);
            
this.groupBox1.Name = "groupBox1";
            
this.groupBox1.Size = new System.Drawing.Size(21672);
            
this.groupBox1.TabIndex = 3;
            
this.groupBox1.TabStop = false;
            
this.groupBox1.Text = "Sort Mode";
            
// 
            
// rbBubble
            
// 
            this.rbBubble.Checked = true;
            
this.rbBubble.Location = new System.Drawing.Point(2416);
            
this.rbBubble.Name = "rbBubble";
            
this.rbBubble.Size = new System.Drawing.Size(6424);
            
this.rbBubble.TabIndex = 0;
            
this.rbBubble.TabStop = true;
            
this.rbBubble.Text = "Bubble";
            
// 
            
// rbSelection
            
// 
            this.rbSelection.Location = new System.Drawing.Point(2440);
            
this.rbSelection.Name = "rbSelection";
            
this.rbSelection.Size = new System.Drawing.Size(8824);
            
this.rbSelection.TabIndex = 1;
            
this.rbSelection.Text = "Selection";
            
// 
            
// rbInsert
            
// 
            this.rbInsert.Location = new System.Drawing.Point(11216);
            
this.rbInsert.Name = "rbInsert";
            
this.rbInsert.Size = new System.Drawing.Size(8824);
            
this.rbInsert.TabIndex = 4;
            
this.rbInsert.Text = "Insertion";
            
// 
            
// btnClear
            
// 
            this.btnClear.Location = new System.Drawing.Point(136112);
            
this.btnClear.Name = "btnClear";
            
this.btnClear.Size = new System.Drawing.Size(8823);
            
this.btnClear.TabIndex = 4;
            
this.btnClear.Text = "Clear Result";
            
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
            
// 
            
// rbShell
            
// 
            this.rbShell.Location = new System.Drawing.Point(11240);
            
this.rbShell.Name = "rbShell";
            
this.rbShell.Size = new System.Drawing.Size(6424);
            
this.rbShell.TabIndex = 5;
            
this.rbShell.Text = "Shell";
            
// 
            
// Algorithms
            
// 
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
            
this.ClientSize = new System.Drawing.Size(248173);
            
this.Controls.Add(this.btnClear);
            
this.Controls.Add(this.groupBox1);
            
this.Controls.Add(this.lblOutput);
            
this.Controls.Add(this.btnSort);
            
this.Controls.Add(this.tbxInput);
            
this.Name = "Algorithms";
            
this.Text = "Algorithms";
            
this.groupBox1.ResumeLayout(false);
            
this.ResumeLayout(false);

        }
        
#endregion

        
private void btnSort_Click(object sender, System.EventArgs e)
        {
            
char[] tmp = tbxInput.Text.ToCharArray ();
            
int startIndex = 0;

            
if ((null != tmp) && (0 != tmp.Length))
            {
                
if (true == rbBubble.Checked)
                {
                    lblOutput.Text 
= BubbleSort (tmp, ref startIndex);
                }
                
else if (true == rbInsert.Checked)
                {
                    lblOutput.Text 
= InsertSort (tmp, ref startIndex);
                }
                
else if (true == rbSelection.Checked)
                {
                    lblOutput.Text 
= SelectionSort (tmp, ref startIndex);
                }
                
else if (true == rbShell.Checked)
                {
                    lblOutput.Text 
= ShellSort (tmp, ref startIndex);
                }
            }
        }

        
int Key (char c)
        {
            
//
            
// Sort rule. The default is using ASCII.
            
//
            return c;
        }

        
bool Less (char a, char b)
        {
            
return Key(a) < Key(b);
        }

        
void ExchangePlace (ref char a, ref char b)
        {
            
char c = a;
            a 
= b;
            b 
= c;
        }

        
void CompExch (ref char a, ref char b)
        {
            
if (Less (b, a))
                ExchangePlace (
ref a, ref b);
        }

        
string BubbleSort (char[] tmp, ref int startIndex)
        {
            
for (int i=startIndex; i<tmp.Length; i++)
            {
                
for (int j=tmp.Length - 1; j>i; j--)
                {
                    CompExch (
ref tmp[j - 1], ref tmp[j]);
                }
            }

            
return new string (tmp);
        }

        
string SelectionSort (char[] tmp, ref int startIndex)
        {
            
for (int i=startIndex; i<tmp.Length; i++)
            {
                
int min = i;

                
for (int j=i+1; j<tmp.Length; j++)
                    
if (Less (tmp[j], tmp[min]))
                        min 
= j;

                ExchangePlace (
ref tmp[i], ref tmp[min]);
            }

            
return new string (tmp);
        }

        
string InsertSort (char[] tmp, ref int startIndex)
        {
            
for (int i=startIndex + 1; i<tmp.Length; i++)
                CompExch (
ref tmp[startIndex], ref tmp[i]);

            
for (int i=startIndex + 2; i<tmp.Length; i++)
            {
                
int j=i;
                
char c = tmp[i];

                
while (Less (c, tmp[j-1]))
                {
                    tmp[j] 
= tmp[j - 1];
                    j
--;
                }

                tmp[j] 
= c;
            }

            
return new string (tmp);
        }

        
string ShellSort (char[] tmp, ref int startIndex)
        {
            
int i, h;
            
for (h=startIndex; h<=(tmp.Length -startIndex)/9; h = 3*+1);

            
for ( ; h>0; h /= 3)
            {
                
for (i=startIndex + h; i<tmp.Length; i++)
                {
                    
int j = i;
                    
char c = tmp[i];

                    
while (j >= startIndex+&& Less (c, tmp[j-h]))
                    {
                        tmp[j] 
= tmp[j-h];
                        j
-=h;
                    }

                    tmp[j] 
= c;
                }
            }

            
return new string (tmp);
        }

        
private void btnClear_Click(object sender, System.EventArgs e)
        {
            lblOutput.Text 
= "Result";
        }
    }
}

posted on 2005-10-23 09:43  Easy Company  阅读(353)  评论(1编辑  收藏  举报

导航