C#如何编写一个函数用来实现对一个整型数组中的10个数升序排列。

2024-12-25 12:35:57
推荐回答(4个)
回答1:

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();
}
}

回答2:

using System;

namespace VcQuickSort
{
///


/// ClassQuickSort 快速排序。
/// 范维肖
///

public class QuickSort
{
public QuickSort()
{
}

private void Swap(ref int i,ref int j)
//swap two integer
{
int t;
t=i;
i=j;
j=t;
}

public void Sort(int [] list,int low,int high)
{
if(high<=low)
{
//only one element in array list
//so it do not need sort
return;
}
else if (high==low+1)
{
//means two elements in array list
//so we just compare them
if(list[low]>list[high])
{
//exchange them
Swap(ref list[low],ref list[high]);
return;
}
}
//more than 3 elements in the arrary list
//begin QuickSort
myQuickSort(list,low,high);
}

public void myQuickSort(int [] list,int low,int high)
{
if(low{
int pivot=Partition(list,low,high);
myQuickSort(list,low,pivot-1);
myQuickSort(list,pivot+1,high);
}
}

private int Partition(int [] list,int low,int high)
{
//get the pivot of the arrary list
int pivot;
pivot=list[low];
while(low{
while(low=pivot)
{
high--;
}
if(low!=high)
{
Swap(ref list[low],ref list[high]);
low++;
}
while(low{
low++;
}
if(low!=high)
{
Swap(ref list[low],ref list[high]);
high--;
}
}
return low;
}

}
}

回答3:

乱写算法!!

建议LZ使用快速排序,核心思路就是:比较-置换-递归

///


/// 指定数组起始和结束位置,对数组进行快速排序
///

/// 数组起始下标
/// 数组结束下标
/// 排序数组
public static void QuickSort(int startIndex, int endIndex, int[] x)

回答4:

如果你是要要交作业,自己写
如果你是要实现这个功能,用Array.Sort(数组名)就可以了