This code shows how to use a minimum Windows Gui program to display the results of a bubble sort of an integer array.
A simple Bubble Sort
// sorting an integer array's values in ascending order
// a Windows GUI program with a button and label
// compiled with C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
using System;
using System.Windows.Forms; // GUI stuff
namespace BubbleSorterSpace
{
public class BubbleSorter : System.Windows.Forms.Form
{
private System.Windows.Forms.Button sortButton;
private System.Windows.Forms.Label resultLabel;
// required designer variable
private System.ComponentModel.Container components = null;
public BubbleSorter()
{
// required for GUI support
InitializeComponent();
}
// clean up any resources being used
protected override void Dispose( bool disposing )
{
if ( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.sortButton = new System.Windows.Forms.Button();
this.resultLabel = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// sortButton
//
this.sortButton.Location = new System.Drawing.Point(109, 8);
this.sortButton.Name = "sortButton";
this.sortButton.TabIndex = 0;
this.sortButton.Text = "Sort Data";
this.sortButton.Click += new System.EventHandler(this.sortButton_Click);
//
// resultLabel
//
this.resultLabel.Location = new System.Drawing.Point(8, 40);
this.resultLabel.Name = "resultLabel";
this.resultLabel.Size = new System.Drawing.Size(280, 64);
this.resultLabel.TabIndex = 1;
//
// the Window Form
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 109);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.resultLabel, this.sortButton});
this.Name = "bubbleSort";
this.Text = "C# bubble sort";
this.ResumeLayout(false);
}
static void Main()
{
Application.Run(new BubbleSorter());
}
private void sortButton_Click( object sender,
System.EventArgs e )
{
int[] ar = { 22, 6, 4, -7, 13, 12, 89, 68, 37, 44, -15 };
resultLabel.Text = "items in original order\n";
for ( int i = 0; i < ar.Length; i++ )
resultLabel.Text += " " + ar[ i ];
// sort items of array ar
BubbleSort( ar );
resultLabel.Text += "\n\nitems in ascending order\n";
for ( int i = 0; i < ar.Length; i++ )
resultLabel.Text += " " + ar[ i ];
}
// sort the items of an array using bubble sort
public void BubbleSort( int[] ar )
{
for ( int pass = 1; pass < ar.Length; pass++ )
for ( int i = 0; i < ar.Length - 1; i++ )
if ( ar[ i ] > ar[ i + 1 ] )
Swap( ar, i );
}
// swap two items of an array
public void Swap( int[] ar, int first )
{
int hold;
hold = ar[ first ];
ar[ first ] = ar[ first + 1 ];
ar[ first + 1 ] = hold;
}
}
}
Lardmeister 461 Posting Virtuoso
arfat32 0 Newbie Poster
hasnainhaider 0 Newbie Poster
kplcjl 17 Junior Poster
kplcjl 17 Junior Poster
kplcjl 17 Junior Poster
kplcjl 17 Junior Poster
kplcjl 17 Junior Poster
kplcjl 17 Junior Poster
kplcjl 17 Junior Poster
Momerath 1,327 Nearly a Senior Poster Featured Poster
kplcjl 17 Junior Poster
kplcjl 17 Junior Poster
kplcjl 17 Junior Poster
kplcjl 17 Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.