LISTING PROGRAM
using System;
using System.Collections.Generic; using System.Linq;
using System.Windows.Forms;
namespace SortingApplication {
static class Program {
/// <summary>
/// The main entry point for the application. /// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FormAplikasiSorting());
} } }
using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
namespace SortingApplication {
class BucketSort {
public static void bsort3(int[] integers) {
//Verify input
if (integers == null || integers.Length == 0) return;
//Find the maximum and minimum values in the array int maxValue = integers[0]; //start with first element int minValue = integers[0];
//Note: start from index 1
for (int i = 1; i < integers.Length; i++) {
if (integers[i] > maxValue) maxValue = integers[i];
if (integers[i] < minValue) minValue = integers[i]; }
//Create a temporary "bucket" to store the values in order //each value will be stored in its corresponding index //scooting everything over to the left as much as possible (minValue)
//e.g. 34 => index at 34 - minValue
List<int>[] bucket = new List<int>[maxValue - minValue + 1];
//Initialize the bucket
for (int i = 0; i < bucket.Length; i++) {
}
//Move items to bucket
for (int i = 0; i < integers.Length; i++) {
bucket[integers[i] - minValue].Add(integers[i]); }
//Move items in the bucket back to the original array in order int k = 0; //index for original array
for (int i = 0; i < bucket.Length; i++) {
if (bucket[i].Count > 0) {
for (int j = 0; j < bucket[i].Count; j++) {
integers[k] = bucket[i][j]; k++;
} } } } } }
using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
namespace SortingApplication {
class StrandSort {
public int[] Sort(int[] data){ int[] arr = data;
for (int i = 1; i < arr.Length; i++) {
int j = i - 1; int index = -1;
while (arr[i] < arr[j]) { index = j;
j--; if (j < 0) break; }
if(index == -1) continue;
int temp = arr[i];
for (int k = i; k > index; k--) { arr[k] = arr[k - 1];
}
arr[index] = temp; }
return arr; }
} }
using System;
using System.Collections.Generic; using System.Linq;
namespace SortingApplication {
class QuickSortDualPivot {
public int[] sort(int[] input) {
int[] data = input;
sort(data, 0, data.Length - 1);
return data; }
private void sort(int[] input, int lowIndex, int highIndex) {
if (highIndex <= lowIndex) return;
int pivot1 = input[lowIndex]; int pivot2 = input[highIndex];
if (pivot1 > pivot2) {
exchange(input, lowIndex, highIndex); pivot1 = input[lowIndex];
pivot2 = input[highIndex];
//sort(input, lowIndex, highIndex); }
else if (pivot1 == pivot2) {
int swapIndex = lowIndex;
while (pivot1 == pivot2 && swapIndex <= highIndex) {
swapIndex++;
exchange(input, swapIndex, highIndex); pivot2 = input[highIndex];
}
if (pivot1 > pivot2) {
exchange(input, lowIndex, highIndex); pivot1 = input[lowIndex];
pivot2 = input[highIndex];
//sort(input, lowIndex, highIndex); }
}
int i = lowIndex + 1; int lt = lowIndex + 1; int gt = highIndex - 1;
while (i <= gt) {
if (less(input[i], pivot1)) {
exchange(input, i++, lt++); }
else if (less(pivot2, input[i])) {
exchange(input, i, gt--); }
else {
i++; }
exchange(input, lowIndex, --lt); exchange(input, highIndex, ++gt);
sort(input, lowIndex, lt - 1);
//if (less (input[lt], input[gt])) sort (input, lt+1, gt-1); sort(input, lt + 1, gt - 1);
sort(input, gt + 1, highIndex); }
public bool less(int a, int b) {
return a < b; }
public void exchange(int[] input, int i, int r) {
if (i >= input.Length) return;
if (input[i] == null) {
int temp = input[i]; input[i] = input[r]; input[r] = temp; }
else {
int temp = input[i]; input[i] = input[r]; input[r] = temp; }
} } }
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data;
using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms; using System.Diagnostics; using System.Threading;
using System.Windows.Forms.DataVisualization.Charting;
namespace SortingApplication {
public partial class FormAplikasiSorting : Form {
int[] data,data2,data3,strand,quick,bucket; int n, nilaimaks;
Random r = new Random();
ProgressDialog progressWindow;
ToolTip tooltip = new ToolTip(); Point? prevPosition = null;
public FormAplikasiSorting() {
InitializeComponent();
// Console.WriteLine("Bucket Sort");
// int[] sortBucket = new BucketSort().sort(data); // printArray(sortBucket);
public void printArray(int[] input) { for (int i = 0; i < input.Length; i++) {
Console.Write(input[i] + ", "); }
Console.WriteLine(); }
private void button1_Click(object sender, EventArgs e) {
richTextBox1.Text = null; richTextBox2.Text = null; richTextBox3.Text = null;
Thread thread = new Thread(new ThreadStart(GenerateData)); progressWindow = new ProgressDialog();
progressWindow.SetTitle("Generating random data"); progressWindow.SetLabelMessage("generating..."); thread.Start();
progressWindow.ShowDialog(); }
private void GenerateData() {
StringBuilder sb = new StringBuilder(); n = Convert.ToInt32(maxitem.Text);
nilaimaks = Convert.ToInt32(maxitemvalue.Text); data = new int[n];
data2 = new int[n]; data3 = new int[n];
for (int i = 0; i < n; i++) {
int j = r.Next(1, nilaimaks); data[i] = j;
data2[i] = j; data3[i] = j; sb.Append(j); if (i != n - 1) sb.Append(", "); }
progressWindow.SetLabelMessage("displaying data ..."); this.BeginInvoke(new Action(() => {
richTextBox1.Text = sb.ToString(); }));
this.BeginInvoke(new Action(() => {
richTextBox2.Text = sb.ToString(); }));
this.BeginInvoke(new Action(() => {
richTextBox3.Text = sb.ToString(); }));
// Close the dialog if it hasn't been already if (progressWindow.InvokeRequired)
progressWindow.BeginInvoke(new Action(() => progressWindow.Close()));
}
void Button2Click(object sender, EventArgs e) {
Thread thread = new Thread(new ThreadStart(Sort)); progressWindow = new ProgressDialog();
progressWindow.SetTitle("Sorting Data"); progressWindow.SetLabelMessage("sorting ..."); progressWindow.SetIndeterminate(true);
thread.Start();
progressWindow.ShowDialog(); }
private void Sort() {
//chart1.Series.Clear();
//while(chart1.Series.Count > 0) //{
// foreach (var series in chart1.Series) // {
// series.Points.Clear(); // }
// MessageBox.Show("ada"); //}
StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); StringBuilder sb3 = new StringBuilder();
decimal rtmQuick = 0; decimal rtmStrand = 0; decimal rtmBucket = 0;
Stopwatch watch1 = new Stopwatch();// running time Stopwatch watch2 = new Stopwatch();// running time Stopwatch watch3 = new Stopwatch();// running time progressWindow.SetLabelMessage("sorting using QuickSortDualPivot");
watch1 = Stopwatch.StartNew(); watch1.Restart();
quick = new QuickSortDualPivot().sort(data); watch1.Stop();
this.BeginInvoke(new Action(() => {
rtmQuick =
Math.Round(Convert.ToDecimal(watch1.Elapsed.TotalMilliseconds * 1000), 4); textBox9.Text = rtmQuick.ToString();
}));
progressWindow.SetLabelMessage("sorting using StrandSort"); watch2 = Stopwatch.StartNew();
watch2.Restart();
strand = new StrandSort().Sort(data2); watch2.Stop();
this.BeginInvoke(new Action(() => {
rtmStrand =
Math.Round(Convert.ToDecimal(watch2.Elapsed.TotalMilliseconds * 1000), 4); textBox10.Text = rtmStrand.ToString();
}));
progressWindow.SetLabelMessage("sorting using BucketSort"); watch3 = Stopwatch.StartNew();
watch3.Restart(); bucket = data3;
BucketSort.bsort3(bucket); watch3.Stop();
this.BeginInvoke(new Action(() => {
rtmBucket =
}));
for (int i = 0; i < n; i++) {
sb1.Append(data[i].ToString()); sb2.Append(data2[i].ToString()); sb3.Append(data3[i].ToString()); if (i != n - 1)
{
sb1.Append(", "); sb2.Append(", "); sb3.Append(", "); }
}
this.BeginInvoke(new Action(() => {
txtStrandSort.Text += sb2.ToString(); txtBucketSort.Text += sb3.ToString(); txtQuickSort.Text += sb1.ToString(); foreach (var series in chart1.Series) {
series.Points.Clear(); }
chart1.Series[0].Points.AddXY("Bucket Sort", rtmBucket); chart1.Series[0].Points.AddXY("Strand Sort", rtmStrand); chart1.Series[0].Points.AddXY("Quick Sort 2 Pivot", rtmQuick);
}));
// Close the dialog if it hasn't been already if (progressWindow.InvokeRequired)
progressWindow.BeginInvoke(new Action(() => progressWindow.Close()));
}
private void FormAplikasiSorting_Load(object sender, EventArgs e) {
} } }
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data;
using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms;
namespace SortingApplication {
public partial class ProgressDialog : Form {
public ProgressDialog() {
InitializeComponent(); SetIndeterminate(true); }
public void UpdateProgress(int progress) {
if (progressBar.InvokeRequired)
else
progressBar.Value = progress; }
public void SetLabelMessage(string text) {
if (progressBar.InvokeRequired)
progressBar.BeginInvoke(new Action(() => lblText.Text = text));
else
lblText.Text = text; }
public void SetTitle(string text) {
if (progressBar.InvokeRequired)
progressBar.BeginInvoke(new Action(() => this.Text = text)); else
this.Text = text; }
public void SetIndeterminate(bool isIndeterminate) {
if (progressBar.InvokeRequired) {
progressBar.BeginInvoke(new Action(() => {
if (isIndeterminate)
progressBar.Style = ProgressBarStyle.Marquee; else
progressBar.Style = ProgressBarStyle.Blocks; }
)); } else {
if (isIndeterminate)
progressBar.Style = ProgressBarStyle.Marquee; else
progressBar.Style = ProgressBarStyle.Blocks; }
}
private void ProgressDialog_Load(object sender, EventArgs e) {
} } }
namespace SortingApplication {
partial class FormAplikasiSorting {
/// <summary>
/// Required designer variable. /// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used. /// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
{
components.Dispose(); }
base.Dispose(disposing); }
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary>
private void InitializeComponent() {
System.Windows.Forms.DataVisualization.Charting.ChartArea
chartArea2 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); System.Windows.Forms.DataVisualization.Charting.Legend legend2 = new System.Windows.Forms.DataVisualization.Charting.Legend();
System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series();
this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.maxitem = new System.Windows.Forms.TextBox(); this.maxitemvalue = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.label6 = new System.Windows.Forms.Label(); this.label7 = new System.Windows.Forms.Label(); this.label8 = new System.Windows.Forms.Label(); this.label9 = new System.Windows.Forms.Label(); this.label10 = new System.Windows.Forms.Label(); this.label11 = new System.Windows.Forms.Label(); this.textBox9 = new System.Windows.Forms.TextBox(); this.textBox10 = new System.Windows.Forms.TextBox(); this.textBox11 = new System.Windows.Forms.TextBox(); this.button2 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.richTextBox2 = new System.Windows.Forms.RichTextBox(); this.richTextBox3 = new System.Windows.Forms.RichTextBox(); this.txtBucketSort = new System.Windows.Forms.RichTextBox(); this.txtStrandSort = new System.Windows.Forms.RichTextBox(); this.txtQuickSort = new System.Windows.Forms.RichTextBox(); this.chart1 = new
System.Windows.Forms.DataVisualization.Charting.Chart(); this.label12 = new System.Windows.Forms.Label(); this.label13 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit(); this.SuspendLayout();
// // label1 //
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(22, 51); this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(97, 15); this.label1.TabIndex = 0;
this.label1.Text = "Max Item "; //
// label2 //
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label2.Location = new System.Drawing.Point(22, 75); this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(92, 15); this.label2.TabIndex = 1;
this.label2.Text = "Max Item Value"; //
// maxitem //
this.maxitem.Location = new System.Drawing.Point(125, 51); this.maxitem.Name = "maxitem";
this.maxitem.Size = new System.Drawing.Size(96, 20); this.maxitem.TabIndex = 2;
this.maxitem.Text = "1000"; //
// maxitemvalue //
this.maxitemvalue.Location = new System.Drawing.Point(125, 75); this.maxitemvalue.Name = "maxitemvalue";
this.maxitemvalue.Size = new System.Drawing.Size(96, 20); this.maxitemvalue.TabIndex = 3;
this.maxitemvalue.Text = "1000"; //
// button1 //
this.button1.Location = new System.Drawing.Point(257, 71); this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 5;
this.button1.Text = "Data Acak";
this.button1.UseVisualStyleBackColor = true; this.button1.Click += new
System.EventHandler(this.button1_Click); //
// label3 //
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(18, 104); this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(63, 13); this.label3.TabIndex = 10;
this.label3.Text = "Bucket Sort"; //
// label4 //
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(241, 104); this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(60, 13); this.label4.TabIndex = 13;
this.label4.Text = "Strand Sort"; //
// label5 //
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(468, 104); this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(93, 13); this.label5.TabIndex = 14;
this.label5.Text = "Quick Sort 2 Pivot"; //
// label6 //
this.label6.AutoSize = true;
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(125, 13); this.label6.TabIndex = 15;
this.label6.Text = "Hasil Sorting Bucket Sort"; //
// label7 //
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(241, 310); this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(122, 13); this.label7.TabIndex = 16;
this.label7.Text = "Hasil Sorting Strand Sort"; //
// label8 //
this.label8.AutoSize = true;
this.label8.Location = new System.Drawing.Point(468, 310); this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(155, 13); this.label8.TabIndex = 17;
this.label8.Text = "Hasil Sorting Quick Sort 2 Pivot"; //
// label9 //
this.label9.AutoSize = true;
this.label9.Location = new System.Drawing.Point(22, 506); this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(73, 13); this.label9.TabIndex = 18;
this.label9.Text = "Running Time"; //
// label10 //
this.label10.AutoSize = true;
this.label10.Location = new System.Drawing.Point(241, 506); this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(73, 13); this.label10.TabIndex = 19;
this.label10.Text = "Running Time"; //
// label11 //
this.label11.AutoSize = true;
this.label11.Location = new System.Drawing.Point(468, 506); this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(73, 13); this.label11.TabIndex = 20;
this.label11.Text = "Running Time"; //
// textBox9 //
this.textBox9.Location = new System.Drawing.Point(471, 522); this.textBox9.Name = "textBox9";
this.textBox9.Size = new System.Drawing.Size(96, 20); this.textBox9.TabIndex = 21;
//
// textBox10 //
this.textBox10.Location = new System.Drawing.Point(244, 522); this.textBox10.Name = "textBox10";
this.textBox10.Size = new System.Drawing.Size(96, 20); this.textBox10.TabIndex = 22;
//
// textBox11 //
this.textBox11.Name = "textBox11";
this.textBox11.Size = new System.Drawing.Size(96, 20); this.textBox11.TabIndex = 23;
//
// button2 //
this.button2.Location = new System.Drawing.Point(348, 71); this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 24;
this.button2.Text = "Sorting";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.Button2Click); //
// richTextBox1 //
this.richTextBox1.Location = new System.Drawing.Point(22, 120); this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(196, 173); this.richTextBox1.TabIndex = 25;
this.richTextBox1.Text = ""; //
// richTextBox2 //
this.richTextBox2.Location = new System.Drawing.Point(244, 120); this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(196, 173); this.richTextBox2.TabIndex = 26;
this.richTextBox2.Text = ""; //
// richTextBox3 //
this.richTextBox3.Location = new System.Drawing.Point(471, 120); this.richTextBox3.Name = "richTextBox3";
this.richTextBox3.Size = new System.Drawing.Size(196, 173); this.richTextBox3.TabIndex = 27;
this.richTextBox3.Text = ""; //
// txtBucketSort //
this.txtBucketSort.Location = new System.Drawing.Point(22, 326); this.txtBucketSort.Name = "txtBucketSort";
this.txtBucketSort.Size = new System.Drawing.Size(196, 173); this.txtBucketSort.TabIndex = 28;
this.txtBucketSort.Text = ""; //
// txtStrandSort //
this.txtStrandSort.Location = new System.Drawing.Point(244, 326); this.txtStrandSort.Name = "txtStrandSort";
this.txtStrandSort.Size = new System.Drawing.Size(196, 173); this.txtStrandSort.TabIndex = 29;
this.txtStrandSort.Text = ""; //
// txtQuickSort //
this.txtQuickSort.Location = new System.Drawing.Point(471, 326); this.txtQuickSort.Name = "txtQuickSort";
this.txtQuickSort.Size = new System.Drawing.Size(196, 173); this.txtQuickSort.TabIndex = 30;
this.txtQuickSort.Text = ""; //
// chart1 //
chartArea2.Name = "ChartArea1";
this.chart1.Legends.Add(legend2);
this.chart1.Location = new System.Drawing.Point(78, 551); this.chart1.Name = "chart1";
series2.ChartArea = "ChartArea1"; series2.Legend = "Legend1";
series2.LegendText = "Running Time"; series2.Name = "Series1";
this.chart1.Series.Add(series2);
this.chart1.Size = new System.Drawing.Size(545, 155); this.chart1.TabIndex = 31;
this.chart1.Text = "chart1"; //
// label12 //
this.label12.AutoSize = true;
this.label12.Font = new System.Drawing.Font("MS Reference Sans Serif", 8.25F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label12.Location = new System.Drawing.Point(23, 9); this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(676, 15); this.label12.TabIndex = 32;
this.label12.Text = "IMPLEMENTASI DAN ANALISIS ALGORITMA BUCKET SORT, STRAND SORT DAN QUICK SORT 2 PIV" +
"OT"; //
// label13 //
this.label13.AutoSize = true;
this.label13.Font = new System.Drawing.Font("MS Reference Sans Serif", 8.25F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label13.Location = new System.Drawing.Point(170, 24); this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(372, 15); this.label13.TabIndex = 33;
this.label13.Text = "DALAM PENSORTIRAN DATA YANG BERJUMLAH BANYAK";
//
// FormAplikasiSorting //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(708, 702);
this.Controls.Add(this.maxitemvalue); this.Controls.Add(this.maxitem); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Name = "FormAplikasiSorting"; this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Aplikasi Sorting Data"; this.Load += new
System.EventHandler(this.FormAplikasiSorting_Load);
((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit(); this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.RichTextBox txtQuickSort; private System.Windows.Forms.RichTextBox txtStrandSort; private System.Windows.Forms.RichTextBox txtBucketSort; private System.Windows.Forms.RichTextBox richTextBox3; private System.Windows.Forms.RichTextBox richTextBox2; private System.Windows.Forms.RichTextBox richTextBox1;
#endregion
private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox maxitem; private System.Windows.Forms.TextBox maxitemvalue; private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label6; private System.Windows.Forms.Label label7; private System.Windows.Forms.Label label8; private System.Windows.Forms.Label label9; private System.Windows.Forms.Label label10; private System.Windows.Forms.Label label11; private System.Windows.Forms.TextBox textBox9; private System.Windows.Forms.TextBox textBox10; private System.Windows.Forms.TextBox textBox11; private System.Windows.Forms.Button button2;
private System.Windows.Forms.DataVisualization.Charting.Chart chart1; private System.Windows.Forms.Label label12;
private System.Windows.Forms.Label label13; }