新年散分,回帖均有分,顺便收集排序算法
新的一年已经开始了,在此祝大家新年快乐!在新的一年里工作更上一层楼!
顺便在这里收集一些排序算法,以int数组为例,条件是不要用Sort方法,否则一个Array.Sort()就搞定了。
我先发一个冒泡排序的算法(稍微优化了一下):
private int[] ArraySort(int[] array) { int temp; bool noSwap = true; for (int i = 0; i < array.Length; i++) { for (int j = i + 1; j < array.Length; j++) { if (array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; noSwap = false; } } if (noSwap) return array;//没有再发生交换,排序结束 else noSwap = true; } return array; }private static void Shell_Sort(int[] b) { int[] a = new int[b.Length]; b.CopyTo(a, 0); int key; Console.WriteLine("Shell排序"); int gap=5,k; for(;gap>0;gap/=2) for (int j = gap; j < 10; j++) { if (a[j] < a[j - gap]) { key = a[j]; for (k = j - gap; k >= 0 ; k -= gap) { if (key < a[k]) { a[k + gap] = a[k]; } else break; } a[k + gap] = key; } } Print(a); }
[解决办法]
up
[解决办法]
/**
* 插入排序数组
* @author Edwin
* @version 1.1
*/
public class InsertionArray {
/**
* 插入排序数组
* @param lngArr为要排序的数组
*/
public void insertionSort(long[]lngArr)
{
int intOut=0,intIn=0,intElems=lngArr.length;
for(intOut=1; intOut<intElems; intOut++)
{
long temp = lngArr[intOut];
intIn = intOut;
while(intIn>0 && lngArr[intIn-1] >= temp)
{
lngArr[intIn] = lngArr[intIn-1];
--intIn;
}
lngArr[intIn] = temp;
} // end for
} // end insertionSort()
}
---------------------
等会更精彩!!!!!
[解决办法]
冒泡,插入,选择,最常用的三种排序方法
新年好
[解决办法]
书本上的
[解决办法]
HAPPY NEW YEAR
[解决办法]
同上面
原先有一个输出比较次数和交换次数的
我刚才删除了那部分了.....
private static void Select_Sort(int[] b) { int[] a = new int[b.Length]; b.CopyTo(a, 0); Console.WriteLine("选择排序"); int min,temp; for(int i=0;i<9;i++) { min=i; for (int j = i + 1; j < 10; j++) { if (a[min] > a[j]) min = j; } if(min!=i) { temp=a[i]; a[i]=a[min]; a[min]=temp; } } Print(a); }
[解决办法]
Happy 牛 year
[解决办法]
冒泡法?
[解决办法]
快速排序:
private int[] m_arrValue = new int[100];/// <summary>/// 进行快速排序/// </summary>/// <param name="nLow">排序起始元素索引</param>/// <param name="nHigh">排序结束元素索引</param>private void QuickSort(int nLow, int nHigh){ int nPivotPos = 0; if (nLow < nHigh) { nPivotPos = this.Partition(nLow, nHigh); this.QuickSort(nLow, nPivotPos - 1); this.QuickSort(nPivotPos + 1, nHigh); }}/// <summary>/// 执行一趟快速排序过程/// </summary>/// <param name="nLow">一趟快速排序的起始元素索引</param>/// <param name="nHigh">一趟快速排序的结束元素索引</param>/// <returns>枢轴元素索引</returns>private int Partition(int nLow, int nHigh){ int pivotValue = this.m_arrValue[nLow]; while (nLow < nHigh) { while (nLow < nHigh && this.m_arrValue[nHigh] >= pivotValue) nHigh--; if (nLow < nHigh) this.m_arrValue[nLow++] = this.m_arrValue[nHigh]; while (nLow < nHigh && this.m_arrValue[nLow] <= pivotValue) nLow++; if (nLow < nHigh) this.m_arrValue[nHigh--] = this.m_arrValue[nLow]; } this.m_arrValue[nLow] = pivotValue; return nLow;}
[解决办法]
快乐 paopao
[解决办法]
接分 元旦快乐
[解决办法]
新年好
[解决办法]
happy 牛 year````````````````````````
[解决办法]
新年快乐~
[解决办法]
jf新年快乐
[解决办法]
新年快乐
[解决办法]
------解决方案--------------------
新年快乐~~
[解决办法]
新年快乐
[解决办法]
新年快乐
[解决办法]
新年快乐
[解决办法]
新年快乐
[解决办法]
新年快乐
[解决办法]
新年快乐!
[解决办法]
╭┘└┘└╮
└┐..┌┘────╮
╭┴──┤ ├╮
│o o│ │ ●
╰─┬─╯ │
HAPPY 牛 YEAR
[解决办法]
冒泡 bubble
[解决办法]
堆排序 heap
[解决办法]
快速排序 quicksort
[解决办法]
新年快乐
[解决办法]
呵呵~~
排序 ---
新年新气象
大家不重样
[解决办法]
n趟m路排序
二叉平衡树
红黑树
B树
------------------------------
[解决办法]
简单一点
void quickSort(char* arr,int startPos, int endPos) { int i,j; char ch; ch=arr[startPos]; i=startPos; j=endPos; while(i<j) { while(arr[j>=ch && i<j)--j; arr=arr[j]; while(arr<[i]=ch && i<j)++i; arr[j]=arr[i]; } arr[i]=ch; if(i-1>startPos) quickSort(arr,startPos,i-1); if(endPos>i+1) quickSort(arr,i+1,endPos); }
[解决办法]
happy new year
[解决办法]
happy 牛 year
[解决办法]
新年快乐^_^
[解决办法]
新来,新年好!
[解决办法]