C # Insert Sort bubble Sort Select sort Quick sort Heap sort Merge sort base Sort Hill sort

Source: Internet
Author: User

C # Insert Sort bubble Sort Select sort Quick sort Heap sort Merge sort base Sort Hill sortThe following is a list of eight basic sorts of data structures and algorithms: Insert sort bubble Sort Select sort Quick sort Heap sort Merge sort base sort Hill sort and then test example. Code location: http://download.csdn.net/detail/luozuolincool/8040027 Sort class:public class Sortings
{
Insert Sort
public void Insertsort (int[] array)
{
int temp = 0;
int index = 0;
for (int i = 0; i < array. Length; i++)
{
for (int j = 0; J < i; J + +)
{
if (Array[i] < ARRAY[J])//from J to i the number of the whole right move, put I data to J position
{
index = i;
temp = Array[i];
while (Index > J)
{
Array[index] = array[index-1];
index--;
}
ARRAY[J] = temp;
Break
}
}
}
PrintArray (Array, "insert sort:");
}
public void PrintArray (int[] array, String type)
{
Console.WriteLine (type);
for (int i = 0; i < array. Length; i++)
{
Console.Write (Array[i] + ",");
}
Console.WriteLine ();
}
Bubble sort
public void Bubblesort (int[] array)
{
int temp = 0;
BOOL exchanged = true;
for (int i = 0; i < array. Length; i++)
{
if (!exchanged)
Break
for (int j = Array. Length-1; J > i; j--)
{
exchanged = false;
if (Array[j] < array[j-1])//The number behind is smaller than the previous exchange
{
temp = Array[j];
ARRAY[J] = array[j-1];
ARRAY[J-1] = temp;
exchanged = true;
}
}
}
PrintArray (Array, "bubble sort:");
}
Select sort: From front to back, select the smallest one first, the second small one in the second position.
public void Selectionsort (int[] array)
{
int minindex = 0;
int temp = 0;
for (int i = 0; i < array. Length; i++)
{
Minindex = i;
for (int j = i; J < Array. Length; J + +)
{
if (Array[j] < Array[minindex])
Minindex = j;
}
Place the smallest number I to J in position I
temp = Array[minindex];
Array[minindex] = Array[i];
Array[i] = temp;
}
PrintArray (Array, "select Sort:");
}
Quick Sort
public void QuickSort (int[] array)
{
Quicksort1 (array, 0, array. LENGTH-1);
PrintArray (Array, "quick sort:");
}
public void Quicksort1 (int[] array, int start, int end)
{
if (Start >= end)
Return
int i = start, j = end;
int k = Array[i];
while (I < j)
{
while (Array[j] >= k && i < J)
j--;
Array[i] = Array[j];
while (Array[i] < K && I < J)
i++;
ARRAY[J] = Array[i];
}
Array[i] = k;
Quicksort1 (array, start, i-1);
Quicksort1 (array, I +1, end);
}
Heap Sort
public void Stacksort (int[] array)
{
Myheap h = new Myheap ();
H.buildheap (array);
H.heapsort ();
H.printheap ();
}
Merge sort
public void MergeSort (int[] array)
{
MergeSort1 (array, 0, array. LENGTH-1);
PrintArray (Array, "merge:");
}
private void MergeSort1 (int[] array, int start,int end)
{
if (Start >= end)
Return
MergeSort1 (Array, start, (start+end)/2);
MergeSort1 (Array, (start + end)/2+1,end);
Merge (array, start, (start + end)/2, end);

}
private void Merge (int[] array,int start,int mid,int end)
{
queue<int> q = new queue<int> ();
int i = start, J = mid + 1;
while (i <=mid && J <=end)
{
if (Array[i] < array[j])
{
Q.enqueue (Array[i]);
i++;
}
Else
{
Q.enqueue (Array[j]);
j + +;
}
}
while (I <= mid)
Q.enqueue (array[i++]);
while (j <= End)
Q.enqueue (array[j++]);

for (i = start; I <= end; i++)
Array[i] = Q.dequeue ();
}
Base sort
public void Radixsort (int[] array)
{
int maxlength=0;//data Maximum number of digits
Application space for data storage
List<list<int>> lists=new list<list<int>> ();
Apply for 10 barrels for storage of 0-9
for (int i = 0; i <; i++)
Lists. ADD (New list<int> ());
Get the maximum number of bits of data
for (int i = 0; i < array. Length; i++)
MaxLength = MaxLength < Array[i]. ToString (). Length? Array[i]. ToString (). Length:maxlength;
for (int i = 0; i < maxlength; i++)
{
Data into the bucket
for (int j = 0; J < Array. Length; J + +)
{
LISTS[ARRAY[J]/(int) (Math.pow (Ten, I))-array[j]/(int) (Math.pow (Ten, i+1)) *10]. ADD (Array[j]);
}
int t = 0;
Put the data inside the bucket back into the array
for (int k = 0; k < k++)
{

foreach (int item in LISTS[K])
array[t++] = Item;
}
Empty the data inside the bucket
for (int k = 0; k < k++)
{
LISTS[K]. Clear ();
}


}
PrintArray (Array, "radix sort");
}
Hill sort
public void Shellsort (int[] array)
{
int step = array. LENGTH/2;
while (Step > 0)
{
Shellinsert (array, step);
Console.WriteLine ();
PrintArray (Array, "Hill");
Step = STEP/2;
}
PrintArray (Array, "Hill");
}
private void Shellinsert (int[] array,int step)
{
int temp = 0;
int index = 0;
for (int i = 0; i < array. Length; I=i+step)
{
for (int j = 0; J < i; j=j+step)
{
if (Array[i] < ARRAY[J])//from J to i the number of the whole right move, put I data to J position
{
index = i;
temp = Array[i];
while (Index > J)
{
Array[index] = array[index-1];
index--;
}
ARRAY[J] = temp;
Break
}
}
}
}
}
Test Code:static void Main (string[] args)
{
Int[] array = {12,3,23,4,21,44,2,3,11};
Console.WriteLine ("Array to be sorted:");
for (int i = 0; i < array. Length; i++)
{
Console.Write (array[i]+ ",");
}
Console.WriteLine ();
Insertsort (array);
Bubblesort (array);
Selectionsort (array);
(New Sortings ()). QuickSort (array);
(New Sortings ()). Stacksort (array);
(New Sortings ()). MergeSort (array);
(New Sortings ()). Shellsort (array);
(New Sortings ()). Radixsort (array);
Console.read ();
}

C # Insert Sort bubble Sort Select sort Quick sort Heap sort Merge sort base Sort Hill sort

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.