Insert | sort | algorithm
This article describes the four sort algorithms for C #: Bubble sort, select sort, insert sort, and Hill sort
Bubble sort
Using System;
Namespace Bubblesorter
{public class Bubblesorter
{public void Sort (int [] list)
{int i,j,temp;
BOOL Done=false;
J=1;
while (j
{done=true;
For (i=0;i
{
if (list[i]>list[i+1])
{
Done=false;
Temp=list[i];
LIST[I]=LIST[I+1];
List[i+1]=temp;
} }
j + +;}
} }
public class MainClass
{public static void Main ()
{
Int[] Iarrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
Bubblesorter sh=new Bubblesorter ();
Sh. Sort (iarrary);
for (int m=0;m
Console.Write ("{0}", Iarrary[m]);
Console.WriteLine ();
} }
}
Select sort
Using System;
Namespace Selectionsorter
{public class Selectionsorter
{private int min;
public void Sort (int [] list)
{for (int i=0;i
{min=i;
for (int j=i+1;j
{if (List[j]
Min=j;
}
int t=list[min];
List[min]=list[i];
list[i]=t;
} }
}
public class MainClass
{public static void Main ()
{
Int[] Iarrary=new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
Selectionsorter ss=new Selectionsorter ();
Ss. Sort (iarrary);
for (int m=0;m
Console.Write ("{0}", Iarrary[m]);
Console.WriteLine ();
} }
}
Insert Sort
Using System;
Namespace Insertionsorter
{public class Insertionsorter
{public void Sort (int [] list)
{for (int i=1;i
{int t=list[i];
int j=i;
while ((j>0) && (list[j-1]>t))
{list[j]=list[j-1];
--j;
}
list[j]=t;}
}
}
public class MainClass
{public static void Main ()
{
Int[] Iarrary=new int[]{1,13,3,6,10,55,98,2,87,12,34,75,33,47};
Insertionsorter ii=new Insertionsorter ();
Ii. Sort (iarrary);
for (int m=0;m
Console.Write ("{0}", Iarrary[m]);
Console.WriteLine ();
} }
}
Hill sort
The hill sort is to insert the component segments into the sort.
Using System;
Namespace Shellsorter
{
public class Shellsorter
{
public void Sort (int [] list)
{
INT Inc;
For (inc=1;inc<=list. LENGTH/9;INC=3*INC+1);
for (; inc>0;inc/=3)
{
for (int i=inc+1;i<=list. LENGTH;I+=INC)
{
int t=list[i-1];
int j=i;
while ((J>inc) && (list[j-inc-1]>t))
{
LIST[J-1]=LIST[J-INC-1];
J-=inc;
}
list[j-1]=t;
} }
} }
public class MainClass
{public static void Main ()
{
Int[] Iarrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
Shellsorter sh=new Shellsorter ();
Sh. Sort (iarrary);
for (int m=0;m
Console.Write ("{0}", Iarrary[m]);
Console.WriteLine ();
} }
}