1 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]; (minvalue) 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 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++) bucket[i] = new List<int>();
2 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; using System.Text;
3 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); 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++); if (less(pivot2, input[i])) exchange(input, i, gt--); i++;
4 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; 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);
5 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) txtbucketsort.text = ""; txtstrandsort.text = ""; txtquicksort.text = "";
6 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 = Math.Round(Convert.ToDecimal(watch3.Elapsed.TotalMilliseconds * 1000), 4); textbox11.text = rtmbucket.tostring();
7 )); 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(() => rtmquick); )); 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", 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) progressbar.begininvoke(new Action(() => progressbar.value = progress));
8 progressbar.value = progress; text)); public void SetLabelMessage(string text) if (progressbar.invokerequired) progressbar.begininvoke(new Action(() => lbltext.text = lbltext.text = text; public void SetTitle(string text) if (progressbar.invokerequired) progressbar.begininvoke(new Action(() => this.text = text)); this.text = text; public void SetIndeterminate(bool isindeterminate) if (progressbar.invokerequired) progressbar.begininvoke(new Action(() => if (isindeterminate) progressbar.style = ProgressBarStyle.Marquee; progressbar.style = ProgressBarStyle.Blocks; )); if (isindeterminate) progressbar.style = ProgressBarStyle.Marquee; 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) if (disposing && (components!= null))
9 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.autosize = true;
10 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.location = new System.Drawing.Point(18, 310);
11 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.location = new System.Drawing.Point(25, 522);
12 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.chartareas.add(chartarea2); legend2.name = "Legend1";
13 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.label13); this.controls.add(this.label12); this.controls.add(this.chart1); this.controls.add(this.txtquicksort); this.controls.add(this.txtstrandsort); this.controls.add(this.txtbucketsort); this.controls.add(this.richtextbox3); this.controls.add(this.richtextbox2); this.controls.add(this.richtextbox1); this.controls.add(this.button2); this.controls.add(this.textbox11); this.controls.add(this.textbox10); this.controls.add(this.textbox9); this.controls.add(this.label11); this.controls.add(this.label10); this.controls.add(this.label9); this.controls.add(this.label8); this.controls.add(this.label7); this.controls.add(this.label6); this.controls.add(this.label5); this.controls.add(this.label4); this.controls.add(this.label3); this.controls.add(this.button1);
14 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;