* semoga berkah
Linear Search:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sorting
{
public partial class frm_linear : Form
{
int count = 0;
int x;
public frm_linear()
{
InitializeComponent();
}
private static int search(int[] arr, int x)
{
int n = arr.Length;
for (int i = 0; i < n; i++)
{
if (arr[i] == x)
return i;
}
return -1;
}
private void btn_search_Click(object sender, EventArgs e)
{
// 1. Simpan data dari listbox kedalam array
int n = lst_data.Items.Count;
int[] data = new int[n];
// pindahkan data kedalam array
for (int i = 0; i < n; i++)
{
data[i] = int.Parse(lst_data.Items[i].ToString());
}
// cari data
int cari = int.Parse(txt_search.Text);
// 2. Panggil fungsi search
int result = search(data, cari);
// buat array index
int[] index = new int[n];
// hitung posisi indexnya dan jumlah data
for (int i = 0; i < n; i++)
{
if (data[i] == cari) {
index[count] = i;
count++;
}
}
// tampilkan hasil jumlah data dan index di label
if (result == -1)
{
lbl_hasil.Text = lbl_hasil.Text +"Data " + cari + " tidak ditemukan!";
lbl_jumlahdata.Text = "";
lbl_index.Text = "";
}
else
{
lbl_hasil.Text = lbl_hasil.Text +"Data " + cari + " ditemukan!";
lbl_jumlahdata.Text = lbl_jumlahdata.Text + count;
for (x = 0; x < count; x++)
{
lbl_index.Text = lbl_index.Text + index[x] + ", ";
}
}
}
private void btn_input_Click(object sender, EventArgs e)
{
if (txt_input.Text != "")
{
lst_data.Items.Add(txt_input.Text);
txt_input.Clear();
txt_input.Focus();
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sorting
{
public partial class frm_linear : Form
{
int count = 0;
int x;
public frm_linear()
{
InitializeComponent();
}
private static int search(int[] arr, int x)
{
int n = arr.Length;
for (int i = 0; i < n; i++)
{
if (arr[i] == x)
return i;
}
return -1;
}
private void btn_search_Click(object sender, EventArgs e)
{
// 1. Simpan data dari listbox kedalam array
int n = lst_data.Items.Count;
int[] data = new int[n];
// pindahkan data kedalam array
for (int i = 0; i < n; i++)
{
data[i] = int.Parse(lst_data.Items[i].ToString());
}
// cari data
int cari = int.Parse(txt_search.Text);
// 2. Panggil fungsi search
int result = search(data, cari);
// buat array index
int[] index = new int[n];
// hitung posisi indexnya dan jumlah data
for (int i = 0; i < n; i++)
{
if (data[i] == cari) {
index[count] = i;
count++;
}
}
// tampilkan hasil jumlah data dan index di label
if (result == -1)
{
lbl_hasil.Text = lbl_hasil.Text +"Data " + cari + " tidak ditemukan!";
lbl_jumlahdata.Text = "";
lbl_index.Text = "";
}
else
{
lbl_hasil.Text = lbl_hasil.Text +"Data " + cari + " ditemukan!";
lbl_jumlahdata.Text = lbl_jumlahdata.Text + count;
for (x = 0; x < count; x++)
{
lbl_index.Text = lbl_index.Text + index[x] + ", ";
}
}
}
private void btn_input_Click(object sender, EventArgs e)
{
if (txt_input.Text != "")
{
lst_data.Items.Add(txt_input.Text);
txt_input.Clear();
txt_input.Focus();
}
}
}
}
Binary Search:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sorting
{
public partial class frm_binary : Form
{
int count = 0;
int x;
public frm_binary()
{
InitializeComponent();
}
private void btn_input_Click(object sender, EventArgs e)
{
if (txt_input.Text != "")
{
lst_data.Items.Add(txt_input.Text);
txt_input.Clear();
txt_input.Focus();
}
}
private void btn_search_Click(object sender, EventArgs e)
{
// 1. Simpan data dari listbox kedalam array
int n = lst_data.Items.Count;
int[] data = new int[n];
// pindahkan data kedalam array
for (int i = 0; i < n; i++)
{
data[i] = int.Parse(lst_data.Items[i].ToString());
}
//sort data pada array dan tampilkan
Array.Sort(data);
for (int i = 0; i < n; i++)
{
lst_datasort.Items.Add(data[i]);
}
// cari data
int cari = int.Parse(txt_search.Text);
// 2. Panggil fungsi binary search
int result = binarySearch(data, 0, n - 1, cari);
// buat array index
int[] index = new int[n];
// hitung posisi indexnya dan jumlah data
for (int i = 0; i < n; i++)
{
if (data[i] == cari)
{
index[count] = i;
count++;
}
}
// tampilkan hasil jumlah data dan index di label
if (result == -1)
{
lbl_hasil.Text = lbl_hasil.Text + "Data " + cari + " tidak ditemukan!";
lbl_jumlahdata.Text = "";
lbl_index.Text = "";
}
else
{
lbl_hasil.Text = lbl_hasil.Text + "Data " + cari + " ditemukan!";
lbl_jumlahdata.Text = lbl_jumlahdata.Text + count;
for (x = 0; x < count; x++)
{
lbl_index.Text = lbl_index.Text + index[x] + ", ";
}
}
}
// Returns index of x if it is present in
// arr[l..r], else return -1
static int binarySearch(int[] arr, int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l) / 2;
// If the element is present at the
// middle itself
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, r, x);
}
// We reach here when element is not present
// in array
return -1;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sorting
{
public partial class frm_binary : Form
{
int count = 0;
int x;
public frm_binary()
{
InitializeComponent();
}
private void btn_input_Click(object sender, EventArgs e)
{
if (txt_input.Text != "")
{
lst_data.Items.Add(txt_input.Text);
txt_input.Clear();
txt_input.Focus();
}
}
private void btn_search_Click(object sender, EventArgs e)
{
// 1. Simpan data dari listbox kedalam array
int n = lst_data.Items.Count;
int[] data = new int[n];
// pindahkan data kedalam array
for (int i = 0; i < n; i++)
{
data[i] = int.Parse(lst_data.Items[i].ToString());
}
//sort data pada array dan tampilkan
Array.Sort(data);
for (int i = 0; i < n; i++)
{
lst_datasort.Items.Add(data[i]);
}
// cari data
int cari = int.Parse(txt_search.Text);
// 2. Panggil fungsi binary search
int result = binarySearch(data, 0, n - 1, cari);
// buat array index
int[] index = new int[n];
// hitung posisi indexnya dan jumlah data
for (int i = 0; i < n; i++)
{
if (data[i] == cari)
{
index[count] = i;
count++;
}
}
// tampilkan hasil jumlah data dan index di label
if (result == -1)
{
lbl_hasil.Text = lbl_hasil.Text + "Data " + cari + " tidak ditemukan!";
lbl_jumlahdata.Text = "";
lbl_index.Text = "";
}
else
{
lbl_hasil.Text = lbl_hasil.Text + "Data " + cari + " ditemukan!";
lbl_jumlahdata.Text = lbl_jumlahdata.Text + count;
for (x = 0; x < count; x++)
{
lbl_index.Text = lbl_index.Text + index[x] + ", ";
}
}
}
// Returns index of x if it is present in
// arr[l..r], else return -1
static int binarySearch(int[] arr, int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l) / 2;
// If the element is present at the
// middle itself
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, r, x);
}
// We reach here when element is not present
// in array
return -1;
}
}
}
Jump Search:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sorting
{
public partial class frm_jump : Form
{
int count = 0;
int x;
public frm_jump()
{
InitializeComponent();
}
private void btn_input_Click(object sender, EventArgs e)
{
if (txt_input.Text != "")
{
lst_data.Items.Add(txt_input.Text);
txt_input.Clear();
txt_input.Focus();
}
}
private void btn_search_Click(object sender, EventArgs e)
{
// 1. Simpan data dari listbox kedalam array
int n = lst_data.Items.Count;
int[] data = new int[n];
// pindahkan data kedalam array
for (int i = 0; i < n; i++)
{
data[i] = int.Parse(lst_data.Items[i].ToString());
}
// sort data pada array dan tampilkan
Array.Sort(data);
for (int i = 0; i < n; i++)
{
lst_datasort.Items.Add(data[i]);
}
// cari data
int cari = int.Parse(txt_search.Text);
// 2. Panggil fungsi jump search
int result = jumpSearch(data, cari);
// buat array index
int[] index = new int[n];
// hitung posisi indexnya dan jumlah data
for (int i = 0; i < n; i++)
{
if (data[i] == cari)
{
index[count] = i;
count++;
}
}
// tampilkan hasil jumlah data dan index di label
if (result == -1)
{
lbl_hasil.Text = lbl_hasil.Text + "Data " + cari + " tidak ditemukan!";
lbl_jumlahdata.Text = "";
lbl_index.Text = "";
}
else
{
lbl_hasil.Text = lbl_hasil.Text + "Data " + cari + " ditemukan!";
lbl_jumlahdata.Text = lbl_jumlahdata.Text + count;
for (x = 0; x < count; x++)
{
lbl_index.Text = lbl_index.Text + index[x] + ", ";
}
}
}
public static int jumpSearch(int[] arr, int x)
{
int n = arr.Length;
// Finding block size to be jumped
int step = (int)Math.Floor(Math.Sqrt(n));
// Finding the block where element is
// present (if it is present)
int prev = 0;
while (arr[Math.Min(step, n) - 1] < x)
{
prev = step;
step += (int)Math.Floor(Math.Sqrt(n));
if (prev >= n)
return -1;
}
// Doing a linear search for x in block
// beginning with prev.
while (arr[prev] < x)
{
prev++;
// If we reached next block or end of
// array, element is not present.
if (prev == Math.Min(step, n))
return -1;
}
// If element is found
if (arr[prev] == x)
return prev;
return -1;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sorting
{
public partial class frm_jump : Form
{
int count = 0;
int x;
public frm_jump()
{
InitializeComponent();
}
private void btn_input_Click(object sender, EventArgs e)
{
if (txt_input.Text != "")
{
lst_data.Items.Add(txt_input.Text);
txt_input.Clear();
txt_input.Focus();
}
}
private void btn_search_Click(object sender, EventArgs e)
{
// 1. Simpan data dari listbox kedalam array
int n = lst_data.Items.Count;
int[] data = new int[n];
// pindahkan data kedalam array
for (int i = 0; i < n; i++)
{
data[i] = int.Parse(lst_data.Items[i].ToString());
}
// sort data pada array dan tampilkan
Array.Sort(data);
for (int i = 0; i < n; i++)
{
lst_datasort.Items.Add(data[i]);
}
// cari data
int cari = int.Parse(txt_search.Text);
// 2. Panggil fungsi jump search
int result = jumpSearch(data, cari);
// buat array index
int[] index = new int[n];
// hitung posisi indexnya dan jumlah data
for (int i = 0; i < n; i++)
{
if (data[i] == cari)
{
index[count] = i;
count++;
}
}
// tampilkan hasil jumlah data dan index di label
if (result == -1)
{
lbl_hasil.Text = lbl_hasil.Text + "Data " + cari + " tidak ditemukan!";
lbl_jumlahdata.Text = "";
lbl_index.Text = "";
}
else
{
lbl_hasil.Text = lbl_hasil.Text + "Data " + cari + " ditemukan!";
lbl_jumlahdata.Text = lbl_jumlahdata.Text + count;
for (x = 0; x < count; x++)
{
lbl_index.Text = lbl_index.Text + index[x] + ", ";
}
}
}
public static int jumpSearch(int[] arr, int x)
{
int n = arr.Length;
// Finding block size to be jumped
int step = (int)Math.Floor(Math.Sqrt(n));
// Finding the block where element is
// present (if it is present)
int prev = 0;
while (arr[Math.Min(step, n) - 1] < x)
{
prev = step;
step += (int)Math.Floor(Math.Sqrt(n));
if (prev >= n)
return -1;
}
// Doing a linear search for x in block
// beginning with prev.
while (arr[prev] < x)
{
prev++;
// If we reached next block or end of
// array, element is not present.
if (prev == Math.Min(step, n))
return -1;
}
// If element is found
if (arr[prev] == x)
return prev;
return -1;
}
}
}
Visual Studio 2012 C# C sharp membuat Form tampilan GUI Algoritma Pemrograman Searching Array Linear Search, Binary Search, Jump Search
Agus Mulyanto, S.Kom., M.T., M.Sc | Universitas Teknokrat Indonesia
Teknik Informatika IF18Fx
Tidak ada komentar:
Posting Komentar