Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello.. i need help.. can someone tell me the code for 4 way merge sort.
input is excel file with 10000 random numbers and I need to sort with 4 way merge sort using C#.
Thanks before.
Posted
Comments
Joezer BH 5-Jun-13 8:24am    
Best way to learn is to try something yourself and then ask specific questions rather than just asking for the complete code.

1 solution

 
Share this answer
 
Comments
vand6 5-Jun-13 8:38am    
I tried it myself based on 2 way merge sort but I have some problems.
My code is shown below


namespace MergeSort
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

List<int> unsorted = new List<int>();
public string connstr =
@"Provider=Microsoft.Ace.Oledb.12.0;Data Source=C:\Users\OEM\Desktop\Merge.xlsx;Excel 12.0;
Extended Prperties=HDR=YES";

private void btnFill_Click(object sender, EventArgs e)
{

DataTable dtbl = new DataTable();
OleDbConnection conn = new OleDbConnection(connstr);
string strSql = "SELECT * FROM [data$]";
OleDbCommand cmd = new OleDbCommand(strSql, conn);
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
dtbl = ds.Tables.Add("Tabela");
da.Fill(dtbl);
foreach (DataRow row in dtbl.Rows)
{
unsorted.Add(int.Parse(row[0].ToString()));
}
var wrapped = (from i in unsorted select new { Value = i }).ToArray();
dgvExcel.AutoGenerateColumns = true;
dgvExcel.DataSource = wrapped;


}

public List<int> concat(List<int> less, int pivot, List<int> greater)
{

List<int> sorted = new List<int>(less);

sorted.Add(pivot);

foreach (int i in greater)
{
sorted.Add(i);

}
return sorted;
}

#region "merge sort"

public List<int> mergesort(List<int> m)
{
List<int> result = new List<int>();
List<int> first = new List<int>();
List<int> second = new List<int>();
List<int> third = new List<int>();
List<int> fourth = new List<int>();


if (m.Count <= 1)
return m;

int one = m.Count / 4;
for (int i = 0; i < one; i++)
first.Add(m[i]);
int two = one + one;
for (int i = one; i < two; i++)
second.Add(m[i]);
int three = one + two;
for (int i = two; i < three; i++)
third.Add(m[i]);
for (int i = three; i <m.count ;="" i++)
="" fourth.add(m[i]);
=""


="" first="mergesort(first);
" second="mergesort(second);
" third="mergesort(third);
" fourth="mergesort(fourth);
"
="" if="" (first[first.count="" -="" 1]="" <="second[0]" &&="" second[second.count-1]<="third[0]" third[third.count-1]<="fourth[0]" )
="" return="" append(first,="" second,="" third,="" fourth);

="" result="merge(first," fourth);
="" result;

="" }

="" public="" list<int=""> append(List<int> a, List<int> b, List<int> c, List<int> d)
{
List<int> result1 = new List<int>(a);
List<int> result2 = new List<int>(b);
List<int> result3 = new List<int>(c);
List<int> result4 = new List<int>(d);

foreach (int x in b)
result1.Add(x);
foreach (int y in d)
result3.Add(y);
return result1;
}

public List<int> merge(List<int> a, List<int> b, List<int> c, List<int> d)
{
List<int> s = new List<int>();
while (a.Count > 0 && b.Count > 0 && c.Count>0 && d.Count>0 && e.Count>0)
{
if (a[0] < b[0] && b[0]<c[0] &&="" c[0]<d[0]="" d[0]<e[0])
="" {
="" s.add(a[0]);
="" a.removeat(0);
="" }
="" else
="" s.add(b[0]);
="" b.removeat(0);
="" while="" (a.count=""> 0)
{
s.Add(a[0]);
a.RemoveAt(0);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900